234k views
3 votes
Row array userGuess contains a sequence of user guesses. Assign correctGuess with true when myNumber is equal to the user guess.

User Olegflo
by
4.4k points

2 Answers

1 vote

Answer:

The MATLAB code is given below with appropriate comments

Step-by-step explanation:

%Define the function.

function correctGuess = EvaluateGuesses(myNumber,userGuess)

%Compute the length.

l = length(userGuess);

%Create an array.

correctGuess = zeros(1, l);

%Begin the loop.

for k = 1: l

%Check the condition.

if myNumber == userGuess(k)

%Update the array correctGuess.

correctGuess(k) = 1;

%End of if.

end

%End of the function.

end

%Call the function.

EvaluateGuesses(3, [3, 4, 5])

User Spacetyper
by
4.0k points
6 votes

Answer:

correctGuess = (myNumber == userGuess);

Step-by-step explanation:

All you have to do is use the logical operator "==" to signify a relationship between myNumber and userGuess. Whenever the value of myNumber is equal to userGuess, MATLAB will input a "1" into the logical array. It will input a "0" when myNumber does NOT equal userGuess.

Example problem for EvaluateGuesses(3, [3, 4, 5])

correctGuess = (myNumber == userGuess);

The program checks for when 3 is equal to any number in the array userGuess. 3 only equals the first value, so the result is [1, 0, 0].

Therefore, the answer is correctGuess = (myNumber == userGuess);

(This is much simpler than what the other user said... this is most likely what your homework expects you to answer with.)

User Diazdeteran
by
3.9k points