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
Write a loop that reads positive integers from standard input, printing out those values that are greater than 100, and that ter
zepelin [54]
Thank you for posting your question here. Below is the answer, I hope it helps. 

int number = 0;

while (stdin.hasNextInt() && number >= 0){
number =stdin.nextInt();
if (number > 100)
System.out.print(number + " ");
<span>}</span>
7 0
3 years ago
How many bits per pixel does a black and white image require?
allochka39001 [22]
Depends on the size of the image

Hope this is helpful
8 0
3 years ago
I want to know all the part of computer system
Effectus [21]
There are alot of parts in a computer system. You have to be a little specific
4 0
3 years ago
n a particular board game, there is exactly one row and it comprises N spaces, numbered 0 through N - 1 from left to right. Ther
Ganezh [65]

Answer:

class MarblesBoard(object):

   def __init__(self, seq):

        self.seq = list(seq)

   def switch(self):

       temp = self.seq[0]

       self.seq[0] = self.seq[1]

       self.seq[1] = temp

   def rotate(self):

       temp = self.seq[0]

       for i in range(1, len(self.seq)):

           self.seq[i-1] = self.seq[i]

       self.seq[-1] = temp

   def is_solved(self):

       for i in range(len(self.seq)):

           if i != self.seq[i]:

               return False

       return True

   def __str__(self):  

       return ' '.join(list(map(str,self.seq)))

   def __repr__(self):

       return ' '.join(list(map(str,self.seq)))

class Solver(object):  

   def __init__(self, board):

       self.board = board

   def solve(self):      

       steps = 0

       while not self.board.is_solved():        

           if self.board.seq[0] > self.board.seq[1] and self.board.seq[0] != len(self.board.seq) - 1:

               self.board.switch()

           else:

               self.board.rotate()

           print(self.board)

           steps += 1

       print('Total steps:', steps)

Explanation:

The Python class MarblesBoard creates an object of the board game used in the Solver class object instance and it holds data or attributes of the player piece movement and the magic methods (__str__ and __repr__). The Solver object gets the switch and rotate movementt of the player and displays the total steps at the end of the game.

3 0
3 years ago
The state department that issues driver's licenses would like a police officer to be able to scan the driver's license and insta
frozen [14]

Answer:

The technology that the state should use is the Optical Character Recognition or OCR system which comprises of a driver license card with bar code or optical code character engraved on it that can be read by optical scanners that are then linked via the internet to access the recorded data regarding the card holder, including outstanding tickets and warrants.

With the system, a police will be able to both verify the authenticity of the drivers license and get answers to questions about the vehicle driver

Explanation:

6 0
3 years ago
Other questions:
  • Write a function named print_backward that accepts a String as its parameter and prints the characters in the opposite order. Fo
    12·1 answer
  • How many bits are required to address a 1mx8 main memory if main memory is byte addressable?
    6·1 answer
  • Write a program that reads a file containing text. Read each line and send it to the output file, preceded by line numbers. If t
    8·1 answer
  • How is it possible to find encyclopedias and reference texts on the internet
    11·2 answers
  • A database stores a large amount data in vertical ___
    10·1 answer
  • Hope goes through the steps of sorting a list. The resulting list is shown below.
    15·1 answer
  • Can I get some help thank you
    8·2 answers
  • On the server side, the database environment must be properly configured to respond to clients' requests in the fastest way poss
    12·1 answer
  • When nat hides the source ip of an originating device, this is known as ________?
    15·2 answers
  • Free coins who is octane and dont say u dont know because i will do the same to u
    13·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!