Monthly Archives: February 2015

#mastery20 – Use of loops with FOR

Primero se debe de llamar a la librería iostream, y así poder utilizar datos de entrada y salida.

También se debe de definir el using namespace std.

Una vez esto, se debe de introducir el programa principal, el int main.

Se deben de definir las variables.

Después, se debe de preguntar por la variable con el cin >>

Una vez esto, se aplica un ciclo FOR para saber su tabla de multiplicar.

Un ciclo FOR sigue el siguiente formato:

Se escribe for, y dentro de parentesis, se hacen tres cosas:

1, Se declara al contador y el valor que tendrá al principio.

2, se declara la condición con la que se estará en el ciclo for (una vez que la condición no se cumple, se sale del ciclo). Esta condición debe de tener involucrado al contador.

3, Cómo irá incrementando o decreciendo el contador.

Un ciclo FOR, tiene como diferencia al While que, solo puede ser utilizado cuando se sabe exactemente el numero de veces que se repetirá el ciclo, contrario a lo que hace un while, que se puede aplicar sin saber cuantas veces se repetirá.

Dentro del ciclo FOR, se irá imprimiendo la multiplicación del numero proporcionado por el usuario, por 1, 2, 3,…, hasta llegar al 10.

Se compila y se corre el programa.

 

Aquí está el código:

https://github.com/LuluisAngel/20/tree/master

 

Foto del programa corriendo:

 

Factorial Calculator – WSQ09

Factorial Calculator – WSQ09

This program asks the user for a number and prints the factorial result. It uses a function in which, as long as the variable a is a positive number, then it does the product of b (which has the value of 1) and a (given by the user), using a loop to stop the program when a is equal to itself.

The purpose of the if – else conditional is to make sure, once again, the user is giving the program a positive integer, if the number inserted is lower than 0, the program will ask the user to try again with a positive integer.

To asure this program keeps running while the user wants it to, we use another loop, defining the value for as the character (‘y’), so after it prints the answer it also asks the user if they’d like to try another number, the program will only do the loop if the user answers with y.

Github code link.

Factorial Calculator – WSQ09

Fun with Numbers (Function) – WSQ08

Fun with Numbers (Function) – WSQ08

First, you need to define the function, and state the variables you’re gonna use in parenthesis, they have to be different from the ones you’re gonna use to ask the user the value of these, then you do the operation under another variable, when you are done with this, type return and the name of the variable that contains said operation. You must do this for every function you create.

After that, ask for the original variables, the ones that are supposed to be different from the ones you initially did the function(s) with, create one last variable that will be equal to the name of the function you want, finally print the result using the variable you just created, which contains/is equal to the function.

Github code link.

Fun with Numbers (Function) – WSQ08

#mastery18- Nasting of Conditionals Statements

Primero se debe de llamar a la librería iostream, y así poder utilizar datos de entrada y salida.

También se debe de definir el using namespace std.

Una vez esto, se debe de introducir el programa principal, el int main.

Se deben de definir las variables.

Después, se debe de preguntar por las variables con el cin >>

Se debe de declarar la primera condición, y dentro de esta condición, se deben de declarar las otras condiciones.

Si la primera condición es verdadera, entonces revisará dentro del código en esa primera condicición, qué corresponde al numero dado, por lo que checará la siguiente condición y si no se cumple, hará lo del else if de la primera parte.

Si la primera condición no se cumple, hará lo que la condición en else dice.

Se compila y se corre el programa.

 

Aquí está el código:

https://github.com/LuluisAngel/18/tree/master

 

Foto del progra corriendo:

#WSQ09 – Factorial Calculator

 

Para esta actividad solo utilice un ciclo do while para validar si la persona quería seguir repitiendo el proceso, y dentro de este ciclo, realice un ciclo for para así calcular el factorial de un numero dado por el usuario. Este programa solo calcula hasta el factorial del numero 12, pero se le puede añadir cualquier numero (infinito) con un BigInteger.

No tuve que buscar como realizar este programa, o ciclo for o do while porque ya había programado con este lenguaje anteriormente. Lo que sí busque fue como poder calcular el factorial de un numero mayor a 12, pero era un poco más complicado que solo declarar el valor como un BigInteger. 

 

Aquí está el código (Para el factorial de 1-12):

https://github.com/LuluisAngel/Factorial/tree/master

 

Programa corriendo:

#WSQ09


#WSQ09

#WSQ09

/*

Main: Loops
By: Jorge Cervantes

Referencia: XXXXX
———————————————————————
*/


using namespace std;

string ans;
int fact, n;
char m;
long total;

void orden(){

if( n = 0 && n > n;
fact = 0;
total = 1;
orden();
switch(m){

case(1):

cout > ans;

break;

case(3):

cout

Untitled

Para este programa primero investigué como llamar una función, leí en el libro y después vi algunos videos como este https://www.youtube.com/watch?v=YM0utTK7Iro:

Para llamar una función primero debes hacer la función antes del int main, poner las variables involucradas y llamarlas como en el ejemplo de la imagen. poniendo un nombre asignado a la función para poder imprimirla seguido de la función y ya en un cout pones el nombre que le asignaste para que se imprima las variables que el usuario ingresó deben ir dentro de el parentesis donde pusiste las variables X y Y

#WSQ09 #TC1017

 

Factorial Calculator:

A Factorial calculator it´s defined as a calculation of the factorial. This is represented as the Multiplication of the composition of the number given. For example: If you want to calculate the factorial of 4! you should multiply 4*3*2*1=24. In a C++ programm it´s required to do a loop for making a programm that can calculate the factorial of a given number. The first thing required it´s making the structure of your programm, libraries (basic libraries), using namespace std; , int main ( ) { }, and then inside the main a loop indicating the start and the end of it it´s required. Notice that i has to be lower than 1 to define the work time of the loop. A new integer it´s required to save the result of the multiplications. On the example code it´s defined as factorial. Notice that the factorial it´s equal to the given number because the multiplication has to start with a number lower than the given number, example 4 (given number) 3 (begin of the multiplication so, the loop will do the follow equation: 4*3*2*1.

 

 

Example code:

<iostream>

using namespace std;

 

 

int main ()

{

  char A;

  do

  {

    int n,factorial;

 

    cout << “This program will display the factorial of a given number, Enjoy.”<< endl;

    cout << “Give me the number: “<< endl;

    cin >> n;

 

    factorial=n;

    for (int i=n; i>1; i–)

    {

      factorial= factorial * i ;

    }

 

    cout << “The factorial of the given number is: ” << factorial << endl;

    cout << “Would you like to repeat the program? Y/N” << endl;

    cin >> A;

  }while (A == ‘Y’);

 

  cout << “Thanks for using this program. Have a nice day “<< endl;

  return 0;

  

}

Doing Class Work Ahead Of Time

This is a program for the 9th WSQ. It contains some comments explaining why i added those lines of code or what the function does.

Factorial calculator

Aquí lo que hice fue usar un do while pero dentro de este utilicé un ciclo for para calcular un factorial que se le pidió al usuario.

Este WSQ ya es un poco más avanzado pero busque ayuda con compañeros que ya cursaron la materia y no estuvo muy dificil una vez que me explicaron.

Aquí esta el resultado de mi programa corriendo:

Aquí esta mi código:

https://github.com/fernandoaguirrer/TC1017/blob/master/WSQ09.md