Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write a C program that guesses a number. The program should generate a random number from 10-30. The program then prints the following: A random

Write a C program that guesses a number. The program should generate a random number from 10-30. The program then prints the following:

A random number in the range has been generated by the program(Do not print the number). Can you guess what the number is? Please put in your first guess.

The player then types a first guess and the program will respond with one of the following options:

1. Excellent! You got it! Would you like to play again? (yes or no)

2. Too low (wrong). Try again.

3. Too high (wrong). Try again.

If the player's guess is incorrect, the program should loop until the player finally gets the number correct and ask if they would like to play and start a new game. The program should keep telling the player "too high" or "too low" to help the player "zero in" on the correct answer, The searching technique employed in the problem is called binary search.

Pastable code

#include #include

int main() { //generating a random number printf("A random number in the range has been generated by the program. "); printf("Can you guess what the number is? Please put in your first guess. "); int r=10+rand()%20; // finding random number in range of [10,30] int flag=0; int x; while(flag==0) { //loop to keep running till user is playing the game //enter the input scanf("%d",&x); //check is number is low if(xr) printf("Too high(wrong answer). Try again. "); //if user guessed the correct number else(x==r) ; printf("Excellent! You guessed the number correctly! "); //check if user wants to play again then enters 1 for yes and 0 for no printf("Do you want to play again ?(Yes(1)/NO(0)): "); scanf("%d",&x); //if they want to play again generate a new random number; if(x) r=10+rand()%20; else break;

} return 0;

}

This is my code so far but there are errors.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Making Databases Work The Pragmatic Wisdom Of Michael Stonebraker

Authors: Michael L. Brodie

1st Edition

1947487167, 978-1947487161

More Books

Students also viewed these Databases questions