How do you create a vector from user's inputs in while loops?

9 views (last 30 days)
In my assignment I need to make a guess the number game that tells you if your guess was higher or lower then the generated number. As well as tell you if your number is correct with the number of guesses it took to get right. I have all of that and it works but I also need to have code that alerts the user if they had already guessed a number. I'm not sure how to do that.
My code so far:
%Random number generator
magic_number = randi([1,100]);
%User input number
guess = input('Guess a number between 1 and 100: ')
%Counts number of guesses
count=1;
%If the user's guess was not right
while guess~= magic_number
if guess < magic_number
fprintf('The guess was too low.\n\n');
else
fprintf('The guess was too high.\n\n');
end
guess = input('Do no guess a number already guessed. Guess again: ');
count = count + 1;
end
%Final Statements
fprintf('The guess was correct!\n')
fprintf('It took you %0.0f guesses.\n',count)

Answers (2)

Image Analyst
Image Analyst on 8 Feb 2022
You need to create an array to keep track of guessed numbers:
guessedNumbers = []; % Initialize to empty
while guess~= magic_number % Begin the loop
Then inside the loop one way to see if the current guess has been used before is
if sum(guessedNumbers == guess) == 1
% It's been used before so don't add it.
message = sprintf('%d has already been guessed before.\nTry another number', guess);
guessedNumbers % Show guessed numbers in the command window.
uiwait(helpdlg(message));
else
% It's not been used before. Add variable onto the end of the guessedNumbers vector.
guessedNumbers(end+1) = guess;
end

Samuel Nkwindja
Samuel Nkwindja on 8 Feb 2022
One way of doing that is by storing all the values guessed yet in a vector say previousguess for example.You can then use the built in fuction ismember to determine if your guess is part of your previous guesses.
any(ismember(previousguess,guess))
will verify that guess is part of previous guess. The code,
%Random number generator
magic_number = randi([1,100]);
%User input number
guess = input('Guess a number between 1 and 100: ');
%Counts number of guesses
count=1;
previousguess=[];
%If the user's guess was not right
while guess~= magic_number
if any(ismember(previousguess,guess))
fprintf('you already guessed this number! \n\n');
elseif guess < magic_number
fprintf('The guess was too low.\n\n');
else
fprintf('The guess was too high.\n\n');
end
previousguess(count)=guess;
guess = input('Do no guess a number already guessed. Guess again: ');
count = count + 1;
end
%Finial Statements
fprintf('The guess was correct!\n')
fprintf('It took you %0.0f guesses.\n',count)
for faster implementation you should initialise your previousquess vector e.g previousguess=zeros(1,max_number_of_guesses)
Hope this helps!

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!