Info

이 질문은 마감되었습니다. 편집하거나 답변을 올리려면 질문을 다시 여십시오.

Sort them in the correct order?

조회 수: 1 (최근 30일)
Meenakshi S
Meenakshi S 2017년 7월 4일
댓글: Meenakshi S 2017년 7월 14일
% 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
Stephen23
Stephen23 2017년 7월 4일
@Meenakshi S: do you want to ask us a question, or are you just showing off your code?
Meenakshi S
Meenakshi S 2017년 7월 14일
Sir, iam not showing of my code,if you see the code ,the code is not in proper sequence.I cant able to arrange.so iam asking your help

답변 (0개)

이 질문은 마감되었습니다.

태그

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by