Out of Numbers

--Originally published at PZ

This was a very easy program because we had to use the different things that we have seen la de loops and that things with simple functions like sum, product, etc.

This helped me a lot to understand the use of the ints, the structure that they must have and the way they work because. To be honest I didnt search anywhere because I just used the information that Ken gave us, principally the structure of the int and then I add the different variables.

include <iostream>
using namespace std;
int sum(int base, int limit);
int resta(int base, int limit);
int mul(int base, int limit);
int div(int base, int limit);
int rem(int base, int limit);
int main()
{
int a, b, x;
cout << “Please give the first number “;
cin >> a;
cout << “Please give me the second number”;
cin >> b;
cout <<“The sum of “<<a<<” + “<<b<<” is:”<< sum(a, b)<<endl;
cout <<“The substraction of “<<a<<” – “<<b<<” is:”<< resta(a, b)<<endl;
cout <<“The product of “<<a<<” * “<<b<<” is:”<< mul(a, b)<<endl;

cout <<“The division of “<<a<<” / “<<b<<” is:”<< div(a, b)<<endl;
cout <<“The reminder of “<<a<<” / “<<b<<” is:”<< rem(a, b)<<endl;

 

return 0;
}

int sum(int a, int b)
{
int x ;
x=a+b;
return x;
}

int resta(int a, int b)
{
int x ;
x=a-b;
return x;
}

int mul(int a, int b)
{
int x ;
x=a*b;
return x;
}

int div(int a, int b)
{
int x ;
x=a/b;
return x;
}

int rem(int a, int b)
{
int x ;
x=a%b;
return x;
}

 

 


Sum of numbers

--Originally published at PZ

For me this was much more difficult than the last programs because I used loop like For and return, I also used int to create a inclusive sum between the to numbers, for me this was the most difficult becuase I didnt know how te say to the computer to do an inclusive sum between the numbers. I also searched in a lot of blogs but I finally found one that helped me a lot, not also for the code also because in the discussion of the blog the people where sharing pages to understand how to make loops.

#include <iostream>
using namespace std;
int sum(int base, int limit);
int main()
{
int a, b;
cout << “Please give us the lower bound “;
cin >> a;
cout << “Please give us the upper bound:”;
cin >> b;
cout <<“The sum from “<<a<<” to “<<b<<” is:”<< sum(a, b)<<endl;
return 0;
}

int sum(int base, int limit)
{
int sum = 0;
for (int i = base; i <= limit; i++){
sum += i;
}
return sum;
}

Source:http://www.dreamincode.net/forums/topic/239277-sum-of-all-numbers-in-a-range-c-primer-problem/


Random Number

--Originally published at PZ

This was much more challenging work thant the last ones becuase I used a lot of variables and libraries that I didnt know like time, srand and rand. So I started to research in the internet and I found this page: http://www.cplusplus.com/reference/cstdlib/rand/, which helps me a lote because I used to understand the variables that I used, then I added iostream and std so I replaced all the scan and print to cin and cout, so this become much easier for me. Finally I ran the game and I saw that this actually works and its very funny to play with it.

#include <iostream>

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
using namespace std;

int main ()
{
int a, b;

 

srand (time(NULL));
a = rand() % 100 + 1;

do {
cout<< “Guess the number (1 to 100): “<<endl;
cin>>b;
if (a<b) cout<< “The secret number is lower”<<endl;
else if (a>b) cout<< “The secret number is higher”<<endl;
} while (a!=b);

cout<<“Congratulations”<<endl;
return 0;
}

 

Links

Cplusplus, function rand: http://www.cplusplus.com/reference/cstdlib/rand/


Temperature

--Originally published at PZ

To do this work I have readed part of the book and reviewed the week 01 work to do this, because they booth use operations so it was much more easier to do this. I also had to review the cin and cout to take the information, when I completed that I had this part of the code:

#include <iostream>
#include <stdlib.h>
using namespace std;

int main(){

int f,c=0;

cout<<” ingrese temperatura en Fahrenheit:”<<endl;

cin>>f;

c=5*(f-32)/9;

 

cout<<“La temperatura en Celsius es:”<<c<<” Celsius.”<<endl;

return 0;

 

}

But that was just one part because I had to program it to say if it is boiling or not so I searched for the If and Else in this page https://www.tutorialspoint.com/cplusplus/cpp_if_else_statement.htm.

And then I completed this code:

#include <iostream>#include <iostream>#include <stdlib.h> using namespace std;

int main(){
int f,c=0;
cout<<” ingrese temperatura en Fahrenheit:”<<endl;
cin>>f;
c=5*(f-32)/9;

cout<<“La temperatura en Celsius es:”<<c<<” Celsius.”<<endl; if(c>=100) cout<<“La temperatura evapora a este punto.”<<endl;else cout<<“La temperatura no evapora a este punto.”<<endl;
return 0;

}

Links

Turorials Piont, If and else;

https://www.tutorialspoint.com/cplusplus/cpp_if_else_statement.htm.

 


Hello World

--Originally published at PZ

So this is my first post in the blog, and I would like to start writing about this exciting first week in Ken class. I think that this is the subject that I most excited to take, at least, during this first semester, because I would like to learn a lot of stuff about programming also because I have really liked the way that Ken introduced us to the class and the topics that he show us, for example, I enjoyed a lot doing my first homework and I really liked that he works with pages like Lynda ( that I didn’t know before). Finally, I would mention that I feel excited about the method that Ken uses in this class in which we have to be responsible to watch the videos, watch Lynda and read the book to approach the goal of this class and be ready for the exams.                                                                                                It was so exciting for me to make Hello Worl which is a very simple program, but that means much more to me because it was my first experience in C++ language, with Atom, bash, etc. And that motivated me to continue learning.

 

Code:

#include <iostream>#include <iostream>using namespace std;
int main() { cout << “Hello World” << endl; return 0;

}


Fun with Numbers

--Originally published at PZ

This was a little bit difficult than Hello World because in this code I had to search in differents blogs and in Youtube about how to do operations like sum, rest, division and multiplication. I searched in Google and I found this blog http://programa-c.blogspot.mx/2010/04/codigos-c-suma-dos-numeros-enteros.html which help me a lot, becuase I used it like the base an then I added product and division.

#include <iostream>
#include <stdlib.h>
using namespace std;

int main(){

int a,b,suma,resta,producto,division=0;

cout<<“\n ingrese numero a:”;

cin>>a;

cout<<“\n ingrese numero b:”;

cin>>b;

suma=a+b;
resta=a-b;
producto=a*b;
division=a/b;

cout<<“\n la suma de “<<a<<“+”<<b<<” es:”<<suma;
cout<<“\n la resta de “<<a<<“-“<<b<<” es:”<<resta;
cout<<“\n el producto de “<<a<<“*”<<b<<” es:”<<producto;
cout<<“\n la division de “<<a<<“/”<<b<<” es:”<<division;

return 0;

 

}

Links

Programa C, Suma de dos números enteros:

http://programa-c.blogspot.mx/2010/04/codigos-c-suma-dos-numeros-enteros.html