WSQ05 – On To Functions

--Originally published at Programming the city

Hello guys, as usual, this week I created a code like the first one I did(FUN WITH NUMBERS) but with functions.

It was kind of easy but I had a little obstacle when making the division, I named the variable div, but Cygwin told me there was an error there. The solution was, changed the name of the variable.

So here is the code….


wsq05-1wsq05-2


 

And the result is…..

wsq05-3There you go.


Quiz 04

--Originally published at Programming the city

In this quiz, I was very confused because of the functions we had to use, but anyways, with the help of the teacher and my classmates, I could finally do it.

The code…


#include <iostream>
using namespace std;

int minimumThree(int x, int y, int z)
{
int num;
num=min(x,min(y,z));
}
int sumSquares(int x, int y, int z){
int sumsqr;
sumsqr=(x*x)+(y*y)+(z*z);
}

int main ()
{
int x,y,z,num, sumsqr;

cout<<“Give me three numbers, I’ll show you the minimum of the three and also the sum of the squares”<<endl;
cin>>x;
cin>>y;
cin>>z;

num=minimumThree(x,y,z);
sumsqr=sumSquares(x,y,z);

cout<<“The minimum number is: “<<num<<“, and the sum of the squares is: “<<sumsqr;

}


 

And the result is….

quiz04

#TC1017

#Quiz04


WSQ 04, Sum of numbers

--Originally published at Programming the city

Hello guys, I’ve been programming this whole week and I realized you need to practice to become a master in code programming.

In this code, I´ll show you how I can sum the range of the numbers that are between the numbers the user will type.

This is a simple code, but without an erros, whatever the user types, it has an answer.


#include <iostream>
using namespace std;

int main(){
int upper, lower, sum=0, contador, contador2=0;

cout<<“I will calculate the sum of the range between the numbers you will give me”<<endl;

cout<<“Give me the lower bound “;
cin>>lower;
contador=lower*1;

cout<<“Give me the upper bound “;
cin>>upper;
contador2=upper*1;
if(lower<upper){
do{
lower=lower+1;
sum=sum+lower;
}while(lower<upper);
cout<<“The sum from “<<contador<<” to “<<upper<<” is =”<<sum+contador;
}
else{
if(lower>upper){
cout<<“You gave me the numbers in the incorrect order, but don´t worry, I´ll fix that”<<endl;
do{
upper=upper+1;
sum=sum+upper;
}while(upper<lower);
cout<<“The sum from “<<contador2<<” to “<<lower<<” is =”<<sum+contador2;
}
else{
if(lower==upper){
cout<<“You gave me the same number! What’s happening to you?”;
}
}
}

return 0;


As you can see, I put only one library, but many loops and if’s.

wsq04

 

Now you are able to see a beautiful code.

#TC1017

#WSQ04

 


WSQ 03, Guess the number!

--Originally published at Programming the city

Hello there, in this program we had to guess a random number that the computer should choose between 1 and 100.

It took me a long time to program this, but finally, with many help of my good friends and reasoning the code, I finished.

The first thing you got to do is add the libraries, then type the variables and the function that helps you to do the random thing. See in my code that I named a variable contador, this variable helps me to let the user take only 10 opportunities to guess the number.

After all, you have to write a do while so you can keep asking the user as many times as you want.

I really let you think about my code. I hope you understand it and if it helps you to write your own code, it would be awesome.


#include<cstdlib>
#include<ctime>
using namespace std;

int main()
{

int num, guess, contador=10, contador2;
cout<<“I have a number chosen between 1 and 100″<<endl;
srand(time(NULL));
num=rand()%(100);
do{

cout<<“Please guess a number between 1 and 100: “;
cin>>guess;

contador=contador-1;
contador2=10-contador;
cout<<num<<endl;

if(guess>num){
cout<<“I’m sorry but “<<guess<<” is to high, try again you have “<<contador<<” more opportunities”<<endl;
}
else {
if(guess==num){
cout<<“You guessed the number, CONGRATULATIONS! it took you “<<contador2<<” times to get it”<<endl;
}
else{
if(guess<num){
cout<<“I’m sorry but “<<guess<<” is to low, try again you have “<<contador<<” more opportunities”<<endl;
}
}
}
}while (contador>0 && guess!=num);

cout<<“I guess you already lost all your opportunities, GAME OVER”;
return 0;
}


 

And this is the result…

 

wsq03

#TC1017

#WSQ03


WSQ 02

--Originally published at Programming the city

This program was  easier than the others, I liked it because it helps you to practice and develope new skills.

Here is the code…


#include
#include using namespace std;
int main () {
double fa, ce;

cout<<“Give a temperature in Fahrenheit “; cin>>fa;

ce=5*(fa-32)/9;

if (ce>=100)
cout<<“The conversion from Fahrenheit to Celsius of the number you just gave me is “<<ce<<” at that temperature, water boils”;
else
{
cout<<“The conversion from Fahrenheit to Celsius of the number you just gave me is “<<ce<<” at that temperature, water doesn’t boil”;
}
return 0;
}


wsq02

#TC1017

#WSQ02


Quiz 03

--Originally published at Programming the city

Today I did the quiz number 3, it was a little bit difficult at the begining but with the help of my classmates and with teacher, it became easier.

First I added the two libraries, one of these is necessary for you to do the square root of a number and also all math operations.

Secondly , I added the functions of the square and the cube root with the commands I will show you.

And finally there is the whole code.

———————————————————-

#include <iostream>
#include <math.h>

using namespace std;
double square_root(double num) {
num=sqrt(num);
return num;
} ;
double cube_root(double num) {
num=cbrt(num);
return num;
} ;
int main()
{
double num;

cout<<“Give me a number, please “;
cin>>num;
if(num>=0){
cout<<“The square root of “<<num<<” is “<<square_root(num)<<endl;
}
else{
cout<<“You can’t get the square root of a number if it is negative, sorry”<<endl;
}

cout<<“The cube root of “<<num<<” is “<<cube_root(num)<<endl;

return 0;
}


quiz03

And there is the program……..

#TC1017

#Quiz03

 


WSQ 01 Fun with numbers!

--Originally published at Programming the city

Hello people, today I want to show you a code where the user types 2 integer numbers and the program makes the sum, difference, product, division and the reminder of the division between both numbers.

Here is the code:


#include <iostream>
using namespace std;

int main()
{

int num1, num2, sum, difference, product, division, reminder;

cout<<“Give me a number “;
cin>>num1;

cout<<“Give me one more number “;
cin>>num2;

sum=num1+num2;
difference=num1-num2;
product=num1*num2;
division=num1/num2;
reminder=num1%num2;
cout<<“The sum of the numbers you just gave me is “<<num1<<“+”<<num2<<“=”<<sum<<endl;

cout<<“The difference between the numbers you just gave me is “<<num1<<“-“<<num2<<“=”<<difference<<endl;

cout<<“The product of the numbers you just gave me is “<<num1<<“x”<<num2<<“=”<<product<<endl;

cout<<“The division of the numbers you just gave me is “<<num1<<“/”<<num2<<“=”<<division<<endl;

cout<<“The reminder of the integer division is “<<reminder<<endl;
return 0;
}


As you can see, I added the int variables for each math operation, you have to know that the name of the variables is up to you.

Then, you have to input everything you need to, for example the variable sum, you just have to type the two variables and between them, type +, and automatically it will make the sum. For the other operations, is the same path, but with different symbols(-,*,/,%).

Here is the evidence.

wsq01

#WSQ 01

#TC1017


1. First Code: “Hello World”

--Originally published at Programming the city

In this blog, we will be programming in the text editor Atom, then we will save our files as cpp, for example: helloworld.cpp. After that we will run the program in Cygwin(program), this is a tool collection that help us with executable codes. 

Today I will show you the text or the code of my first program, Hello World.


#include
using namespace std;

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


helloworld2

As you can see, I’m only printing the word Hello World, just to know how Atom and Cygwin work.

Here is the code running.

helloworld

And there it is.

First we select the library, then we type g++ and the File name and at the end, we type ./a.exe.

#TC1017

#WSQ01