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
BlackZzzverrR [31]
3 years ago
15

1. Write a program that plays the game of "Guess the Number" as follows: Your program should choose (generate a random number) t

o be guessed by the user in the range of 1 to 1000. The player will type a guess. The program responds with one of the following: a) Excellent! You guessed the number! Would you like to play again (y or n)? b) Too Low. Try Again. c) Too High. Try Again.
Computers and Technology
1 answer:
kirill115 [55]3 years ago
8 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 play again (y or n)? " <<endl;

cin>>reply;  

if(reply == 'y')

    game();      

if(reply == 'n')

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

return 0;

}

void game()

{

   int num, guess;

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

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

   do

{      

        do

        {

            cout<<"Guess any number between 1 and 1000. "<< endl;

            cin>>guess;

            if(guess<1 || guess>1000)

            {

                cout<<"Invalid number. Guess any number between 1 and 1000. "<<endl;

                cin>>guess;

            }        

             

        }while(guess<1 || guess>1000);    

     

    if(guess < num)

    {

        cout<<"Too Low. Try again."<<endl;            

    }      

    if(guess > num)

    {

        cout<<"Too High. Try again."<<endl;              

    }      

}while(guess != num);  

cout<<"Excellent! You guessed the number!"<<endl;

}

 

OUTPUT

Welcome to the game of guessing the correct number  

Guess any number between 1 and 1000.  

45

Too Low. Try again.

Guess any number between 1 and 1000.  

56

Too Low. Try again.

Guess any number between 1 and 1000.  

63

Too High. Try again.

Guess any number between 1 and 1000.  

61

Too High. Try again.

Guess any number between 1 and 1000.  

57

Too Low. Try again.

Guess any number between 1 and 1000.  

58

Too Low. Try again.

Guess any number between 1 and 1000.  

59

Too Low. Try again.

Guess any number between 1 and 1000.  

60

Excellent! You guessed the number!

Would you like to play again (y or n)?  

n

Quitting the game...  

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 executes until a valid input is received from the user.

     do

        {

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

            cin>>guess;

            if(guess<5 || guess>15)

            {

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

                cin>>guess;

            }              

            attempt++;              

        }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;

    }

    if(guess > num)

    {

        cout<<"Too High"<<endl;  

    }  

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
If you are weak or lacking in an area or skillset, what kinds of things can you do to compensate or improve?
Ilya [14]
Put some more of your time to help improve that skill.try to learn to get better.
6 0
3 years ago
In one week, your company received the following quantities of e-mail messages. Monday 240 Tuesday 315 Wednesday 290 Thursday 18
Vika [28.1K]

Answer:

280

Explanation:

Average = (240 + 315 + 290 + 180 + 375) ÷ 5

= 1400 ÷ 5

= 280

Cheers

3 0
3 years ago
Collins and quillian explained the results of priming experiments by introducing the concept of _____ into their network model.
insens350 [35]
<span>Spreading Activation This is a model of working memory, which can be otherwise called here and now memory in lay man's term, that tries to clarify how the mind forms related thoughts, particularly semantic or verbal ideas. The spreading enactment show is one way intellectual clinicians clarify the preparing impact, which is the noticeable wonder that a man can all the more rapidly review data about a subject once a related idea has been presented. As indicated by this model, semantic long haul memory comprises of a huge, interrelated system of ideas. At the point when a man is given any idea, the ideas most firmly associated with it are initiated in that individual's psyche, getting ready or "preparing" him or her to review data identified with any of them</span>
4 0
3 years ago
Aika has several labeled folders for different parts of his research. What is the MOST likely reason he does this? He wants to m
kobusy [5.1K]

Answer:

<u>He wants to easily and quickly find specific bits of research that cover certain topics.</u>

<u>Explanation:</u>

Remember, a research work typically has these 6 (six) components:

  • The Abstract.
  • Introduction
  • Methodology
  • Results
  • Discussion
  • References.

Thus, Aika's decision to label several folders for different parts of his research would make it easier and quicker for him to find specific bits of research that cover certain topics, instead of spending time looking for topics.

4 0
3 years ago
Write an expression that executes the loop while the user enters a number greater than or equal to 0. Note: These activities may
RoseWind [281]

Answer:

while ( num >= 0) { ... }

Explanation:

Required

Express to execute a while loop with the given condition.

The condition in the question is that: the user input must be greater than or equal to 0.

The statement for this is:

while ( num >= 0) { ... }

Where num is the input variable

8 0
2 years ago
Other questions:
  • Mica's creating his web page with a software that is free of charge for the first month. If he likes the program, he will have t
    8·2 answers
  • A motherboard uses dual channeling, but you have four DIMMs available that differ in size. The motherboard supports all four siz
    7·1 answer
  • Jeff has created a table to calculate the cost of raw materials that he purchases monthly. Cell A2 shows the fixed cost of the r
    8·2 answers
  • ANSWER QUICKLY!
    10·1 answer
  • When you open a browser window, it open in a _____. <br> a. fieldb. folderc. menud. window?
    7·1 answer
  • Hey, wanna pair our account on Brainly so we can share the perks? https://brainly.com/invite/8db681239ab5027e03b097bcded13755?ut
    15·2 answers
  • Consider the method get Hours, which is intended to calculate the number of hours that a vehicle takes to travel between two mil
    6·1 answer
  • Anyone know how to fix this problem on Microsoft Team?
    14·2 answers
  • Instructions
    8·1 answer
  • Which emerging technologies will have more injury on our day to day country &amp; How?​
    10·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!