Programming in C Write a program that will guess an integer

Programming in C:

Write a program that will guess an integer that the user has picked. Imagine that the user will write down a positive integer x on a piece of paper and your program will repeatedly ask questions in order to guess what x is, and the user replies honestly. Your program will start by asking for an int n, and you must have 1 x n. After that, the program will successively guess what x is, and the user must tell the computer if x is equal to the guess (entering ’e’), larger than the guess (entering ’l’), or smaller than the guess (entering ’s’). Your program will guess by maintaining a lower bound (initially 1) and upper bound (initially n) and pick the largest integer equal to or smaller than1 the midpoint of the lower bound and upper bound. If the user responds with ’l’ indicating that x is larger, the guess becomes the new lower bound plus one. If the user responds with ’s’ indicating that x is smaller, the guess becomes the new upper bound minus one. If the user responds with ’e’ indicating that x is the guess, your program will report the number of guesses made and terminate execution:

Solution

#include #include #include int main(void) { srand(time(NULL)); int r = rand() % 10 + 1; int correct = 0; int guess; int counter = 0; printf(\"Guess my number! \"); do { scanf(\"%d\", &guess); if (guess == r) { counter++; printf(\"You guessed correctly in %d tries! Congratulations!\ \", counter); correct = 1; } if (guess < r) { counter++; printf(\"Your guess is too low. Guess again. \"); } if (guess > r) { counter++; printf(\"Your guess is too high. Guess again. \"); } } while (correct == 0); return 0; }
Programming in C: Write a program that will guess an integer that the user has picked. Imagine that the user will write down a positive integer x on a piece of

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site