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
goldenfox [79]
3 years ago
6

Write a program with a method that plays the guess a number game. The program should allow the user to pick a number between 1 a

nd 1000 in his head. The method should guess the user's number in a minimal amount of attempts. The method should ask the user "is the number greater than or less than the number " and the program gives a particular number. The user in some way just inputs (higher or lower) or (greater than or less than). There also has to be a way for the user to say the number has just been guessed. Of course, the user cannot answer incorrectly or change the number midstream. Note - if written efficiently, the method should be able to guess any number in 10 or less attempts.
Computers and Technology
1 answer:
nlexa [21]3 years ago
8 0

Answer:

Please check the explanation

Explanation:

That's the code and it is done with the program in c++ according to instructions given in the question using binary search. It can guess the correct number in 10 or fewer attempts and also shows the number of attempts it took to guess the number.

​ #include <iostream> using namespace std; int guess() { string input; int l = 1, h = 1000; int mid = (l + h) / 2, count = 0; while (1) { //count the number of attemts to guess the number ++count; //cout << count << "\n"; cout << "\n"; cout << "Is " << mid << " correct? (y/n): "; cin >> input; //if input is y print the guessed no. and return if (input == "y") { cout << mid << " guessed in " << count << " attempts!\n"; return 1; } //if input is n ask the user if it's higher or lower than current guess if (input == "n") { cout << "Is the number greater than or less than the number ? (h/l): "; cin >> input; } //if input is higher assign mid incremented by 1 to low //else decrement mid by 1 and assign to high if (input == "h") l = mid + 1; else h = mid - 1; //calculate mid again according to input by user again mid = (l + h) / 2; } } int main() { cout << "****WELCOME TO THE GUESS THE NUMBER GAME!****\n"; cout << "Guess any number between 1 to 1000.\n"; cout << "This game depends on user giving correct answers and not changing their number middle of game.\n"; guess(); } ​

You might be interested in
"omr, ocr, and micr are all types of ________."
Juliette [100K]
OMR, OCR and MICR are all types of character and mark recognition devices. They are scanners <span>that are able to recognize special </span>characters and marks<span>.
</span>OMR stands for Optical Mark Recognition,which <span>senses the presence or absence of a mark.</span>
OCR stands for Optical Character recognition, which reads special preprinted characters by light source.<span>
MICR stands for Magnetic Ink Character Recognition, where  </span><span>machine reads character made of ink containing magnetized particles.
</span>
4 0
3 years ago
Read 2 more answers
in Google, how should you phrase your search if you want to exclude a certain word from your results(for example,"chocolate")?
dusya [7]
I found this:
ou can exclude words from your search by using the - operator; any word in your query preceded by the - sign is automatically excluded from the search results. Remember to always include a space before the - sign, and none after

I found it here:
http://www.informit.com/articles/article.aspx?p=675274&seqNum=3
3 0
3 years ago
List out analog computer​
Fynjy0 [20]

Answer:

1. Wall clocks.

2. Analog watches

3. Measuring instruments with callibration

Explanation:

5 0
3 years ago
Will sum1 come bust me outta FCA....replies u gotta live in knoxville though
snow_tiger [21]

Answer:

im on my way cuz

Explanation:

4 0
3 years ago
Read 2 more answers
Does anyone play call of duty cold war campaign. I have a question, please help me.
-Dominant- [34]

Answer:

so please

Explanation:

please upload the question so I can answer ,by the way I don't play call of duty

5 0
3 years ago
Read 2 more answers
Other questions:
  • Shawn works at the local electrical company in Nampa, ID, which provides electricity to the entire city of Boise. This area is p
    6·1 answer
  • A collection of facts can be copyrighted, but only if the collection is ____ in a way that causes the resulting work to rise to
    14·1 answer
  • 2. Research, find and use a better (different from the simple Shift Cipher) encryption technique to encrypt a string of your cho
    15·1 answer
  • If we are using an 4-character password that contains only lowercase English alphabetic characters (26 different characters), ho
    15·2 answers
  • One of your professors has asked you to write a program to grade her midterm exams, which consist of only 20 multiple-choice que
    15·1 answer
  • Jamie has to enter the names, grades, and scores of a group of students into a worksheet. Which option will Jamie use to describ
    15·1 answer
  • Bob has 2 candy bars he is fat what hapennes
    5·2 answers
  • it is a program a personal computer microprocessor uses to get the computer system started after you turn it on​
    10·1 answer
  • What is multimedia hard ware? Write any four examples of multimedia hardware. <br>​
    13·1 answer
  • Consider a pipelined processor with just one level of cache. assume that in the absence of memory delays, the baseline cpi of th
    13·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!