Author Archives: Diegops

!

This is how it looks my finished program about factorial:

https://github.com/diegoplascencia/wsq09/blob/master/.gitignore

Matriz

Vamos a aprender a declarar y usar una matriz o arreglo bidimensional. Se declara de manera similar a un vector:

int matrix[filas][columnas];

En donde primero va el tipo de dato (char, float, double, int...). Las matrices se almacenan en posiciones consecutivas de memoria.

La forma de acceder a los elementos de la matriz es utilizando su nombre e indicando 2 subindices, que van en los corchetes. eg int matriz[2][3] = 10

tanto filas como columnas se comienzan a enumerar a partir de 0.

aqui puedes ver mas informacion sobre arreglos bidimensionales:

http://www.cplusplus.com/doc/tutorial/arrays/

 

STRING

Diego Plascencia Sanabria A01229988

Strings are used very often in programming, strings are used to return non numerical values like words or phrases or even letters. Here is an example of a program that use strings:

<iostream>

using namespace std;

string my_string1 = "a string";
string my_string2 = " is this";
string my_string3 = my_string1 + my_string2;

// Will ouput "a string is this"
cout<<my_string3<<endl;

As you can see, it is very easy, hope it helps you.

Untitled

Diego Plascencia Sanabria A01229988

An array stores a fixed sized sequential collection of elements of the same type, it is easier than creating different variables of the same type.

Declaring arrays.

You will need to specify the type of the elements and the number of elements required by an array:

type arrayName [ arraySize ];

eg:

double balance[10];

You can initialize arrays like shown in these examples:

double balance[5] = {1000.0, 2.0, 3.4, 17.0, 50.0};
(In this first case, the number of elements between braces cant belarger
than the number of elements that we declare for the array between brackets.)

double balance[] = {1000.0, 2.0, 3.4, 17.0, 50.0};
(In the second case, in the one you omited the size of
the array, the size will be as big as number of values that you decide.)

Here is the order that the values will gain in our array.

Untitled
visit the link for more detailed info:
http://www.tutorialspoint.com/cplusplus/cpp_arrays.htm

VECTORS

Diego Plascencia Sanabria A01229988

Vectors are sequence containers that can change in size. Its function is to store different values under a single name.

When using vectors in our programs we must include in the top of our program <vector>

The values can be in any data type (int, double, string…).

Vectors are declared with the following syntax:

           vector<type> variable_name (number_of_elements);

or

          vector<type> variable_name;  (the number of elements is optional).

Here are some example of vectors:

    vector<int> values (5);      // Declares a vector of 5 integers
    vector<double> grades (20);  // Declares a vector of 20 doubles
    vector<string> names;        // Declares a vector of strings,  

 

GCD

 

The great common divider can look like a difficult program, but something that help me a lot in this program was writing on a paper the solution, so i can visualize it better.

Take a look to my code at github:

 https://github.com/diegoplascencia/wsq12/blob/master/.gitignore

Use of recursion for repetitive algorithms

In simple words, recursion is when a function calls itself, creating a loop. Recursion could be used to complete the fibbonacci serie, here is an example of a program with recursion:

 <iostream>

using namespace std;

int fib(int x) {
    if (x == 1) {
        return 1;
    } else {
        return fib(x-1)+fib(x-2);
    }
}

int main() {
    cout << fib(5) << endl;
}

As you can see in the example above, the function fib is used inside the same funtion
)it is calling itself).

Thank you for reading my explanation, I hope it have helped you. For a more complete
explanation take a look at this link:

http://computacion.cs.cinvestav.mx/~acaceres/courses/estDatosCPP/node37.html

TYPE OF REPETITION

DIEGO PLASCENCIA SANABRIA A01229988

Existen tres tipos de loops:

While: se usa para que el codigo se repita un numero indefinido de veces, hasta que la condición sea falsa.

For: se usa cuando tu le das el valor hasta el cual la condición se hará negativa, tu la defines.

Recursion: cuando necesitas llamar a la funcion dentro de la misma funcion.

FOR

DIEGO PLASCENCIA SANABRIA A01229988

Un for es un loop que te permite que se repita un numero especifico de ocasiones.

Su sintaxis:

for ( iniciador; condicion; incremento)
{
   cuerpo;
}
  • El iniciador se ejecuta primero y solo es ejecutado una vez. Te permite iniciar y declarar la variable de control del loop.

  • Despues, se evalua la condicion. Si es verdad, el cuerpo del loop se ejecuta, si es falsa el cuerpo no se ejecuta y se pasa a la siguiente declaracion.

  • Despues de que se ejecuta el cuerpo del loop, se regresa la declaracion del incremento. Esto hace que se actualicen las variables del loop.

  • La condicion es evaluada de nuevo, si es verdad se ejecuta el loop y el proceso se repite en el mismo orden y termina hasta que la condicion se haga falsa.

Aqui el diagrama de flujo de un for:

Gracias por leerlo, espero haberte ayudado, 
si quieres mas informacion puedes visitar este link:

http://www.tutorialspoint.com/cplusplus/cpp_for_loop.htm

WHILE

DIEGO PLASCENCIA SANABRIA A01229988

Un while es un loop que se repetirá mientras que el resultado de la condicion dada resulte positivo.

Literalmente se seguira repitiendo mientras la condicion sea positiva.

En caso de que la condicion sea falsa, el programa pasa a la siguite accion, puede ser otro loop o simplemente se detiene. Si la condicon es falsa desde el comienzo, el while podria nunca correr ya que desde el inicio es falsa la condicion.

Aqui les dejo un diagrama de flujo del while:

Gracias por leerlo, espero haberte ayudado, 
si quieres mas informacion puedes visitar este link:

http://www.tutorialspoint.com/cplusplus/cpp_while_loop.htm