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
Your friend wants to know about Microsoft Expression Web 4.
Vlad [161]
Microsoft Expression Web 4 is 
--
Component of Expression Studio
--
Design and Develop Web Pages using HTML5, CC3, ASP.Net, and more
--
Requires .Net Framework & Silverlight 4.0
--
3 0
3 years ago
Computer files containing nothing but printable characters are called ____ files.
natali 33 [55]
Computer files containing nothing but printable characters are called text files<span>.</span>
7 0
3 years ago
When considering the typical characteristics of a server, how is the server optimized in relation to applications?
VLD [36.1K]

Answer: Server are optimized so that they can work efficiently and improve their performance while working. Application server optimization mechanism can be used for optimizing the server with respect to the application.

Some of the techniques used in the application server optimization are as follows:-

  • Load balancing- which helps in upgrading throughput ,improves the time of response, enhance the bandwidth of network etc.
  • Fail-over management- it is used for decrement of latency, balancing the server, increasing the availability of network and it resources etc.

3 0
3 years ago
Discuss advantages and disadvantages of os
Serhud [2]

Answer:

Advantages :

Management of hardware and software

easy to use with GUI

Convenient and easy to use different applications on the system.

Disadvantages:

They are expensive to buy

There can be technical issues which can ruin the task

Data can be lost if not saved properly

Explanation:

Operating system is a software which manages the computer. It efficiently manages the timelines and data stored on it is considered as safe by placing passwords. There are various advantages and disadvantages of the system. It is dependent on the user how he uses the software.

5 0
3 years ago
Which of the following best describes today’s average gamer?
GrogVix [38]
The correct answer is A
6 0
2 years ago
Other questions:
  • Which statement prints "hi" on the screen?
    5·2 answers
  • What is the synonym for term port?
    9·1 answer
  • In which scenario would instant messaging be more useful than other forms of communication?
    10·1 answer
  • Materials such as copper, silver, and aluminum through which electric energy passes freely are called
    13·1 answer
  • What do you think is the most important factor affecting the collection of digital data and what impact do you think that this f
    13·1 answer
  • A thin red border indicates the active cell. (True or False)
    12·1 answer
  • What are some pros for having your phone during school hours?
    10·2 answers
  • Write a Python function called readlinesmton that accepts the name of the file (i.e. filename), starting line number (i.e. m) an
    7·1 answer
  • Wanna play pubg<br><br><br>ill send u my id​
    5·2 answers
  • Is it true that if the user log out the computer will turn off automatically​
    9·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!