Create a function that will accept three scalar values representing a minimum an
ID: 3630963 • Letter: C
Question
Create a function that will accept three scalar values representing a minimum and maximum number and the user’s choice. Generate a random whole number ,”guess” in the range from the minimum and maximum number (including minimum and maximum) and compare this “guess” to the user’s choice. Repeat the generation of “guesses” until the “guess” is equal to the user’s choice. Count how many “guesses” were generated and return (send back) this count to the function call.In the script file, prompt the user to enter whole numbers for the minimum and maximum values. Use a loop to confirm that the minimum number is less than the maximum number and prompt the user to re- enter the minimum and maximum numbers if not (you may assume the user enters whole numbers). Prompt the user to enter a whole number for user’s choice. Use a loop to confirm the user’s choice is between minimum and maximum (may be minimum or maximum as well). Call the function. Then output the number of “guesses” used.
Explanation / Answer
min=ceil(input('Enter minimum value'))
max=ceil(input('Enter maximum value'))
function [gcount] = GuessNumber(minimum,maximum)
gCount=0
generatNum = rand(1,1)*maximum+minimum;
fprintf(' %d',generatNum);
guess=input('Enter guess number')
if (guess==generatNum)
fprintf('First Guess Correct!!');
else
while( guess>generatNum || guess<generatNum)
gcount=gcount+1
fprintf(' Guess Again');
guess=input(' Enter guess number')
end
fprintf('Took number of guesses to count%d',gCount)
end
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.