Guess the number

--Originally published at my programming blog

The assignment is to create a program in which  you need to guess the number the computer was thinking, and if you get it wrong the program tells you if its too high or to small and when you get it right it tells the number of times it took you to get it right.

Here is my code:

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

int main (){
int x, secret,y=0;
srand(time(NULL));
secret=(rand()%100+1);
cout<< “I chose  a number between 1-100, can you guess it?: “<<endl;

do {
cout<<“Write your guess:”<<endl;
cin>>x;
y=y + 1;
if(x>secret){
cout<<“Wrong! ” <<x<< ” is too high, try again.”<<endl;
} else {
if (x<secret){
cout<<“Wrong! “<<x<< ” is too low, try again.”<<endl;
}
}
}while(x!=secret);
cout<<“Congratulations you got it right!!”<<endl;
cout<<“It took you “<<y<<” times to get it right.”<<endl;
return 0;
}

screen-shot-2017-01-31-at-7-40-53-amscreen-shot-2017-01-31-at-7-42-10-am

  1. I included the libraries “cstdlib” and “ctime” so that I could use the srandom function and the time function.
  2. Then I named my variables (the integers “x”, “y” and “secret”)
  3. I wrote the function of srand and inside the parenthesis I wrote time(NULL) so that the random number is always different.
  4. Then I equaled the integer “secret” to the random function “rand” and put %100 +1 to specify that I want a number between 1 and a 100.
  5. I added the cout to ask the user for a number.
  6. I made a loop called do-while, this means that while a condition is true this loop will repeat, so I did it with the condition that x!=secret, so while the user doesn’t guess the secret number this will repeat itself.
  7. Inside the loop I added an if so if x>secret the program tells you your number is too high, and another if so if
    Continue reading "Guess the number"

Temperature in Celsius

--Originally published at my programming blog

So this week my assignment was to ask the user to give me a temperature in Fahrenheit and my program would give the user the temperature in Celsius and tell him the state of water in that temperature.

I included some mastery topics, I used nested ifs, the use of ifs and elses.

It wasn’t hard, I enjoyed this assignment! I thought the use of ifs would be hard but I understood it pretty fast.

If you don’t understand the use of ifs, I’ll explain what I did, so I just put an if and inside that if I added another if and after that if an else. So first I thought okay.. if the degrees in Celsius are more or equal to a 100, water was gas, then inside that if I added if the degrees where less or equal to 0 water was  solid and if it wasn’t neither of them water was liquid.

This is my code:

screen-shot-2017-01-26-at-10-36-44-am

#include <iostream>
using namespace std;
int main()
{
int F, C;
cout<< “Do you want to convert Fahrenheit to Celsius?”<<endl;
cout<< “And know what is the state of water in that temperature?”<<endl;
cout<< “Let’s begin!”<< endl;
cout<< “What is the temperature in Fahrenheit?”<<endl;
cin>> F;
cout<< “A temperature of “<<F<< ” degrees Fahrenheit is: “<<endl;

C=5*(F-32)/9;
cout<< C << ” degrees Celsius” << endl;
if (C>=100) {
cout<< “Water is in a gas state.”<< endl;
} else {
if (C<=0) {
cout<< “Water is in a solid state.”<< endl;
} else {
cout<< “Water is in a liquid state.”<< endl;
}

}
return 0;
}

And this how it looks once the program runs:

screen-shot-2017-01-26-at-10-37-39-am


Quiz #3

--Originally published at my programming blog

Today I did the Quiz #3, at first I was lost I didn’t understand very well how to make a function, but I asked for help and my classmates and my teacher helped me.

Once you understand it it’s not that hard.

What I needed to do was ask the user for a number and the calculate the square and cube root of that number, also I needed to include what happens if the user enters a negative number.

First I included the library cmath so I could use math functions in my program, after that I created my two functions, one for the square root and the other for the cube root and I specified what the function needed to do. And then started with the ” int main ()” and all my basic program and at the end I added an “if” to give the option of when the user entered a negative number.

So this is my code:

screen-shot-2017-01-26-at-10-15-19-am

#include
#include
using namespace std;

double square_root(double x){
return pow (x, 0.5);
}
double cube_root(double x){
return pow (x, 0.333);
}

int main(){
double x;
cout<<“Let’s find the square and cube root of a number!”<<endl;
cout<< “Give me a number:”<<endl; cin>>x;
if (x<0){
cout<<“Error! The number you entered is negative.”<<endl;
} else {
cout<< “The square root is: “<<square_root(x)<<endl;
cout<< “The cube root is: “<<cube_root(x)<<endl;
return 0;
}
}

And this is the program when I run it:

screen-shot-2017-01-26-at-10-20-35-amscreen-shot-2017-01-26-at-10-19-59-am


I did it!

--Originally published at my programming blog

When the teacher explained us the homework WSQ01 I was scared, I didn’t even know how to run the “Hello World” program, but right now I’m really proud of myself! It wasn’t as hard as I thought it would be, I read all chapter 2 of the book and it explains very well, also I read a post in the webpage cplusplus.com that also helped me. So thanks to this I understood how to do the program!

screen-shot-2017-01-18-at-5-01-45-pm

So what I did was:

  1. I typed the basic things like” #include <iostream>”, “using namespace std;” and “int main ()”
  2. Then I put an integer with the values of a and b
  3. Then I typed “cout<< ” First integer: “<<endl;
  4. Then typed “cin>> a; ” (“cin” means that you insert the value you want)
  5. The I typed “cout<< “Second integer: “<< endl;
  6. Again I typed “cin>> b;” ( but this time I entered the b value)
  7. And then I just typed “cout<< “Sum of the two numbers: ” << a+b << endl; (after the string I put operation I wanted which was the sum a+b)
  8. And I did the same with the different operations (difference, division and the remainder)

screen-shot-2017-01-18-at-5-00-09-pm

Here is my program:

#include <iostream>
using namespace std;
int main()
{
int a, b;
cout << “First integer: ” << endl;
cin >> a;
cout << “Second integer: ” << endl;
cin >> b;
cout << “Sum of the two numbers: ” << a+b << endl;
cout << “Difference of the two numbers: ” << a-b<< endl;
cout << “Integer based division of the two numbers: ” << a/b << endl;
cout << “Remainder of the integer division of the two numbers: ” << a%b << endl;
return 0;
}

Thanks for reading!