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
Crank
3 years ago
9

Write a program to have the computer guess at a number between 1 and 20. This program has you, the user choose a number between

1 and 20. Then I, the computer will try to my best to guess the number. Is it a 18? (y/n) n Higher or Lower (h/l) l Is it a 5?(y/n) n Higher or Lower (h/l) h Is it a 10? (y/n) y I got tour number of 10 in 3 guesses.
Computers and Technology
1 answer:
ivanzaharov [21]3 years ago
6 0

Answer:

This question is answered using C++ programming language.

#include<iostream>

#include <cstdlib>

#include <ctime>

using namespace std;

int main(){

   int num, computerguess;

   char response, hl;

   cout<<"Choose your number: ";

   cin>>num;

   srand((unsigned) time(0));

   int high = 20;    int low = 1;    int guess = 0;

   computerguess = low + rand() % high;

   cout<<"Is it "<<computerguess<<" ?y/n: ";

   cin>>response;

   while(response == 'n'){

       cout<<"Higher or Lower? h/l: ";

       cin>>hl;

       if(hl == 'h'){            low = computerguess+1; }

       else{   high = computerguess-1; }

       guess++;

       computerguess = low + rand() % high;

       cout<<"Is it "<<computerguess<<" ?y/n: ";

   cin>>response;

   }

   cout<<"I got your number of "<<num<<" in "<<guess+1<<" guesses.";

   return 0;

}

Explanation:

This declares num (user input) and computerguess as integer

   int num, computerguess;

This declares response (which could be yes (y) or no (n) and hl (which represents high or low) as char

   char response, hl;

This prompts user for input

   cout<<"Choose your number: ";

This gets user input

   cin>>num;

This allows the computer be able to generate different random numbers at different intervals

   srand((unsigned) time(0));

This declares and initializes the range (high and low) to 20 and 1 respectively. Also, the initial number of guess is declared and initialized to 0

   int high = 20;    int low = 1;    int guess = 0;

Here, the computer take a guess

   computerguess = low + rand() % high;

This asks if the computer guess is right

   cout<<"Is it "<<computerguess<<" ?y/n: ";

This gets user response (y or n)

   cin>>response;

The following iteration is repeated until the computer guess is right

   while(response == 'n'){

This asks if computer guess is high or low

       cout<<"Higher or Lower? h/l: ";

This gets user response (h or l)

       cin>>hl;

If the response is higher, this line gets the lower interval of the range

       if(hl == 'h'){            low = computerguess+1; }

If the response is lower, this line gets the upper interval of the range

       else{   high = computerguess-1; }

This increments the number of guess by 1

       guess++;

Here, the computer take a guess

   computerguess = low + rand() % high;

This asks if the computer guess is right

   cout<<"Is it "<<computerguess<<" ?y/n: ";

This gets user response (y or n)

   cin>>response;    }

This prints the statistics of the guess

   cout<<"I got your number of "<<num<<" in "<<guess+1<<" guesses.";

You might be interested in
8. When requesting a review of a denied claim, it is required to send a _______________ with the claim.
liq [111]
I'd say an appeal letter.

An appeal letter should be sent with the claim to request for review of an insurance claim that has been denied or underpaid by an insurance company. An appeal is important because it lets the insurance company know that you disagree with the decisions they have issued



4 0
3 years ago
A motor controller controls the and of the motors.
o-na [289]

Answer:

yes. and?

Explanation:

is that all?

4 0
3 years ago
Since the 1970s, local tv newscasts have developed a similar look. that's because
professor190 [17]
<span>(news doctors, consultants) said people should be young, attractive, smile and look happy.</span>
6 0
4 years ago
What is the effective address generated by the following instructions? Every instruction is
kaheart [24]
D. Mov ax, [bx+si] is your answer
5 0
3 years ago
Emilee is watching an online video. The video is being sent to her laptop by a server over the Internet which splits the video i
Tresset [83]

Answer:

The answer is "Option C".

Explanation:The packet includes clients and modifies information both of these are often referred to as the payload. To achieve payload data by monitor data to get the control signals. It is a unit that will be an information material consisting of a specific network path into such a single packet and web browser transmissions and other types of networks will be used in the IP transmissions, in this, the data packet may arrive in out of order, and the wrong choice can be defined as follows:

  • In choice A, when the connection is good so, all the data is sent on the computer, that's why it is incorrect.
  • In choice B, It is wrong because it can't send back to the server.
  • In choice D, It can't be sent in order, that's why it is wrong.  
6 0
4 years ago
Other questions:
  • Which element in Access is an ideal data source?
    12·2 answers
  • He ____________ may include a high-level WBS in a graphic chart format or as an indentured list of the work elements and associa
    14·1 answer
  • Which statement describes borders and shading?
    11·2 answers
  • Types of relationships include aggregation, composition, link, generalization, and specialization 9. TRUE FALSE
    9·1 answer
  • When performing actions between your computer and one that is infected with a virus, which of the following offers NO risk of yo
    11·2 answers
  • If you wish to collect data from two related websites with different domains in a single google analytics property, what feature
    8·1 answer
  • Click to review the online content. Then answer the question(s) below, using complete sentences. Scroll down to view additional
    14·2 answers
  • If I write too much for an exam answer, do I get downgraded?
    7·1 answer
  • The US government appropriates less than one million dollars in funding each year to STEM education programs. True False
    7·2 answers
  • How are online sources used? Check all that apply.
    6·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!