1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
juin [17]
3 years ago
5

Write a program that generates a random number between 5 and 15 and asks the user to guess what the number is. If the user’s gue

ss 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 the user correctly guesses the random number. Then the program should display the number of guesses along with the following message Congratulations. You figured out my number. Suggest that you also give the user the opportunity to play the game again or quit.
Computers and Technology
1 answer:
stepan [7]3 years ago
4 0

Answer: The c++ program for the number guessing game is given below.

#include <iostream>

using namespace std;

// method declaration without parameters

void game();

int main() {    

char reply;

// calling the game method

game();  

cout<<"Would you like to continue with the game ? Enter y to continue. Enter q to quit the game. " <<endl;

cin>>reply;  

if(reply == 'y')

    game();      

if(reply == 'q')

    cout<<"Quitting the game..."<<endl;  

return 0;

}

void game()

{

   int num, guess, attempt=0;  

num = (rand() % 10) + 6;  

cout<<endl<<"Welcome to the game of guessing the correct number "<<endl;  

   do

{    

    cout<<"Enter any number between 5 and 15. "<< endl;

    cin>>guess;      

    if(guess<5 || guess>15)

    {

        do

        {

            cout<<"Invalid number. Enter any number between 5 and 15. "<<endl;

            cin>>guess;

        }while(guess<5 || guess>15);

    }  

    if(guess < num)

    {

        cout<<"Too Low"<<endl;

        attempt++;        

    }

    if(guess > num)

    {

        cout<<"Too High"<<endl;

        attempt++;          

    }      

}while(guess != num);  

cout<<"Congratulations. You figured out my number in "<<attempt<< " attempts."<<endl;

}

Explanation:

The expression

(rand() % 10)

generates a random number between 0 and 9. 6 is added to generate a random number between 5 and 15.

The integer variable attempt is initialized to 0.

Next, user is asked to guess the number. If the user enters an invalid input, the do-while loop begins until a valid input is received from the user.

    cout<<"Enter any number between 5 and 15. "<< endl;

    cin>>guess;      

    if(guess<5 || guess>15)

    {

        do

        {

            cout<<"Invalid number. Enter any number between 5 and 15. "<<endl;

            cin>>guess;

        }while(guess<5 || guess>15);

    }  

User is prompted to input the guess using the same do-while loop as above. Until the user guesses the correct number, the loop continues. Each incorrect guess by the user, increments the variable attempt by 1. This represents the number of attempts done by the user to guess the correct number.

    if(guess < num)

    {

        cout<<"Too Low"<<endl;

        attempt++;        

    }

    if(guess > num)

    {

        cout<<"Too High"<<endl;

        attempt++;          

    }  

The random number generation and the guessing logic is put in a separate method game().

The entry and exit from the game only is put inside the main method.

You might be interested in
What do you mean by computer ethics?​
STALIN [3.7K]

Answer:

Computer ethics is a field of applied ethics that addresses ethical issues in the use, design and management of information technology and in the formulation of ethical policies for its regulation in society.

3 0
2 years ago
True or False? At any point in time, an open file has a current file pointer indicating the place where the next read or write o
mr_godi [17]

Answer:

True is the correct answer for the above question.

Explanation:

  • When any document file is opened then every point has some particular address. so there is a pointer which states that where the read operation and the write operation is going on.
  • When any person writes any program to read a file or write a file then there is a need for some variable that is pointed for the reading and the write operation.
  • The document which is used for the write data or read data is also designed and maintained by some software.
  • Hence we can say that there are needs of some variable that point the operation of the file and it is also stated from the above question. Hence the above question statement is a true statement.
5 0
3 years ago
In which case will the linear search return the lowest value faster than the<br> binary search?
Sveta_85 [38]

Answer:

A linear search is one that scans every record/file until it discovers the value being searched for.

Binary search, on the other hand, is also known as <em>Logarithmic search</em>. It is used to locate the position of a value inside an array that has already been sorted.  

The linear search will return the lowest value faster than the binary search when small arrays are involved.

This will only be feasible when the array is sorted prior.

Cheers!

5 0
3 years ago
The tcp protocol provides error detection and correction. <br><br> a. True <br> b. False
ANEK [815]
B. False

Explanation

TCP provides reliable, ordered, and error-checked delivery of a stream of octets (bytes) between applications running on hosts communicating via an IP network.
4 0
1 year ago
. If you executean infinite recursive function on a computer it will executeforever.
S_A_V [24]

Answer:

b. False

Explanation:

If you execute an infinite recursive function on a computer it will NOT execute forever.

4 0
2 years ago
Other questions:
  • The use of digital technology and the Internet to execute the major business processes in the enterprise is called
    11·1 answer
  • I need a idea of a origami for my coding class and it needs to be easy to make
    15·2 answers
  • What is a main cause of a virus on a computer
    6·1 answer
  • Which wireless technology has a typical transfer rate of 1 Mbps to 3 Mbps at distances up to about 10 meters?
    7·1 answer
  • Declaring a variable in the method’s body with the same name as a parameter variable in the method header is ___________.
    8·1 answer
  • A manager wants to set up an area that is not on the LAN but not quite on the Internet. This area will house servers that will s
    5·1 answer
  • Enter the number 2568 into the box below
    14·1 answer
  • Presently we can solve problem instances of size 30 in 1 minute using algorithm A, which is a algorithm. On the other hand, we w
    8·1 answer
  • What is the difference between = and == in terms of java..?
    6·2 answers
  • HELP PLZZZZZZ
    10·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!