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]
2 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]2 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
Write multiple if statements: If carYear is before 1967, print "Probably has few safety features." (without quotes). If after 19
Liono4ka [1.6K]

Answer:

public class Main

{

public static void main(String[] args) {

    int carYear = 1995;

   

    if(carYear < 1967)

        System.out.println("Probably has few safety features.");

    if(carYear > 1971)

        System.out.println("Probably has head rests.");

    if(carYear > 1992)

        System.out.println("Probably has anti-lock brakes.");

    if(carYear > 2002)

        System.out.println("Probably has tire-pressure monitor.");

   

}

}

Explanation:

The code is in Java.

Initialize the carYear

Use if statements to handle year before 1967, after 1971, after 1992 and after 2002.

Print the required message for each if statement

5 0
3 years ago
network consisting of computers and other devices that are eithrr fully or partially connected to each other
tekilochka [14]

The answer is a Mesh topology. This method connects every device to each other device in the network. A wired full-mesh topology is not as common as it is impractical and highly expensive. A partial mesh topology offers redundancy if one of the connections goes down and usually uses a connecting medium such as a router to eliminate cables and expensive PCI NIC's.

8 0
3 years ago
What do you observe on the filter paper strip after 2 to 3 hours?
just olya [345]
Is there pictures or anything esle to read in order to figure out the answer

5 0
3 years ago
Read 2 more answers
Shreya has combined all the cells in a single leftmost column in a table. Instead of identifying the category multiple times, sh
aleksklad [387]

The place where Shreya will find these options is option b: Design tab, Merge group.

<h3>What is the Design tab?</h3>

The Design tab is known to be one that is made up of table formatting, cell as well as the table borders.

It is one that has the features of arranging the table on the page, as well as the size of the table.

Hence, The place where Shreya will find these options is option b: Design tab, Merge group.

Learn more about Design tab from

brainly.com/question/2501083

#SPJ1

3 0
1 year ago
Which tool lets you increase the view, but not the size, of text, images, and other content?
blagie [28]
Use ctrl + or - or if in microsoft word, use the zoom tool
7 0
3 years ago
Other questions:
  • What bit position in an ascii code must be complemented to change the ascii letter represented from uppercase to lowercase and v
    14·1 answer
  • What are the first two lines of defense a company should take when addressing security risks?
    10·1 answer
  • In what areas is leslie's underspending hurting her
    10·1 answer
  • What is the benefit of a cloud computing infrastructure? ○ Companies have full control over the data that is hosted. ○ Companies
    11·1 answer
  • Why are the keys on the qwerty keyboard arranged the way they are?
    11·1 answer
  • A communication medium which allows receivers to observe multiple cues, such as body language and tone of voice, and allows send
    15·1 answer
  • Assslainsdffddsvvdesdssbhasasco5m
    5·2 answers
  • A (n) ___ system can help a business alleviate the need for multiple information systems.
    13·1 answer
  • An update anomaly can occur if A. an instance of the same data is stored in two or more places in the database. B. a data elemen
    7·1 answer
  • Which are print settings that can be modified in word 2019 choose two answers​
    8·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!