Question

In: Computer Science

*C PROGRAMMING* Create a program which requires the computer to guess the user's number from 1-100....

*C PROGRAMMING*

Create a program which requires the computer to guess the user's number from 1-100. The user does not need to enter their number at any point. The computer should just guess a random number, and then the user can choose whether or not the number is too high or too low, and if the computer does not guess the correct number, it will will keep guessing and eliminate the values from the range that is either too high or too low (depending on the user's selection after a guess is made).

EXAMPLE OF PROGRAM LOGIC:

num = 38 (user's selection - not actual input)

1 to 100 (high and low variables initialized)

computer guess 50 (guess determined by 1 + 100 / 2)
high (user says the number is too high)

49 (new high value - if high subtract 1 from guess and store in high)
computer guess 25 (guess determined by 1 + 49 / 2)
low (user)

26 (new low value - if low add 1 to guess and store in low)
computer guess 38 (guess determined by 26 + 50 / 2)
Correct! (user)

Solutions

Expert Solution

#include <stdio.h>

int main()
{
char ch;
int num,low=1,high=100,mid,f=0;
  
printf("High and low variables initialized\n");
while(low<high&&f==0)
{
mid=(low+high)/2;
printf("Computer Guess is %d\nenter h-high l-low c-correct:\n",mid);
scanf(" %c",&ch);//enter character based on guessed number high,low,or equal
if(ch=='h')
{
high=mid-1;//If guessed value is high make high=mid-1
printf("high=%d\n",high);
  
}
else if(ch=='l')//if guessed number is low
{
low=mid+1;//make low to mid +1
  
printf("Low=%d\n",low);
}
else if(ch=='c')
{
f=1;//if it is correct break from it
num=mid;
break;
}
  
}
if(f==1)
printf("The number is %d",num);
else
printf("You didn't guessed right number");
return 0;
}


Related Solutions

We will create a program that will guess a number between 1 and 100 chosen by...
We will create a program that will guess a number between 1 and 100 chosen by the user.Your program will be iteratively built in several stages, where each stage focuses on implementing just one type of operation. Each stage will build upon the work from the previous stage. This is known as iterative design, a process that allows us to focus on working on a single, simple task at a time, but the resulting software slowly evolves into something much...
Create a PYTHON program that lets a user guess a number from 1 to 1,000: -i...
Create a PYTHON program that lets a user guess a number from 1 to 1,000: -i used random.randint(1,1000) -must give hints like (pick a lower/higher number) which i already did -tell the user when he has repeated a number (help) -the game only ends when you guess the number (help) -you loose $50 every failed attempt (done) ****I did it as a while loop -
Create a C++ program that generates a random number from 1 to 100 using the rand()...
Create a C++ program that generates a random number from 1 to 100 using the rand() function. Give the user a total 5 chances to guess the number. Stop the program and indicate the user guessed the correct number and should be applauded. Also, please find out which guess was the closest to the actual number. For example, if the rand() produces 57 and the user guessed 10, 80, 52, 33 and 44 in their respective turns then print out...
C++ Programming level 1 using step number 2 is a must Guess the Number Introduction In...
C++ Programming level 1 using step number 2 is a must Guess the Number Introduction In this assignment you will create a program that will simulate a number guessing game. Skills: random, while-loop, Boolean flags Algorithm to win The guess the number game has an algorithm to lead to the winning value. The way it works is say you have the set of numbers 1 to 100. You always begin by choosing the half-way point, then a hint will be...
1. [100] Create a C program for a number guessing game. Here are the requirements: a....
1. [100] Create a C program for a number guessing game. Here are the requirements: a. Your program generates a random number between -100 and 100 and keeps asking the user to guess the number until the user guesses it correctly. Your random number generator should be implemented as a C function which takes min and max values as input parameters and returns a random number between those values including the min and the max. b. If the user guesses...
Write a C program that asks the user to guess a number between 1 and 15(1...
Write a C program that asks the user to guess a number between 1 and 15(1 and 15 are included). The user is given three trials. This is what I have so far. /* Nick Chioma COP 3223 - HW_2 */ #include <iostream> #include <time.h> // needed for time function #include <stdlib.h> // needed for srand, rand functions int main () {       int numbertoguess, num, correct, attempts;       srand(time(NULL)); //this initializes the random seed, making a new...
PYTHON Exercise 5. Guess the number 1. Develop a “Guess the number” game. Write a program...
PYTHON Exercise 5. Guess the number 1. Develop a “Guess the number” game. Write a program that comes up with a random number and the player has to guess it. The program output can be like this: I am thinking of a number between 1 and 20. Take a guess. 10 Your guess is too low. Take a guess. 15 Your guess is too low. Take a guess. 17 Your guess is too high. Take a guess. 16 Good job!...
Develop a C program that generates a random number from 1 to 100, then prompt the...
Develop a C program that generates a random number from 1 to 100, then prompt the user to guess the number until it is guessed correctly. Let the user know if the guess is too high, too low, or correct. The program should count the number of times the user guesses and display the number, along with their rank, after the number is guessed correctly. Rank the user as follows: Super Guesser: 1 to 4 guesses Excellent Guesser: 5 to...
C Programming Please modify this code to display the "guess" value from the calculation and the...
C Programming Please modify this code to display the "guess" value from the calculation and the "iterations" it took to complete. Please also add the input to the program. Input: Program will prompt the user to provide the desired decimal-place accuracy, in the range of [1-15], inclusive. User prompt must clearly indicate what is a valid input. User input must be validated - ensuring that the value entered is an integer in the above range. Sample output for one perfect...
C programming Get a number from the user and write a program that checks to see...
C programming Get a number from the user and write a program that checks to see if the inputted number is equal to x*x+x and x must be greater than 0. For example, if the user inputs 12. The program should check to see if 12 = x*x+x. In this case it is true because 3*3+3 = 12.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT