Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program to guess a number from 0 to 999, to the following specifications

ID: 3590625 • Letter: W

Question

Write a program to guess a number from 0 to 999, to the following specifications.

Tell the user to think of a number from 0 to 999. Note that the computer is guessing the number, the user just thinking of one.

Always start with a lower bound of 0 and an upper bound of 999.

As long as the user inputs a string starting with 'l' or 'm'

The next guess is always the average of the upper bound and the lower bound, so the first guess is always 499.

Prompt the user in the following format (changing 499 to another guess each time) and then input a string.

As long as the user does not enter 'y', 'l' or 'm', prompt them with Enter y, lowercase L, or m only! Try again: and input another string.

If the user inputs a string starting with 'l' (or 'L'), change the upper bound.

If the user inputs a string starting with 'm' (or 'M'), change the lower bound.

The next guess is the average of the upper bound and the lower bound.

Keep count of the number of guesses.

Report the number of guesses made and the correct guess.

Sample run (user input in red):

Explanation / Answer

Save the below code in numberguess.c/c++, compile and run.

Give input through stdin whenever prompt. As per your input in question.

/*************************************/

/*
* numberguess.cpp
*
* Created on: 12-Oct-2017
*      Author:
*/

#include <stdio.h>

using namespace std;

int main()
{
   printf("Think of a number from 0 to 999 ");
   int low=0,high=999;
   int guess=(low+high)/2;
   char choice;
   int count=1;
   while(1)
   {
       guess=(low+high)/2;
       printf("Is it %d ?(Enter y if it is,l if your number is less, or m if your number is more.) ",guess);
       fgets(&choice,3,stdin);

       while(!(choice=='y' || choice=='l' || choice =='m'))
       {
       printf("Enter y, lowercase L, or m only! Try again ");
       fgets(&choice,3,stdin);
       }
       if(choice=='y')
       {
           printf("I guessed your number %d in %d guesses. ",guess,count);
           return 0; //to exit the program
       }
       else if(choice=='l')
       {
           high=guess; //updating high value with last guess
           count++; //increment counter
       }
       else if(choice=='m')
       {
           low=guess; //updating low value with last guess
           count++; //increment
       }
   }
   return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote