Question

" Number Guessing Game You should think of a number between 1 and 100. Then, over...

"

Number Guessing Game

You should think of a number between 1 and 100. Then, over and over again, the computer can suggest an answer. Your response to the computer guess will be as following:

  1. If that answer is too small, you enter the character '>'.
  2. If the computer's answer is too large, you enter the character '<'.
  3. If the computer's answer is just right, you enter '='

Requirements:

  • Your code should show the number of time the computer needed to guess the correct number
  • Make sure your code looks nice
  • Make sure to write comments to explain how your algorithm works
  • Please upload code + screenshot of the output

Hints:

  • There are multiple ways for the computer to find the number, there are a slow algorithm and a faster algorithm (If you use the faster algorithm you will get a full mark)
  • Finding a middle point always helps

Have fun!

"

Here is what I have so far:

/*
Input: <, >, and =.
Output: Number.
Description: This code will guess your number in 7 moves or less.
Language: C
*/

#include <stdlib.h>
#include <stdio.h>

void main(void)
{
int mid = 50;
int max = 100;
int min = 1;
char ans;
char small = '>';
char large = '<';
char right = '=';
int i = 0;

printf("Choose a number between 1 to 100. I can guess your number in 7 moves or less\n");
printf("Is your number 50?\n");
printf("[>] If answer is too small.\n");
printf("[<] If answer is too large.\n");
printf("[=] If answer is just right.\n");
scanf(" %c", &ans);
while(ans != right)
{
if(ans == large)
{
printf(" Is your number %d?\n", mid/2);
max = mid + 1;
mid = (min+max)/2;
scanf(" %c", &ans);
i++;
}
if(ans == small)
{
printf(" Is your number %d?\n", (mid + max)/2);
min = mid - 1;
mid = (mid + max)/2;
scanf(" %c", &ans);
i++;
}
}
if(ans == right)
{
printf("Yay! I choose your answer in %d moves\n", i);
exit(0);
}
}

-------------------------------------------------

Currently, it works if the number is less than 50 but breaks when the number is higher than 50. I am pretty sure the math part is wrong but I don't know how to fix it.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

CODE:-

#include <stdlib.h>
#include <stdio.h>

void main(void)
{
int mid = 50;
int max = 100;
int min = 1;
char ans;
char small = '>';
char large = '<';
char right = '=';
int i = 1;/*here it must be 1 because suppose number guessed was actually 50 then
it shows that number of moves was 0 which is incorrect because it does have make a move
by comparing guessed number to 50*/

printf("Choose a number between 1 to 100. I can guess your number in 7 moves or less\n");
printf("Is your number 50?\n");
printf("[>] If answer is too small.\n");
printf("[<] If answer is too large.\n");
printf("[=] If answer is just right.\n");
scanf(" %c", &ans);
while(ans != right)
{
if(ans == large)
{
max = mid-1;/* here first we need to change max value as mid is too large
so , we need to change max to number immediate smaller than mid*/
mid = (max+min)/2;/*now, change mid according to new max value .
add max and min and divide it by 2 and then take its floor value*/
printf(" Is your number %d?\n", mid);
scanf(" %c", &ans);
i++;
}
if(ans == small)
{
min = mid+1;/* here first we need to change min value as mid is too small
so , we need to change min to number immediate bigger than mid*/
mid = (min+max)/2;/*now, change mid according to new max value .
add max and min and divide it by 2 and then take its floor value*/
printf(" Is your number %d?\n", mid);

scanf(" %c", &ans);
i++;
}
}
if(ans == right)
{
printf("Yay! I choose your answer in %d moves\n", i);
exit(0);
}
}

output:-

When guessed number is larger than 50

when guessed number is smaller than 50

CODE screenshot:-

Add a comment
Know the answer?
Add Answer to:
" Number Guessing Game You should think of a number between 1 and 100. Then, over...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Java Listed below is code to play a guessing game. In the game, two players attempt...

    Java Listed below is code to play a guessing game. In the game, two players attempt to guess a number. Your task is to extend the program with objects that represent either a human player or a computer player. boolean checkForWin (int guess, int answer) { System.out.println("You guessed" + guess +"."); if (answer == guess) { System.out.println( "You're right! You win!") ; return true; } else if (answer < guess) System.out.println ("Your guess is too high.") ; else System.out.println ("Your...

  • In Java You’re going to make a Guess the Number game. The computer will "think" of...

    In Java You’re going to make a Guess the Number game. The computer will "think" of a secret number from 1 to 20 and ask the user to guess it. After each guess, the computer will tell the user whether the number is too high or too low. The user wins if they can guess the number within six tries. The program should look like this in the console, player input is in bold: Hello! What is your name? Abaddon...

  • Make a FLOWCHART for the following JAVA Prime Number Guessing Game. import java.util.Random; import java.util.Scanner; public...

    Make a FLOWCHART for the following JAVA Prime Number Guessing Game. import java.util.Random; import java.util.Scanner; public class Project2 { //Creating an random class object static Random r = new Random(); public static void main(String[] args) {    char compAns,userAns,ans; int cntUser=0,cntComp=0; /* * Creating an Scanner class object which is used to get the inputs * entered by the user */ Scanner sc = new Scanner(System.in);       System.out.println("*************************************"); System.out.println("Prime Number Guessing Game"); System.out.println("Y = Yes , N = No...

  • python code for guessing game users enter the number the computer will guess 8:38 PM 100...

    python code for guessing game users enter the number the computer will guess 8:38 PM 100 nstructure.com 1. Number guessing game : (5 points) 1) Requirements: Your program will ask the user to enter a number between 1 and 1000. Then it will try to guess the correct number with the guaranteed minimal average number of guesses. Which search algorithm should you use? 2) Expected Output: Enter a number between 1 and 1000? 784 Computer guesses: 500 Type 'l' if...

  • Write a JAVA program that plays a number guessing game with the user. A sample run...

    Write a JAVA program that plays a number guessing game with the user. A sample run for the game follows. User input is shown in boldface in the sample run. Welcome to the game of Guess It! I will choose a number between 1 and 100. You will try to guess that number. If your guess wrong, I will tell you if you guessed too high or too low. You have 6 tries to get the number. OK, I am...

  • Need help with this C program? I cannot get it to compile. I have to use...

    Need help with this C program? I cannot get it to compile. I have to use Microsoft Studio compiler for my course. It's a word game style program and I can't figure out where the issue is. \* Program *\ // Michael Paul Laessig, 07 / 17 / 2019. /*COP2220 Second Large Program (LargeProg2.c).*/ #define _CRT_SECURE_NO_DEPRECATE //Include the following libraries in the preprocessor directives: stdio.h, string.h, ctype.h #include <stdio.h> /*printf, scanf definitions*/ #include <string.h> /*stings definitions*/ #include <ctype.h> /*toupper, tolower...

  • #include<stdio.h> #include<stdlib.h> #include <time.h> int main() { /* first you have to let the computer generate...

    #include<stdio.h> #include<stdlib.h> #include <time.h> int main() { /* first you have to let the computer generate a random number. Then it has to declare how many tries the user has for the game loop. we then need the player to enter their guess. After every guess we have to give an output of how many numbers they have in the right location and how many they have the right number. The player will keep guessing until their 10 tries are...

  • For a C program hangman game: Create the function int play_game [play_game ( Game *g )]...

    For a C program hangman game: Create the function int play_game [play_game ( Game *g )] for a C program hangman game. (The existing code for other functions and the program is below, along with what the function needs to do) (Also the link to program files (hangman.h and library file) is below the existing code section. You can use that to check if the code works) What int play_game needs to do mostly involves calling other functions you've already...

  • C++ HELP I need help with this program. I have done and compiled this program in a single file called bill.cpp. It works fine. But I need to split this program in three files 1. bill.h = contains the...

    C++ HELP I need help with this program. I have done and compiled this program in a single file called bill.cpp. It works fine. But I need to split this program in three files 1. bill.h = contains the class program with methods and variables 2. bill.cpp = contains the functions from class file 3. main.cpp = contains the main program. Please split this program into three files and make the program run. I have posted the code here. #include<iostream>...

  • I am trying to figure out why my C code is outputting "exited, segmentation fault". The...

    I am trying to figure out why my C code is outputting "exited, segmentation fault". The game is supposed to generate 4 random numbers and store them in the "secret" array. Then the user is suppose to guess the secret code. The program also calculates the score of the user's guess. For now, I printed out the random secret code that has been generated, but when the game continues, it will output "exited, segmentation fault". Also, the GetSecretCode function has...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT