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
Which are the benefits of leveraging web technologies?
allsm [11]

Lower cost - You can leverage web technologies like FB and Google which allow you to advertise and reach billions of users relying on their services at a very low cost as compared to other mediums.  

Security – Data stored with web technology companies offers more security because it is stored in huge data centers that have high-end protection

Increase business efficiency – Web technologies can increase business efficiency and unlock value in your business functions. They can reduce time, provide critical analytics and help your business grow.

4 0
3 years ago
Read 2 more answers
The challenge of preparing for ____________________ is in ensuring that employees do not feel that they are being prepared for t
Liula [17]

Answer:

"Cross-training " seems to be the right response.

Explanation:

  • Cross-training seems to be the method of constructing a multi-professional workers staff with incentive plans to make sure that they must have the same tools to complete different occupational tasks throughout the organization.
  • This will be a very broad approach besides randomized controlled training methods, both maximum and minimum frequency, for generations.
7 0
3 years ago
Which phone has the most GB (Gigo Bites)<br><br>A.Samsung<br>B.Iphone<br>C.LG<br>D.ZTE
Lina20 [59]
It's GigaBytes actually. And is it RAM or storage capabilities?

And also those are brands. Not models. For example, Samsung has different phones. Not just one.


7 0
3 years ago
Read 2 more answers
Write the definition of a void function that takes as input an integer and outputs two times the number if it is even; otherwise
KatRina [158]

Answer:

Written in C++

void number(int n){

if(n%2 == 0)

cout<<2 * n;

else

cout<<5 * n;

}

Explanation:

The programming language is not stated.

However, I answered using C++

This line defines the function as void

void number(int n){

This line checks if the number is even

if(n%2 == 0)

If yes, it doubles the number and prints the output

cout<<2 * n;

If otherwise,

else

It multiplies the number by 5 and prints the output

cout<<5 * n;

}

To call the function from main, use:

number(n);

Note than n must be declared as integer

See attachment

Download cpp
6 0
3 years ago
4. Give name=
Svet_ta [14]

Answer:

What does Bob [1] return?

What about Bob[-2]?

What about Bob[1:-1]?

How to get the length of Bob?​

Explanation:

8 0
3 years ago
Other questions:
  • Raeball was a lovin doll. And she spoke her mind as she stood tall. She made you special and loved. You're missed by so many. An
    10·2 answers
  • How many packets does your computer send/receive in a single mouse click when you visit a website?
    6·1 answer
  • What determines how large the crystals in an igneous rock will be?
    15·2 answers
  • There are a multitude of items that Cyber Security professionals view as attack vectors but none are more prevalent and exploita
    13·1 answer
  • Self contained sequences of actions to be performed are?
    6·1 answer
  • Can you tell me what is rast
    5·1 answer
  • Can someone take away your points
    15·1 answer
  • Why do you want to work from our company?​
    9·2 answers
  • Which of the following is not a component of Power BI?
    5·1 answer
  • quizlet ann is a security professional for a midsize business and typically handles log analysis and security monitoring tasks f
    6·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!