- Details
- Parent Category: Programming Assignments' Solutions
We Helped With This MATLAB Programming Homework: Have A Similar One?

Category | Programming |
---|---|
Subject | MATLAB |
Difficulty | Undergraduate |
Status | Solved |
More Info | Write My Matlab Homework |
Short Assignment Requirements
Assignment Description
Mini Project – Guess My Number Game (Step 2)
% Guess a number
clc;
% Generate a number from 1 to 1000
integer=ceil(rand(1)*1000);
% Store the number of guesse made
numGuesses=0;
% Chouse your number
fprintf('I have a number between 1 and 1000 ');
guess=-1;
while (guess~=integer)
% increment the number of guesses
numGuesses=numGuesses+1;
% Get user input
guess=input('Guess what it is: ');
% Tell the use about their guess
if guess>integer
if (guess-integer)>100
fprintf('Too large ', guess);
elseif (guess-integer)>10 & (guess-integer)<=100
fprintf('Close. Make it smaller ', guess);
elseif (guess-integer)<=10
fprintf('Very close now. Make it a little bit smaller ', guess);
end
elseif integer>guess
if (integer-guess)>100
fprintf('Too small. ', guess);
elseif (integer-guess)>10 & (integer-guess)<100
fprintf ('Colose. Make it larger. ', guess);
elseif (integer-guess)<10
fprintf ('Very close now. Make it a little larger. ', guess);
end
else
fprintf('Yes! You guessed the correct number in %d shots. ', numGuesses);
end
end
fprintf('My number is %d. ',integer);
>>GuessNumber
I have a number between 1 and 1000
Guess what it is: 500
Close. Make it smaller
Guess what it is: 250
Too small.
Guess what it is: 300
Too small.
Guess what it is: 400
Colose. Make it larger.
Guess what it is: 450
Colose. Make it larger.
Guess what it is: 475
Colose. Make it larger.
Guess what it is: 490
Very close now. Make it a little bit smaller
Guess what it is: 485
Very close now. Make it a little larger.
Guess what it is: 487
Very close now. Make it a little bit smaller
Guess what it is: 486
Yes! You guessed the correct number in 10 shots.
My number is 486.
Assignment Description
Guess-My-Number Game --- Final Step
Objective:
The purpose of this step of the project is to practice nested loops and gain more experience on loops, particularly how to control the execution flow of loops. Requirement:
Further improve your MatLab program GuessNumber.m. The program in step 2 terminates after the user provides a guess matching the integer. If the user wants to play another round, she must execute the program again. In this step, after the improvement, your program can allow the user to continue playing after each round without re-executing the program. Specifically, after each round, the user is asked whether she wants to play another round or not. If the answer is yes (‘Y’), the program generates another random integer and asks the user to make guesses. Otherwise, the program terminates.
With the program in step 2, the user has to keep guessing until the integer is guessed. With another improvement, the program in this step will allow user to start another round without finishing the current round (i.e., the user has not guessed the integer generated for this round). Specifically, if the user provides a non-positive guess (0 or negative), the program finishes this the existing round and starts a new round. Sample Inputs and Outputs:
>> GuessNumber
I have a number between 1 and 1000. Guess what it is: 500 Too small.
Guess what it is: 750 Too big.
Guess what it is: 625
Very close now. Make it a little bit larger.
Guess what it is: 630
Very close now. Make it a little bit larger.
Guess what it is: 633
Yes! You guessed the integer in 5 shots.
My number is 633.
Do you want to play one more round? (Y/N) Y I have a number between 1 and 1000. Guess what it is: 500 Too big.
Guess what it is: 250 Too big.
Guess what it is: 125 Close. Make it smaller.
Guess what it is: 75 Close. Make it larger.
Guess what it is: 100
Very close now. Make it a little bit smaller.
Guess what it is: 95
Very close now. Make it a little bit larger.
Guess what it is: 97
Very close now. Make it a little bit larger. Guess what it is: 0 My number is 98.
Do you want to play one more round? (Y/N) Y I have a number between 1 and 1000.
Guess what it is: 0
Do you want to play one more round? (Y/N) N
Instruction 1) Program
The existing program in step 2 is to play one round of the game. To allow the user to play more than one round, you need to improve the program and put it into another loop. But, you need to solve the following problems in the new program:
1. How to control the loop, such that the loop continues if the user wants to play another round?
In your program, after each round, it asks the user whether she wants to play another round. Specifically, the program can call input function to allow the user to express her option. In the input call, with a message, you need to instruct the user how to express the option. For example, letter Y means yes; and letter N means no. Or, number 1 means yes, number 0 means no. You can define your way to express yes or no. Then you need to examine the input of the user, and decide whether the loop should continue or not.
2. How to terminate the current round? Consider to use break statement.
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.
Note that the program skeleton is just a suggestion. It is totally OK if you follow other structures or use other methods.
moreround = true; while moreround == true % play one round of the game % break statement is not shown … option = input('Do you want to play one more round? (Y/N) ', 's'); if option … moreround = … end end |
2) Testing
If you have tested your program thoroughly in step 2, you want to test only the new code in this step. To reduce the effort to guess the number in each round, you can first test whether your program can finish a round if you provide a non-positive guess. If this part works, you can finish one round quickly. After one round, you should be able to move to another round. If you cannot continue, fix the problem. Keep doing a few rounds and then choose to stop. Your program should terminates.
3) Submission and Grading
You need to submit TWO files. One file is your program. The other is a text (.txt) file that contains the commands you used to run your program and the corresponding outputs of your program to show that your program works correctly. You can copy from MatLab command window and paste into the text file. We will review these two files to see if your program really works as required. You will get 25 points for your program and another 15 points for submitting the text file.