Question: Requirement: Improve your MatLab program GuessNumber.m. After the improvement, your program can allow the user (i.e., the person who run the program) to try different
Requirement: Improve your MatLab program GuessNumber.m. After the improvement, your program can allow the user (i.e., the person who run the program) to try different guesses to approach and finally reach the randomly generated integer. For each guess, the program prints out a message, as described in the instruction for step 1. Based on the message, the user can makeadjustment and then provide a guess closer to the integer. The program counts the number of guesses the user makes. When the user makes a guess matching the integer, the program shows the number of times the user has tried, as well as the integer.
MY CODE:
%rand function below will select a random number num=randi(1000);
%Prompting user for input fprintf('I have a number between 1 and 1000. '); guess=input('Guess what it is: '); if guess>num if (G-num)>100 fprintf('Too large. ');
elseif and((guess-num)>10, (guess-num)<=100) fprintf('Close. Make it smaller. ');
elseif (guess-num)<=10 fprintf('Very close now. Make it a little smaller. ');
end
elseif num>guess if (num-guess)>100 fprintf('Too Small. ');
elseif and((num-guess)>10, (num-guess)<100) fprintf('Close. Make it larger. ');
elseif (num-guess) <10 fprintf('Very close now. Make it a little larger. ');
end
%displays if use made correct guess else fprintf('Yes! You guessed it. '); end
fprintf('My number is: %.2f ',num);
Instruction 1) Program The major improvement is to put the main body of the program for step 1 into a loop, such that the user can try different guesses. Specifically, you need to solve the following problems in the new program: 1. What operations in the existing program for step 1 should be included in the loop? The operations need to be repeated basically include: 1) asking the user for an input, and 2) comparing the input with the integer and displaying the corresponding message. The code corresponding to these operations should be included inside the loop. 2. How to control the loop, such that the loop terminates when the user makes a guess matching the integer? In your program, you can use a flag to denote whether the user has made a guess matching the integer or not. Thus, the program checks the flag to determine whether the loop should terminate or not. Specifically, the loop continues as long as the flag is false, and the flag is set to true when a guess matches the integer, 3. How to count the number of guesses. You maintain a counter in your program and increase the counter by 1 every time when the user makes a guess.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
