Link of the picture: Link of the picture

So in this sixth class that I had on last Monday I started how to make a program with loops in C++.#Mastery13

What I did for this numeric program is solving the problem to the user by writing a program that picks a random integer in the range of 1 to 100.

There are different ways to make that happen , I chose one method that best works for me.

After that, inside  the program prompts the user for a guess of the value, with hints of ‘too high’ or ‘too low’ from the program.

The program continues to run until the user guesses the integer. I did something extra here including telling the user how many guesses the user had to make to get the right answer.

I checked that my program doesn’t always use the same random number chosen by splitting my problem solving into parts.

At first I only generated the random number and printed that as a first step.

The resources I need it to solve it are here:

Random number link 1

Random number link 2

The following photograph shows the solution to this problem:

wsq3

Picture of the author.

So at first I wrote the same structure of the program just did the same as what i did in Hello World: Second Class, Second Blog (Blog of the second class 12/01/17) and Hello World.cpp,  #WSQ01 Post Fun with Numbers 16/01/17 and WSQ1.cpp and #WSQ02 Post Temperature 23/01/17 and WSQ02.cpp where i explained the application of the #MasteryTopic01 that it is for comments that could be very useful when debugging and #MasteryTopic04 that is basic output for data. Also I did some other mastery topics.

What i first put in the code was the library to call all the fuctions of inputs and outputs of data in languague C++ #MasteryTopic06 Calling Functions and #MasteryTopic08 Importing and using libraries.

What i then put in the code was the library in C++ of   to call all the fuctions of random numbers of data in languague C++ #MasteryTopic06 Calling Functions and #MasteryTopic08 Importing and using libraries.

What i then put in the code was the library in C++ of   to call all the fuctions of time of data in languague C++ #MasteryTopic06 Calling Functions and #MasteryTopic08 Importing and using libraries. I used it for the program that doesn’t always use the same random number.

wsq3-v2

Then i had to write a command that everytime goes with the instructions of input and outputs of data that is call std but with this command helps writing these std out of the main because the machine factorizes them.

Next I initialize the begin of my program int main()

After that i had to declarate just one basic type of variable int variables iSecret, iGuess and guesses initialized in 0. #Mastery03 #MasteryTopic03  because the program will show the value in an int way in the secret number.

Next I Initialize the secret number that i have to make first with the random seed, the «seed» is a starting point for the sequence and the guarantee is that if you start from the same seed you will get the same sequence of numbers.

srand((int)time(0));

After that I generate secret number between 1 and 100 and the value i saved in the variable iSecret:

iSecret = (rand() % 100) + 1;

Next we use the #Mastery13. Use of loops with «while» and «do while». This command is the loop do-while that allows you in doing at least once of the instruccions in this loop and while this case of iSecret don´t be equal than iGuess, it will execute forever every instructions of the loop label in this loop.


do{

cout<<"I have a secret number chosen between 1 and 100."<<endl; //#Mastery04 command of out data in form of text
cout<<"Guess the number (1 to 100): ";//#Mastery04 command of out data in form of text cin>>iGuess; //#Mastery05 This command allows enter data for inputs. In this case this enters the
//value of variable 'iGuess'
guesses++; //This command is the process of guesses=guesses+1 where in this i am using as a counter
//of how many guesses the user has try in guess the number.

if(iSecret<iGuess) //#Mastery10 use of the if statement //This command allows the condition if the value of iSecret
//is lower of the value of iGuess and when this condition will be true, the tasks inside of the statement will execute.
// In this case if the value iSecret is lower than iGuess this will occur:
{
cout<<"I'm sorry but "<<iGuess<<" is too high,try again"<<endl; //command of out data in form of text //In this text we show the authentic output of the value entered of iGuess in the terminal interface #MasteryTopic04 } if(iSecret>iGuess) //#Mastery10 use of the if statement //This command allows the condition if the value of iSecret
//is higher of iGuess and when this condition will be true, the tasks inside of the statement will execute.
// In this case if the value iSecret is higher than iGuess this will occur:
{
cout<<"I'm sorry but "<<iGuess<<" is too low,try again"<<endl; //command of out data in form of text
//In this text we show the authentic output of the value entered of iGuess in the terminal interface #MasteryTopic04
}

}while(iSecret!=iGuess);

After that we use #Mastery04 command of out data in form of text to tell the user that i have a secret number chosen between 1 and 100 and tell the user to guess the number 1 to 100:

cout<<"I have a secret number chosen between 1 and 100."<<endl; //#Mastery04 command of out data in form of text
cout<<"Guess the number (1 to 100): "; 

Next I use the #Mastery05. This command allows enter data for inputs. In this case this enters the value of variable ‘iGuess’

 cin>>iGuess;

After that, this command is the process of guesses=guesses+1 where in this i am using as a counter of how many guesses the user has try in guess the number.

guesses++;

Next I need the #Mastery10 use of the if statement. This command allows the condition if the value of iSecret is lower of the value of iGuess and when this condition will be true, the tasks inside of the statement will execute. In this case if the value iSecret is lower than iGuess this will occur:

if(iSecret<iGuess) //#Mastery10 use of the if statement //This command allows the condition if the value of iSecret
//is lower of the value of iGuess and when this condition will be true, the tasks inside of the statement will execute.
// In this case if the value iSecret is lower than iGuess this will occur:
{
cout<<"I'm sorry but "<<iGuess<<" is too high,try again"<<endl; //command of out data in form of text
//In this text we show the authentic output of the value entered of iGuess in the terminal interface #MasteryTopic04

}

After that, Inside the if i have a command of out data in form of text to tell the user that the SECRET number is too high, and the user needs to try again. In this text we show the authentic output of the value entered of iGuess in the terminal interface #MasteryTopic04

cout<<"I'm sorry but "<<iGuess<<" is too high,try again"<<endl; 

After that I need to use the #Mastery10 use of the if statement. This command allows the condition if the value of iSecret is higher of iGuess and when this condition will be true, the tasks inside of the statement will execute. In this case if the value iSecret is higher than iGuess this will occur:

 if(iSecret>iGuess) //#Mastery10 use of the if statement //This command allows the condition if the value of iSecret
//is higher of iGuess and when this condition will be true, the tasks inside of the statement will execute.
// In this case if the value iSecret is higher than iGuess this will occur:
{
cout<<"I'm sorry but "<<iGuess<<" is too low,try again"<<endl; //command of out data in form of text
//In this text we show the authentic output of the value entered of iGuess in the terminal interface #MasteryTopic04
}

After that inside of the command of out data in form of text to tell the user that the SECRET number is too high, and the user needs to try again.In this text we show the authentic output of the value entered of iGuess in the terminal interface #MasteryTopic04

cout<<"I'm sorry but "<<iGuess<<" is too low,try again"<<endl;

After that we have the end of the loop do-while. This command is the loop do-while that allows in doing at least 1 time of the instructions written in this loop and while in this case. The value of iSecret don’t be equal to the value of iGuess, it will execute forever all of the instrucccions lable in this loop.

}while(iSecret!=iGuess);

Next I have a command of out data in form of text. In this text we show the authentic output of the value entered of iGuess in the terminal interface #MasteryTopic04 and we tell the user that he is right.

cout<<"You got it!!!!. The right answer is indeed "<<iGuess<<endl;

After that we need to use a command of out data in form of text. In this text we show the authentic output of the value of guesses in the terminal interface #MasteryTopic04 to tell the user how many guesses tried in saying the secret number.

cout<<"You made "<< guesses << " guesses to get the right number."<<endl;

Finally we have a command that allows labeling the final of the function main ()

return 0;

The following pictures shows the code of the program:

wsq3v3

wsq3v4

wsq3v5

wsq3v6

And i also resolved it by doing this video tutorial that i made reafirming these parts at this link :#WSQ03 Fourth Tutorial PICK A NUMBER 23/01/17

Finally with the command ./a. out tells the terminal to run de C++ file:

wsq3

This code will be here and in Github:

Link in Github:Link in Github

If you do not want the #WSQ3.cpp there is it in here with comments. The comments are very useful to detect errors and create new things #Mastery01 #Masterytopic01 #MasteryTopic01 #MASTERY01 #MASTERYTOPIC01 :

Also in the last class i learned how to present my C++ in a better ‘readable’ way after doing it #Mastery02 #Masterytopic02 #MasteryTopic02 #MASTERY02 #MASTERYTOPIC02:

#include <iostream>//Library to call all the
//fuctions of inputs and outputs of data in languague
//C++ #MasteryTopic01 and #Mastery08 (Importing and using libraries)

#include <cstdlib> //Library to call all the
//fuctions of random numbers in languague
//C++ #MasteryTopic01 and #Mastery08 (Importing and using libraries)

#include <ctime> //Library to call all the
//fuctions of time in languague
//C++ #MasteryTopic01 and #Mastery08 (Importing and using libraries)

using namespace std; //In C++ we need a command
// that everytime goes with the instructions of input and outputs
//of data that is call std but with this command helps writing
//these std out of the main because the machine factorizes them

int main () //Begin of the program
{

int iSecret, iGuess,guesses=0; //Declarate int variables iSecret, iGuess and guesses initialized in 0

srand((int)time(0)); //Initialize random seed, the "seed" is a starting point for
//the sequence and the guarantee is that if you start from the same seed you will
//get the same sequence of numbers.

iSecret = (rand() % 100) + 1; /* generate secret number between 1 and 100: */

//Next we use the #Mastery13 Use of loops with "while" and "do while"
// This command is the loop do-while that allows you in doing at least once of
//the instruccions in this loop and while this case of
//iSecret don´t be equal than iGuess, it will execute forever every instructions of the loop
//label in this loop.
do{

cout<<"I have a secret number chosen between 1 and 100."<<endl; //#Mastery04 command of out data in form of text
cout<<"Guess the number (1 to 100): ";//#Mastery04 command of out data in form of text cin>>iGuess; //#Mastery05 This command allows enter data for inputs. In this case this enters the
//value of variable 'iGuess'
guesses++; //This command is the process of guesses=guesses+1 where in this i am using as a counter
//of how many guesses the user has try in guess the number.

if(iSecret<iGuess) //#Mastery10 use of the if statement //This command allows the condition if the value of iSecret
//is lower of the value of iGuess and when this condition will be true, the tasks inside of the statement will execute.
// In this case if the value iSecret is lower than iGuess this will occur:
{
cout<<"I'm sorry but "<<iGuess<<" is too high,try again"<<endl; //command of out data in form of text //In this text we show the authentic output of the value entered of iGuess in the terminal interface #MasteryTopic04 } if(iSecret>iGuess) //#Mastery10 use of the if statement //This command allows the condition if the value of iSecret
//is higher of iGuess and when this condition will be true, the tasks inside of the statement will execute.
// In this case if the value iSecret is higher than iGuess this will occur:
{
cout<<"I'm sorry but "<<iGuess<<" is too low,try again"<<endl; //command of out data in form of text
//In this text we show the authentic output of the value entered of iGuess in the terminal interface #MasteryTopic04
}

}while(iSecret!=iGuess); // This command is the loop do-while that allows in doing
//at least 1 time of the instructions written in this loop and while in this case
//The value of iSecret don't be equal to the value of iGuess, it will execute forever
//all of the instrucccions lable in this loop.

cout<<"You got it!!!!. The right answer is indeed "<<iGuess<<endl; //command of out data in form of text
//In this text we show the authentic output of the value entered of iGuess in the terminal interface #MasteryTopic04
cout<<"You made "<< guesses << " guesses to get the right number."<<endl; //command of out data in form of text
//In this text we show the authentic output of the value of guesses in the terminal interface #MasteryTopic04

return 0; // This command allows label the final of the function main ()

}
Anuncio publicitario

13 comentarios en “#WSQ03 Post Pick a Number 23/01/17 and WSQ03.cpp

Deja una respuesta

Introduce tus datos o haz clic en un icono para iniciar sesión:

Logo de WordPress.com

Estás comentando usando tu cuenta de WordPress.com. Salir /  Cambiar )

Imagen de Twitter

Estás comentando usando tu cuenta de Twitter. Salir /  Cambiar )

Foto de Facebook

Estás comentando usando tu cuenta de Facebook. Salir /  Cambiar )

Conectando a %s