Question

In: Computer Science

Write a program that generates a random number between 1 and 50 and asks the user...

Write a program that generates a random number between 1 and 50 and asks the user to guess it. As a hint, it tells the user how many divisors it has, (excluding 1 and the number itself). Each time the user makes a wrong guess, it displays "Wrong" and says if the guess was too low or too high, until the user gets it right, in which case it says "Right" and says how many trials it took to guess it. Then, the program asks the user if he or she wants to continue. If the answer is 'y' or 'Y', it generates another number between 1 and 50 and repeats the game. If not, it prints the average of all trials with one decimal place and ends.

Example:

I've picked a number between 1 and 50 that has 0 divisor(s).

Guess the number: 23

Too low!

Guess the number: 37

Too low!

Guess the number: 43

Too high!

Guess the number: 41

Right! It took you 4 trials.

Play again? [y/n]: y

I've picked a number between 1 and 50 that has 1 divisor(s).

Guess the number: 25

Too low!

Guess the number: 49

Right! It took you 2 trials.

Play again? [y/n]: y

I've picked a number between 1 and 50 that has 3 divisor(s).

Guess the number: 30

Too high!

Guess the number: 24

Right! It took you 2 trials.

Play again? [y/n]: n

You averaged 2.7 trials in 3 games.

Press any key to continue.

Im stuck with the loops and if you can explain the steps would be great. At the end I need to show the average in where the user guessed the correct number and how many games they played

Solutions

Expert Solution

#include <cstdlib>

#include <ctime>

#include <iostream>

using namespace std;

int main(){

int game_count=0;

int total_trials=0;

while(true){

srand((unsigned)time(0));

int rnd=(rand() % 50) + 1;

//cout<<rnd<<endl; //it printed the getting random number

int divisor_count=0;

for(int i=2;i<rnd-1;i++){

if(rnd%i==0){

divisor_count=divisor_count+1;

}

}

cout<<"I've picked a number between 1 and 50 that has "<<divisor_count<<" divisor(s)."<<endl;

int n,n_count=1;

while(true){

cout<<"Guess the number:";

cin>>n;

total_trials=total_trials+1;

n_count=n_count+1;

if(n==rnd){

cout<<"Right! it took you "<<n_count<<" trials."<<endl;

game_count=game_count+1;

break;

}

if(n<rnd){

cout<<"Too low!"<<endl;

}else if(n>rnd){

cout<<"Too high!"<<endl;

}

}

char c;

cout<<"Play again?[y/n]:";

cin>>c;

if(c=='n'){

break;

}

}

cout<<"You average "<<(float)total_trials/game_count <<" trials in "<<game_count<<" games."<<endl;

return 0;

}

#if you have any doubt or more information needed comment below.i will respond as possible as soon..thanks...


Related Solutions

Write a program that generates a random number between 1 and 100 and asks the user...
Write a program that generates a random number between 1 and 100 and asks the user to guess what the number is. If the user’s guess is higher than the random number, the program should display “Too high, try again.” If the user’s guess is lower than the random number, the program should display “Too low, try again.” The program should use a loop that repeats until the user correctly guesses the random number. Your program should also keep a...
In Java: Write a program that generates a random number and asks the user to guess...
In Java: Write a program that generates a random number and asks the user to guess the number and keeps track of how many guesses it took If the user input is negative or zero then the loop must stop reading further inputs and display how many guesses they used If they guess the correct number display a message telling them they got it and exit the program If they guess the wrong number (but still a legal guess) you...
17. Write a program that generates a random number and asks the user to guess what...
17. Write a program that generates a random number and asks the user to guess what the number is. If the user’s guess is higher than the random number, the program should display “Too high, try again.” If the user’s guess is lower than the random number, the program should display “Too low, try again.” The program should use a loop that repeats until the user correctly guesses the random number. 18. Enhance the program that you wrote for Programming...
C++. Write a program that generates a random number between 5 and 20 and asks the...
C++. Write a program that generates a random number between 5 and 20 and asks the user to guess what the number is. If the user’s guess is higher than the random number, the program should display Too high. Try again. If the user’s guess is lower than the random number, the program should display Too low, Try again. The program should use a loop that repeats while keeping a count of the number of guesses the user makes until...
Here is my assignment: Write a program that generates a random number between 1 and 50...
Here is my assignment: Write a program that generates a random number between 1 and 50 and asks the user to guess it. As a hint, it tells the user how many divisors it has, (excluding 1 and the number itself). Each time the user makes a wrong guess, it displays "Wrong" and says if the guess was too low or too high, until the user gets it right, in which case it says "Right" and says how many trials...
Random Number Guessing Game Write a program in C++ that generates a random number between 1...
Random Number Guessing Game Write a program in C++ that generates a random number between 1 and 100 and asks the user to guess what the number is. If the user’s guess is higher than the random number, the program should display “Too high. Try again.” If the user’s guess is lower than the random number, the program should display “Too low. Try again.” The program should use a loop that repeats until the user correctly guesses the random number....
Write a program using a Scanner that asks the user for a number n between 1...
Write a program using a Scanner that asks the user for a number n between 1 and 9 (inclusive). The program prints a triangle with 2n - 1 rows. The first row contains only the square of 1, and it is right-justified. The second row contains the square of 2 followed by the square of 1, and is right justified. Subsequent rows include the squares of 3, 2, and 1, and then 4, 3, 2 and 1, and so forth...
Write a C program that asks the user to guess a number between 1 and 15(1...
Write a C program that asks the user to guess a number between 1 and 15(1 and 15 are included). The user is given three trials. This is what I have so far. /* Nick Chioma COP 3223 - HW_2 */ #include <iostream> #include <time.h> // needed for time function #include <stdlib.h> // needed for srand, rand functions int main () {       int numbertoguess, num, correct, attempts;       srand(time(NULL)); //this initializes the random seed, making a new...
Write a program in C++ coding that generates a random number between 1 and 500 and...
Write a program in C++ coding that generates a random number between 1 and 500 and asks the user to guess what the number is. If the user’s guess is higher than the random number, the program should display “Too high, try again.” If the user’s guess is lower than the random number, the program should display “Too low, try again.” The program should use a loop that repeats until the user correctly guesses the random number. Count the number...
In C#, write a console-based program that asks a user to enter a number between 1...
In C#, write a console-based program that asks a user to enter a number between 1 and 10. Check if the number is between the range and if not ask the user to enter again or quit. Store the values entered in an array. Write two methods. Method1 called displayByVal should have a parameter defined where you pass the value of an array element into the method and display it. Method2 called displayByRef should have a parameter defined where you...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT