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
Please tell fast plzzzzzz​
Nata [24]

Answer:

heyyy

your answer is false

HOPE IT HELPS YOU OUT PLEASE MARK IT AS BRAINLIEST AND FOLLOW ME PROMISE YOU TO FOLLOW BACK ON BRAINLY.IN

7 0
3 years ago
Read 2 more answers
What is a sorting algorithm
alexdok [17]

Explanation:

In computer science, a sorting. algorithm is an algorithm that puts elements of a list in a certain order. The most frequently used orders are numerical order and lexicographical order. Efficient sorting is important for optimizing the efficiency of other algorithms that require input data to be in sorted lists

8 0
3 years ago
Why are cyber attacks using SWIFT so dangerous?​
Kipish [7]

Answer:

options below you can select the ans

SWIFT is the protocol used by all US healthcare provides to encrypt medical records

SWIFT is the fight plan and routing system used by all cooperating nations for International commercial flights

SWIFT is the protocol used to transmit all diplomatic telegrams between governments around the world

SWIFT is the protocol used by all banks to transfer money

5 0
3 years ago
Ray invested in 2 different savings bonds did he diversify
Ratling [72]
Ray had become diversified.
8 0
4 years ago
Read 2 more answers
Introduction of waste management​
Molodets [167]

Answer:

Waste management (or waste disposal) includes the activities and actions required to manage waste from its inception to its final disposal. ... Waste management deals with all types of waste, including industrial, biological and household. In some cases, waste can pose a threat to human health.

7 0
3 years ago
Other questions:
  • May someone help please. :(
    9·1 answer
  • Machinery with rotating or reciprocating parts that are within seven feet of the floor or working platform requires what additio
    7·1 answer
  • To use Click and type feature can be used to format and enter text graphic and other items
    6·1 answer
  • In the movie, Willy Wonka and the Chocolate Factory, Augustus Gloop leans over the chocolate river to get a drink and falls in.
    11·1 answer
  • One microsleep warning sign occurs when you catch yourself leaning blank in the drivers seat
    12·1 answer
  • The declaration of variables begins with a command a. dim b. rem c. const​
    8·1 answer
  • At currents as low as
    5·1 answer
  • HELP GEKP HELP HELP HELP HELP Cassandra is shooting a photo series in very low light. What kind of lens opening will
    15·1 answer
  • What concerns exist wherever personally identifiable information is collected stored in digital form or otherwise
    10·1 answer
  • Please help……Your friend is taking images from all over the internet without giving credit to the sources. He tells you that it’
    6·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!