Think of a number between 1 and 100 in your mind. Then, your program should ask
ID: 3549963 • Letter: T
Question
Think of a number between 1 and 100 in your mind. Then, your program should ask you minimal # of questions and determine your number based on your answers. Program's question format will be Is it NN (<, =, >)? You can respond to that question in 3 ways: < indicates that your number is less than computer's guess, = means that program guessed your number right, and > means that your number is greater than computer's guess. Allow the user to play this game any number of times. When the user is ready to quit, output average # of guesses / game.
Use a separate function to play the game. Invoke that function from main() repeatedly as needed.
So the output should look like this:
Guess a number between 1 and 100 (both inclusive) and get ready to answer a few questions.
Is it 50 (<,=,>)? <
Is it 25 (<,=,>)? <
Is it 12 (<,=,>)? >
Is it 18 (<,=,>)? >
Is it 21 (<,=,>)? <
Is it 19 (<,=,>)? >
Your guess is 20
It was a good game! Do you want to play again (y/n) ? y
Guess a number between 1 and 100 (both inclusive) and get ready to answer a few questions.
Is it 50 (<,=,>)? >
Is it 75 (<,=,>)? <
Is it 62 (<,=,>)? >
Is it 68 (<,=,>)? >
Is it 71 (<,=,>)? =
It was a good game!
Do you want to play again (y/n) ? n
Explanation / Answer
please rate - thanks
please rate - thanks
if you don't normally use getch() and #include <conio.h>
just remove them from the code
#include <conio.h>
#include <stdio.h>
int game();
int main()
{int guesses=0,games=0;
char yes;
do
{
guesses+=game();
games++;
printf("It was a good game! Do you want to play again (y/n) ?");
yes=getchar();
while((getchar())!=' ');
}while(yes=='y');
printf("average # of guesses / game. %.2f ",(double)guesses/games);
getch();
return 0;
}
int game()
{char c;
int min=1,max=100,num,count=0;
printf("Guess a number between 1 and 100 (both inclusive) and get ready to answer a few questions. ");
do
{
if(max==min)
{printf("Your guess is %d ",max);
return count;
}
count++;
num=(max+min)/2;
printf("Is it %d (<,=,>)?",num);
c=getchar();
while((getchar())!=' ');
if(c=='>')
min=num+1;
else if(c=='<')
max=num-1;
}while(c!='=');
return count;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.