Foctorial numbers

--Originally published at Solving problems with programming

This homework was a little bit more dificult for me at the biggining, i didn’t understand what was a recursion and how did they work. I wrote it just because a web page said how, but I didn’t understand how it worked. After analizing it finally i understood .

A recursion is the call of the same function that is being defined at that moment. You can define a function and at the same time use it inside that function. It is confusing to understand and to explain also hahaha, but in the code and by doing the exercise you could understarnd it.

int factorial(int n){
int resultado;
if(n>0)
resultado=n*factorial(n-1);
else if(n==0)
resultado=1;
return resultado;

I will simulate an exucution of this function. If n=2, n is bigger than 0 so, we will multiply n by the factorial of n-1 that equals 1. So the factorial of 1 is  1 multiply by the factorial of 1-1 that equals 0. So the factorial of 0 is 1, because that is said on the function, if(n==0) resultado=1. So 2*1*1 gives 2.

Here is all the code

#include <iostream>
using namespace std;
int factorial(int n){
int resultado;
if(n>0)
resultado=n*factorial(n-1);
else if(n==0)
resultado=1;
return resultado;
}
main(){
int num, res;
char op;
do{
cout<<“Escribe el número que desea convertir en factorial”<<endl;
cin>>num;
res = factorial(num);
if(res==0)
cout<<“No se puede sacar el factorial a numeros negativos “<<endl;
else{
cout<< “El número factorial es “<<res<<endl;
}
cout<<“¿Quieres calcular otro número factorial?(S/N)”;
cin>>op;
if(op==’N’||op==’n’)
cout<<“Ten un lindo día”<<endl;
}while(op==’S’||op==’s’);
return 0;
}

I also solve the factorial number with loops do while and for, that are easier to make. The code is linked if you like to take a look.


Quiz 3

--Originally published at Solving problems with programming

In quiz number 3 we have to do 2 funtcions. The first on has to return the smallest number of 3 numbers that the user gives. You are going to use conditionals and  comparisons to get the samller number. But there is a problem, if there a two numbers that are equal and he the smallest you have to manege it. What I did is tell the user that are 2 numbers that are the smaller but are the same. I know there is another way to solve it better, I need to check the block of my classmates to find a better answer. The Second Function has to return the sum of the square of each number, this function is very easy.

Here is the code 

#include
#include
using namespace std;
int minimumThree(int x, int y, int z){
if(x cout << “El número menor es= “;
return x;
} else if(y cout << “El número menor es= “;
return y;
} else if(z cout << “El número menor es= “;
return z;
} else if(z==y and z std::cout << “Hay dos números menores ” << endl;
}
}
int sumSquares(int x, int y, int z) {
int suma;
suma = pow(x,2)+ pow(y,2)+ pow(y,2);
cout <<“La suma de los números al cuadrado= “;
return suma;
}
int main(){
int num1,num2,num3;
cout<< “Escribe número 1″<<endl; cin >> num1;
cout<< “Escribe número 2″<<endl; cin >> num2;
cout<< “Escribe número 1″<<endl; cin >> num3;
cout <<minimumThree(num1,num2,num3)<<endl;
cout <<sumSquares(num1,num2,num3)<<endl;
}


Homework 5 On to functions

--Originally published at Solving problems with programming

This homework is very easy, it is about a old WSQ were we have to do many operations with the same 2 variables, but instead of writting the procedures in the main function we are going to make a function to each operation and then call it int he main function

Heres the code of WSQ05 and the code of WSQ01 or the blogpost Fun With Numbers, so you can see the difference

#include <iostream>
#include <stdlib.h>
using namespace std;
int suma(int numero1, int numero2){
int resultado;
resultado = numero1 +numero2;
return resultado;
}
int resta(int numero1, int numero2){
int resultado;
resultado = numero1-numero2;
return resultado;
}
int multiplicacion(int numero1, int numero2){
int resultado;
resultado = numero1*numero2;
return resultado;
}
int division(int numero1, int numero2){
int resultado;
resultado=numero1/numero2;
return resultado;
}
int residuo(int numero1,int numero2){
int resultado;
resultado=numero1%numero2;
return resultado;
}
int main()
{
int num1, num2, res;
cout <<“Escribe número 1 ” <<‘\n’ ;
cin >> num1;
cout <<“Escribe numero 2 ” <<‘\n’;
cin >> num2;
res = suma(num1,num2);
cout <<“Suma = “<<res<< ‘\n’;
res = resta(num1,num2);
cout <<“Resta = “<<res<< ‘\n’;
res = multiplicacion(num1,num2);
cout <<“Producto = “<<res<<‘\n’;
res = division(num1,num2);
cout <<“Division = “<<res<<‘\n’;
res = residuo(num1,num2);
cout <<“Residuo = “<<res<<‘\n’;

}


Homework 4 sum of numbers

--Originally published at Solving problems with programming

In this homework we are going to make the sum of numbers for a range that the user gives. To do this we are going to use a loop. In the loop has to a counter an a varaible were al the numbers are added. The counter will help the loop to stablish until when the loop is going to be repeated. The loop is going to be repeated until the counter is equal to the highest number from the range of the sum.

Here is my code any question of the structure or suggestion to do it in the comments.

The code here

#include <iostream>
#include <cmath>
using namespace std;
int sumanumeros(int bajo, int alto){
int suma,contador;
suma=0;
contador=bajo;
if(bajo>alto){
cout<<“El número menor es más grande que el mayor”<<endl;
return 0;
} else{
if(bajo==alto){
cout<<“El número menor es igual al mayor”<<endl;
return 0;
} else{
do{
suma=suma+contador;
contador++;
}while(contador<=alto);
return suma;
}
}
}
int main(){
int min,max,resultado;
char op;
cout <<“Este programa sirve para hacer una suma de un rango de números”<<endl;
cout <<“Necesitas dar el número menor y mayor del rango “<<endl;
do{
cout <<“Número menor:”;
cin >> min;
cout<<endl<<“Número mayor:”;
cin>> max;
cout<<endl;
resultado= sumanumeros(min,max);
if(resultado>0);{
cout<<“Resultado: “<<resultado<<endl;
}
cout<<“¿Quiere hacer otra suma?(s/n)”<<endl;
cin>>op;
}while(op==’S’||op==’s’);
return 0;
}


Homework 3 Pick a number

--Originally published at Solving problems with programming

In this homework the user is going to guess the number that you have created, the number is created randomly. The program is going to give clues to the user by telling him if he is under or above the secret number. For that we are going to use a function that creates numbers randomly “rand()” from the library “cstdlib”. For giving the clues we just need conditional structure. And we are going to add a loop for the user to dicide if he would want to play again.

Here you can acces to the code

# include <iostream>
# include <cmath>
# include <cstdlib>
using namespace std;
int randomnumber (){
int random;
random = rand()%100+1;
return random;
}
void pistas (int numus, int random){
if (numus==random){
cout <<“¡ADIVINASTE EL NÚMERO, FELICIDADES!”<<endl;
}else{
if(numus<random){
cout<< “El número es menor”<<endl;
} else if (numus> random){
cout<< “El número es mayor”<<endl;
} else if(numus<0 && numus>100){
cout << “El número esta fuera del rango”<<endl;
}
}
}
int main (){
int num, secret, contador=0;
char op;
cout<<“El chiste de este juego es adivinar el número dentro del rango del 1 al 100″<<endl;
do{
secret=randomnumber();
contador = 0;
do{
if (contador==0) {
cout<<“Escribe un número”<<endl;
} else {
cout<<“Intenta de nuevo”<<endl;
}
cin>> num;
pistas(num,secret);
contador++;
} while (num!= secret);
cout<<“Adivinaste el número en “<<contador<<” intentos”<<endl;
cout<<“¿Quieres seguir jugando?(s/n)”;
cin>>op;
}while(op==’s’||op==’S’);
}

 


Quiz 3

--Originally published at Solving problems with programming

In this quiz what we have to do is create two functions one that calcultes the root square of a variable and the other has to calculate the cubic square of a variable. For doing that we can use the library cmath, that already has the funcion to calculate cubic and root squares. If the number that the user is giving is negative it is imposible to calculate the root square so we are going to inform the user

#include <iostream>
#include <cmath>
using namespace std;

double square_root(double x){
return sqrt(x);
}
double square_cube(double x){
return cbrt(x);
}

int main(){
double num,r2,r3;
cout<< “Ingrese el número al que quiere aplicar raíz cuadrada”<<endl;
cin >> num;
if (num<0) {
cout << “No existe raíz cuadrada negativa”<< endl;
cout << “La raíz cúbica del número es ” <<square_cube(num)<<endl;
} else {
r2= square_root(num);
std::cout << “La raíz cuadrada del número es ” << r2 <<‘\n’;
r3= square_cube(num);
std::cout << “La raíz cúbica del número es ” << r3 << ‘\n’;
}
}

 


Temperture

--Originally published at Solving problems with programming

In this homework we have to convert from Farenheit to Celcius. We are going to use the conversion that we already know, C=5*(F-32)/9. So what we have to do is create two variables one for F and one for C. Ask the user the temperture of F and with the coversion change it to Celcius. We have to add a condition if the temperture in celcius is bigger or equal to 100°, because in that case the water boils, and we want to inform the user.

#include <iostream>
using namespace std;
int main() {
double far,cel,agua;
std::cout << “Escribe la temperatura en Farenheit” << ‘\n’;
std::cin >> far;
cel = 5*(far-32)/9;
std::cout << “La temperatura en celcius es ” <<cel<<“°”<< ‘\n’;
if (cel>=100) {
std::cout << “El agua evapora a esta temperatura” << ‘\n’;
} else {
std::cout << “El agua no evopara a esta temperatura” << ‘\n’;
}
}


Fun with numbers

--Originally published at Solving problems with programming

In this homework with 2 numbers that you ask to the user you will do multiple operations, sum, substracion, division, multiplication and residue.

First you nee to declare those variables that will get the value of those two numbers, you assign the values with cin. With the values in the variables the rest is the very easy

#include <iostream>
#include <stdlib.h>
using namespace std;
int main()
{
int num1, num2, res;
cout <<“Escribe número 1 ” <<‘\n’ ;
cin >> num1;
cout <<“Escribe numero 2 ” <<‘\n’;
cin >> num2;
res = num1 + num2;
cout <<“Suma = “<<res<< ‘\n’;
res = num1 – num2;
cout <<“Resta = “<<res<< ‘\n’;
res = num1 * num2;
cout <<“Producto = “<<res<<‘\n’;
res = num1 / num2;
cout <<“Division = “<<res<<‘\n’;
res = num1 % num2;
cout <<“Residuo = “<<res<<‘\n’;

}


Hello World blog

--Originally published at Solving problems with programming

Im very late to start blogging, but better late than never, so i say helloworld to blogpost. To get started we have a very easy exercise, is the greeting to C++. We will print hello world in the terminal and to do it we need some basic elementes.

#include
using namespace std;

int main()
{
cout <<“Hello world.”<< endl; return 0;
}

#include <iostream> are the libraries that you add to your program. Libraries are a collection of  functions, and functions are a sets of code that do an specific processes. Later on other WSQ we will do our own functions.  The iostream library is the one that lets the program get information in and infromation out. The way you get information in and out is with cout and cin.

Int main() is a function, and is the function of you main program, in c++ everything works with functions and your program is function. We will understand it better later.

cout<<“Hello world.” The cout is what prints our message. the couts need << to say what is going to get out. Being that the infomatino getting out is a string we have to put it in quotes. Strins is the type of data that are letters.

<<endl; We wanto to make a space and beggin in a new line so that is infomation out, for that we have to put another <<, but not another cout, we just have to write it again just if we get information in, and is for the program to identify what is in and what is out. Being that that is the end of our statement, we have to put “;”. Semicolons go always at the end of the statements.

return 0; Finally the return statement is a rule, every funtcion needs

Continue reading "Hello World blog"

Learning how to blog and empowering twitter

--Originally published at Solving problems with programming

I have never bloged before, it is strange for me. I have a lot to learn, there is many options on wordpress that i don’t understand. And i dont event know how to erease the default mesage of WordPress that our teacher dont want us to be showing.

It will be a big effort for me do this blog, becuase y hate writting, but i will do it to practice my english, increase my vocabulary and practice my writting.

Another thing that i have trouble with, is twitter. I don’t understand twitter, I don’t imagine and know how to use it on a productive way. I have heard of being a very good source, even from Ken, but i dont see how. My mission is to learn how is productive and how to use it correctly.