Question

In: Computer Science

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 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.

I've got the basic code, but i'm stuck on if I answer 'Y' for it to repeat the game and how to code the hint (the divisor)

Example:

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

My work so far:

#include <iostream>
#include <cstdlib> //needed to use srand and rand
#include <ctime> //needed to use time
using namespace std;

int main()
{
   int random_number, guess, number_of_trys;
   char go_again;
   bool win = false;
   unsigned seed;

   seed = time(0);
   srand(seed);

   random_number = 1 + rand() % 50;
   number_of_trys = 0;

   cout << "I'have picked a number between 1 and 50. Guess the Number!" << endl;
   cin >> guess;
   cout << random_number;


   do // this will loop while play-again
   {
       while (!win)
       {
           if (guess < random_number) //if the guess is lower than the random number
           {
               cout << "Too Low! Guess Again." << endl;
               cin >> guess;
               number_of_trys++;
           }
           else if (guess > random_number) //if the guess is higher than the random number
           {
               cout << "Too High, Guess Again!" << endl;
               cin >> guess;
               number_of_trys++;
           }
           else
           {
               number_of_trys++;
               cout << "That's Right!!!! It took you " << number_of_trys << " trys." << endl;
               win = true;
           }
       } //end while

       cout << "Play again[Y/N]: ";
       cin >> go_again;

   } while (go_again == 'Y'); //end do while

   return 0;
}

Solutions

Expert Solution

I replace the do-while loops with simple while loops and the code should be clear enough to understand. Ask in the comment section if you don't understand any part.

Code:

#include <iostream>
#include <cstdlib> //needed to use srand and rand
#include <ctime> //needed to use time
using namespace std;

int main()
{
    int random_number, guess, number_of_trys;
    char go_again;
    unsigned seed;

    seed = time(0);
    srand(seed);

    
    float total_tries=0,total_rounds=0;
    
   

    while(1)
    {
        
        random_number = 1 + rand() % 50;
        number_of_trys = 0;
        total_rounds++;
        
        cout << "I'have picked a number between 1 and 50. Guess the Number!" << endl;
        cin >> guess;
        
        while(1)
        {
               if (guess < random_number) //if the guess is lower than the random number
               {
                   cout << "Too Low! Guess Again." << endl;
                   cin >> guess;
                   number_of_trys++;
               }
               else if (guess > random_number) //if the guess is higher than the random number
               {
                   cout << "Too High, Guess Again!" << endl;
                   cin >> guess;
                   number_of_trys++;
               }
               else
               {
                   number_of_trys++;
                   cout << "That's Right!!!! It took you " << number_of_trys << " trys." << endl;
                   total_tries=total_tries+number_of_trys;
                   break;
               }
           
        }

       cout << "Play again[Y/N]: ";
       cin >> go_again;
       if(go_again!='Y')
            break;
    }

    cout<<"\nThanks for playing. Your average of all trials is: ";
    printf("%.1f",(float)(total_tries/total_rounds));

   return 0;
}

Output:


Related Solutions

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 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...
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...
Create a program that generates random number between 0 and 50 and puts them into a...
Create a program that generates random number between 0 and 50 and puts them into a linked list. When it generates 49, instead of adding it to the list, it prints the list, freeing each node and then exits. Submit your .c file
(+30) Write a python program that generates four random between -50 and +50 using python’s random...
(+30) Write a python program that generates four random between -50 and +50 using python’s random number generator (RNG) see the following code (modify as needed ) import random a = 10 # FIX b =100 # FIX x = random.randint(a,b) # (1) returns an integer a <= x <= b # modify a and b according to the lab requirement print('random integer == ', x) and displays 1. the four integers 2. The maximum integer 3. The minimum integer...
Random Number Guessing Game C++. Write a program that generates a random number between 5 and...
Random Number Guessing Game 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...
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...
Write a program that generates a random number in the range of 1 through 100, and...
Write a program that generates a random number in the range of 1 through 100, and asks the user to guess what the number is. When the number is generated by the computer, don’t display the number to the user, but only display if the number generated is odd or even. 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...
Write a program that generates a random number in the range of 1 through 100, and...
Write a program that generates a random number in the range of 1 through 100, and asks the user to guess what the number is. When the number is generated by the computer, don’t display the number to the user, but only display if the number generated is odd or even. 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...
Write a program that generates a random number in the range of 1 through 100, and...
Write a program that generates a random number in the range of 1 through 100, and asks the user to guess what the number is. When the number is generated by the computer, don’t display the number to the user, but only display if the number generated is odd or even. 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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT