English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

"Write a program that plays a guessing game where the computer tries to guess a number picked by the user. The program asks the user to think of a secret number, and then asks the user a sequence of guesses. After each guess, the user must report if it is too high or too low or correct. The program should count the guesses. (Hint: Maintain HighestPossible and LowestPossible variables, and always guess midway between the two. This is called a binary search.) The program output should look similar to:

Think of a number between 1 and 100 and press any key.
Is the number 50 (Correct, Low, High)? _h_
Is the number 25 (Correct, Low, High)? _h_
Is the number 13 (Correct, Low, High)? _l_
Is the number 19 (Correct, Low, High)? _c_
Number of guesses: 4
"

Underlined material is user input

2006-10-20 17:35:14 · 2 answers · asked by Anonymous in Education & Reference Trivia

programming in c++ i guess

2006-10-20 18:50:03 · update #1

2 answers

I don't know what language you're using, so I'll write pseudocode.

HighestPossible=100
LowestPossible=1
print('Think of a number between ' LowestPossible ' and ' HighestPossible ' and press any key.')
correct=false
guesses=0
while correct=false do

midpoint=(LowestPossible + HighestPossible)/2
print('Is the number ' midpoint ' (Correct, Low, High)?')
read(guess)
guesses=guesses+1
if guess='h' then
HighestPossible=midpoint
elseif guess='l' then
LowestPossible=midpoint
else
correct=true
end if

end while
print('Number of guesses: ' guesses)

2006-10-20 17:51:08 · answer #1 · answered by James L 5 · 1 0

You're hurting my brain!

2006-10-21 00:38:32 · answer #2 · answered by sweet ivy lyn 5 · 0 0

fedest.com, questions and answers