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

Write a program that can guess any number between 1 and 1000 by asking 10 simple

ID: 3669144 • Letter: W

Question

Write a program that can guess any number between 1 and 1000 by asking 10 simple questions of the form "Is your number greater than x?".

I believe the idea is to answer a series of yes and no questions and determine from those questions what their number is. However I do not believe that the program needs to take in any user inputs.The only way that I can think to solve this is a series of nested if else statements, but that seems like quite a hassel and that there has to be an easier way. Thanks for the help! (In c)

Explanation / Answer

Write a program that can guess any number between 1 and 1000 by asking 10 simple questions of the form "Is your number greater than x?".

sol. It IS the binary search; the *minimum* number of guesses would be 1 (if you guessed the correct number right off the bat) and the number of guesses in the average/worst cases would be log N, which is log 1000, ie approximately 10.

Program:

#include<stdio.h>
int main(){

int l,u,mid;
char cc;

l=1,u=1000;
while(l<=u)
   {
       mid=(l+u)/2;
       printf(" IS YOUR NUMBER IS GREATER THAN %d than enter y else n",mid);
scanf (" %c",&cc);
       if(cc == 'n'){
           u=mid-1;
       }
       else
           l=mid+1;
   }

       printf("The number is number is %d",mid);
  
return 0;
}

Output:

IS YOUR NUMBER IS GREATER THAN 500 than enter y else n n

IS YOUR NUMBER IS GREATER THAN 250 than enter y else n y

IS YOUR NUMBER IS GREATER THAN 375 than enter y else n y

IS YOUR NUMBER IS GREATER THAN 437 than enter y else n y

IS YOUR NUMBER IS GREATER THAN 468 than enter y else n n

IS YOUR NUMBER IS GREATER THAN 452 than enter y else n n

IS YOUR NUMBER IS GREATER THAN 444 than enter y else n y

IS YOUR NUMBER IS GREATER THAN 448 than enter y else n y

IS YOUR NUMBER IS GREATER THAN 450 than enter y else n n

The number is number is 450

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