Info
Questa domanda è chiusa. Riaprila per modificarla o per rispondere.
Sort them in the correct order?
1 visualizzazione (ultimi 30 giorni)
Mostra commenti meno recenti
% if a>b we swap the roles of a and b.
if (a > b)
c = a; a = b; b = c;
end
function computerGuess (a, b)
% function computerGuess (a, b)
% Game : the computer must guess the integer
% in the interval (a,b) which the user is thinking of.
% the computer asks the user to think of a number
disp(['Think of a number in ' num2str(a) ...
' and ' num2str(b)]);
% We keep track of the previous guess. It is initialized as a-1:
old_guess = a-1;
% Takes the integer part of a and b
a = floor(a);
b = ceil (b);
% loops while the computer guesses
% this loop is exited with a break
while (true)
% as a guess, the computer gives the middle of [a,b]
guess = ceil( (a+b)/2 );
if ( guess == old_guess )
% if the old guess is the same, then the computer is nearly there, the answer is:
guess = guess - 1;
end
elseif ( answer == 2 )
% we must keep the lower interval
b = guess;
answer = menu(['The number is ' num2str(guess) ' ? '], ...
'correct!', too high!', too low!');
else
% we must keep the upper interval
a = guess;
end
if ( answer == 1)
disp('I win !');
break;
old_guess = guess;
end
end
3 Commenti
Stephen23
il 4 Lug 2017
@Meenakshi S: do you want to ask us a question, or are you just showing off your code?
Risposte (0)
Questa domanda è chiusa.
Vedere anche
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!