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

MUST COMPILE you are to write a number guessing game. The program needs to selec

ID: 3632788 • Letter: M

Question

MUST COMPILE

you are to write a number guessing game. The program needs to select a random number between 1 and 100 and then let the user try to guess the number. After each guess the program should report whether the guess was too low, too high or correct. At the end it should report the number of guesses made by the user. Some sample code is given below. Copy this code and fill in the missing code for the 2 functions and print the number of guesses in main.

#include
#include
#include

using namespace std;

// Use random to get a random positive integer.
// Then use %100 to get a number for 0 to 99
// Last add 1 to the number to get a random number from 1 to 100
//
int random_number()
{
}

// This function accepts and judges 1 user guess
// It returns true if additional guessing is needed
bool guess ( int n )
{
int t;

// Prompt for the user to enter a number from 1 to 100

// Read the user's number into variable t

// If t is less than n, tell the user the guess is too small and return true

// If t is greater than n, tell the user the guess is too large and return true

// If you get here the guess is right.
// Tell the user congratulations and return false.

}

int main()
{
int n;
int i;

srand(time(NULL)); // Seed the random number generator with the time.
rand(); // Ignore the first random number which is not
// random enough.

n = random_number(); // Get a random number from 1 to 100.

i = 1;
while ( guess( n ) ) {
i++;
}

// Tell the user how many guesses were required.

return 0;
}

Explanation / Answer

#include<iosteam>
#include<time.h>
#include<string>

using namespace std;

// Use random to get a random positive integer.
// Then use %100 to get a number for 0 to 99
// Last add 1 to the number to get a random number from 1 to 100
//
int random_number()
{
int n = rand()%100 + 1;
return n;
}

// This function accepts and judges 1 user guess
// It returns true if additional guessing is needed
bool guess ( int n )
{
int t;

cout << " enter your guess ";
cin >> t;

if(t < n)
cout << " your guess too small " << endl;
return true;

if(t > n)
cout << " your guess too large " << endl;
return true;

if(t ==n )
cout << " congrats u guessed number " << endl;
return false;


}

int main()
{
int n;
int i;

srand(time(NULL)); // Seed the random number generator with the time.
rand(); // Ignore the first random number which is not
// random enough.

n = random_number(); // Get a random number from 1 to 100.

i = 1;
while ( guess( n ) ) {
i++;
}

cout << " no of guesses required is " << i << endl;

return 0;
}

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