Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

3-It will be important to understand how random numbers are generated using the

ID: 3703111 • Letter: 3

Question

3-It will be important to understand how random numbers are generated using the rand) function. You will also need to have a basic understanding of a while or for loop You are to write a program in which the user tries to guess which number (from 0 to 99, inclusive) that the program generates, based on the rand() function. First the program asks the user to guess which number will be generated, and if the user is correct, it prints "Good guess!" and if the user is incorrect, it prints "Sorry too high or too low (depend of the case), try again!" Some other specifications The program should repeatedly ask the user for a guess and generates the outputs "Number picked: "followed by the number, and the statements as to whether it was correct or not, too high or too low If the user enters a number of O or less, the program should print "Goodbye" and exit. 1. 2. the user enters a number greater than 99, the program should output "Number too high, guess againl" and then ask for another number. 4 When the answer is correct the program must generate a new random number 99 and ask for a new guess.

Explanation / Answer

#include<iostream>

#include<time.h>

#include<stdlib.h>

using namespace std;

int main(){

srand(time(NULL)); //initialize random seed

int correctGuess=1;

while(1){

int guess,random;

if(correctGuess){ //if guess is correct generate new random number

random = rand()%99 +1; //random number between 1 to 99

correctGuess=0;

cout<<"New random number generated"<<endl;

}

cout<<"Enter guess: ";

cin>>guess;

cout<<"Number entered: "<<guess<<endl;

if(guess<=0){ //if guess is 0 or less, exit

cout<<"Goodbye"<<endl;

return 0;

}

else if(guess > 99){

cout<<"Number too high. Guess again."<<endl;

}

else if(guess == random){ //if guess is correct

cout<<"Correct guess"<<endl;

correctGuess=1;

}

else if(guess<random){ //if guess is smaller than random number

cout<<"Sorry too low.Try again."<<endl;

}

else if(guess>random){ //if guess is greater than random number

cout<<"Sorry too high.Try again."<<endl;

}

}

}

Sample running :

New random number generated

Enter guess: 70

Number entered: 70

Sorry too low.Try again.

Enter guess: 71

Number entered: 71

Sorry too low.Try again.

Enter guess: 75

Number entered: 75

Sorry too high.Try again.

Enter guess: 72

Number entered: 72

Correct guess

New random number generated

Enter guess: 60

Number entered: 60

Sorry too low.Try again.

Enter guess: 100

Number entered: 100

Number too high. Guess again.

Enter guess: 0

Number entered: 0

Goodbye

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote