:::::::Here is the program that I have right now:::::: to be honnest i do not kn
ID: 3823485 • Letter: #
Question
:::::::Here is the program that I have right now:::::: to be honnest i do not know where to start when it comes to writting a loop. my current program is in if and elseif construct mode. ::::::the purpose of the program is to guess a number between 1 and 1000 and the computer to prompt you to get it closer and closer to the number the computer guessed.
A=randi(1000);
fprintf('I have a number between 1 and 1000. ');
G=input('guess a number between 1 and 1000 and enter it:');
if G>A
if(G-A)>100;
fprintf('Too large. '); %good
elseif and((G-A)>10,(G-A)<=100);
fprintf('close. make it smaller. ');
elseif (G-A)<=10
fprintf('very close now. make it a little smaller. ');
end
elseif A>G
if(A-G)>100;
fprintf('Too small. '); %good
elseif and(A-G>10,(A-G)<=100);
fprintf('close. make it larger. ');
elseif (A-G)<10;
fprintf('very close. make it a little larger. ');%good
end
else
fprintf('Yes, you guessed it. ');
end
fprintf('Random interger A is: %.2f ',A);
::::::::::here is what i the answer is suppose to look like::::::::
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 The program skeleton is as follows. Note that some lines are pseudo-code and ellipses represent unfinished parts. You need to replace them with real code. %Generate a number between 1 and 1000. Call functions rand and ceil. number rintf I have a number between 1 and 1000 n); counter 0; finish false while guess input(...) counter if fprintf Too big. n); elseif else fprintf 'Yes! You guessed the correct number in %d shots. n',...); finish end end fprintf 'My number is %d.n',Explanation / Answer
Here is the code for you:
%Generate a number between 1 and 1000. Call functions rand and ceil.
number = randi(1000);
fprintf('I have a number between 1 and 1000. ');
counter = 0;
finish = false;
while ~finish
guess = input('Guess what it is: ');
counter = counter + 1;
if guess > number
fprintf('Too big. ');
elseif guess < number
fprintf('Too small. ');
else
fprintf('Yes! You guessed the correct number in %d shots. ', counter);
end
end
fprintf('My number is %d. ', number);
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.