Borrar filtros
Borrar filtros

How to arrange this code in sequence order?

2 visualizaciones (últimos 30 días)
Meenakshi S
Meenakshi S el 14 de Jul. de 2017
Respondida: Jan el 14 de Jul. de 2017
% 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
  1 comentario
John D'Errico
John D'Errico el 14 de Jul. de 2017
Editada: John D'Errico el 14 de Jul. de 2017
What does "sequence order" mean to you?
Do you think there is something wrong with your code? Why? Must we first figure out what you want the code to do? Given that the code apparently does not do what you want, how do we know what you want to do, and what you think is wrong?
If you want help, first make it possible for someone to help you.
Oh. And learn to format your code so it is readable.
https://www.mathworks.com/matlabcentral/answers/13205-tutorial-how-to-format-your-question-with-markup#answer_18099

Iniciar sesión para comentar.

Respuestas (2)

Geoff Hayes
Geoff Hayes el 14 de Jul. de 2017
Meenakshi - do you mean that the code does not work? Note that the first four lines are
% if a>b we swap the roles of a and b.
if (a > b)
c = a; a = b; b = c;
end
where a and b are undefined and (worse) precede the function signature of
function computerGuess (a, b)
Only comments can precede the signature so you have to move these lines into the body of the function? But where? Given what these lines are doing (checking to see if a is greater than b and then swapping the two if true) then this line should be used after the user has selected his or her a and b (for the interval).

Jan
Jan el 14 de Jul. de 2017
This is a strange question. Do you want use to reorder the lines of the code based on guessing what the code should do? This is gun-shot programing. Okay:
Move the first 4 lines:
% if a>b we swap the roles of a and b.
if (a > b)
c = a; a = b; b = c;
end
behind the block:
% Takes the integer part of a and b
a = floor(a);
b = ceil (b);

Categorías

Más información sobre Language Fundamentals en Help Center y File Exchange.

Etiquetas

Aún no se han introducido etiquetas.

Community Treasure Hunt

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

Start Hunting!

Translated by