Write a program that will guess an integer that the user


PROGRAMING ON 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:

Example 1)

Enter n: 50

Is your number 25? l

Is your number 38?

l Is your number 44? s

Is your number 41? e

Your number must be 41. I used 4 guesses.

Example 2)

Enter n: 9

Is your number 5? s

Is your number 2?

l Is your number 3? s

Error: that's not possible.

Example 3)

Enter n: -2

Error: n must be positive.

Example 4)

Enter n: 9

Is your number 5? m

Error: invalid input.

Example 5)

Enter n: a

Error: invalid input.

Request for Solution File

Ask an Expert for Answer!!
Computer Engineering: Write a program that will guess an integer that the user
Reference No:- TGS02900787

Expected delivery within 24 Hours