Game to guess a number
%% Guess Number Game
while(1)
count=0;
guess=input('Guess a number between 0 to 10 ');
if(R>guess) disp('Your guess is too small')
elseif (R<guess)
disp('Your guess is too large')
elseif (R==guess)
disp('You are correct! It is ');guess
R=floor( rand()*10 );
end
count=count+1;
if(count==3)
break;
end
end
You need to modify this code:
(1)Player fails the game if you cannot make it within 3 tries.
(2)Player can do this game up to 10 times.
(3)If player input is larger than 10, it will display “Dumb” and immediately stop the game.
The player fails the game if you cannot make it within 3 tries. Players can do this game up to 10 times. If player input is larger than 10, it will display “Dumb” and immediately stop the game.
(Assignment 5b)
As no coding language has been mentioned, I have used MATLAB to solve the problems. These are live scripts and can be edited by working in a live script window.
PART 1
Please find the code and a sample solution attached. The code has been slightly modified to make it more presentable. Please note that in option 3, it is unclear if entering more than 10 will stop just the current round or the complete game. I have coded it to stop the complete game, if otherwise please say so in the comments, I will update the code accordingly.
%%
Guess Number Game
R = floor( rand()*10 );
count = 0; game = 1;
fprintf("Game number %d ~~~~~~~~~~~~~~~~~~~\n",game);
while game <= 10
guess = input('Guess a number between 0 to 10 ');
if guess > 10
disp('Dumb'); game = 11;
break;
elseif R > guess
disp('Your guess is too small');
count = count+1;
elseif R < guess
disp('Your guess is too large');
count = count+1;
elseif R==guess
disp('You are correct! It is ');guess
count = 0; game = game + 1;
R = floor( rand()*10 );
fprintf("Game number %d ~~~~~~~~~~~~~~~~~~~\n",game);
end
if count == 3
disp('Game lost');
game = game + 1; R = floor( rand()*10 );
fprintf("Game number %d ~~~~~~~~~~~~~~~~~~~\n",game);
end
end
SAMPLE OUTPUT
PART 2
Please find the code and a sample solution attached. Please note that the matrix has initially the names and process of 5 products. More can be added or removed as per convenience without changing anything else in the code.
%% Product price code
matrix = {'Milk','$2/L';'Soda','$5';'Coca
Cola','$10';'Fanta','$7';'Water','$3'};
flag = 0;
while flag == 0
in = input('Please enter product name : ');
if ~any(strcmp(matrix(:,1),in)) && ~strcmp('Bye',in)
disp('Item is not in our store. Please retry.');
elseif any(strcmp(matrix(:,1),'Water')) &&
~strcmp('Bye',in)
posn = find(strcmp(matrix(:,1),in));
fprintf("Item price is "+matrix(posn,2)+".\n");
end
if strcmp('Bye',in) == 1
flag = 1;
end
end
SAMPLE OUTPUT*A humble request* - If you have any doubt, please use the comment section to communicate. Please be a little patient, it is an honest promise I will reply as soon as possible. This will clarify your doubt, and also help me to get better at answering your next questions. At the same time, If my answer helped you, please consider leaving an upvote. I hope you understand my viewpoint. Thank you :)
Get Answers For Free
Most questions answered within 1 hours.