Question

Game to guess a number %% Guess Number Game while(1) count=0; guess=input('Guess a number between 0...

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)

  • First, create a matrix that stores the name of products and prices. 1st and 2nd column should be the product name and its price, respectively. The number of product items should be flexible.
  • Your program will ask to input product name, and it should display the price by looking at the matrix you defined. If the input product name is not on the list, it should display “Item is not in our store. Please retry.”
  • This query should be repeated until you type in “Bye”.

Homework Answers

Answer #1

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 :)

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
PYTHON Exercise 5. Guess the number 1. Develop a “Guess the number” game. Write a program...
PYTHON Exercise 5. Guess the number 1. Develop a “Guess the number” game. Write a program that comes up with a random number and the player has to guess it. The program output can be like this: I am thinking of a number between 1 and 20. Take a guess. 10 Your guess is too low. Take a guess. 15 Your guess is too low. Take a guess. 17 Your guess is too high. Take a guess. 16 Good job!...
Goal: Write a simple number guessing game in java. The game picks a number between bounds...
Goal: Write a simple number guessing game in java. The game picks a number between bounds input by the user, then user has to guess the number. The game will tell you if your guess is too high or too low. When you guess it, the game will tell you how many guesses it took Run multiple games and print statistics (# games, # guesses, avg) when all done. Sample Run: G U E S S I N G G...
Write a c++ program that has the user guess a number between 1 and 100, you...
Write a c++ program that has the user guess a number between 1 and 100, you program should output correct guess if the user guess the right number, too high if they guess a number higher, too low if the guess a number lower and handle invalid input
JAVA Write a number guessing game that will generate a random number between 1and 20 and...
JAVA Write a number guessing game that will generate a random number between 1and 20 and ask the user to guess that number. The application should start by telling the user what the app does. You should then create the random number and prompt the user to guess it. You need to limit the number of times that the user can guess to 10 times. If the user guesses the number, display some message that tell them that they did...
Lottery The lottery game matches three different integer numbers between 1 and 10. Winning depends on...
Lottery The lottery game matches three different integer numbers between 1 and 10. Winning depends on how many matching numbers are provided by a player. The player provides three different integers between 1 and 10. If there is a match of all 3 numbers, the winning $ 1000. If there is a match with 2 numbers, the winning $ 10. If there is a match with 1 number, the winning $ 1. With no match, the winning is $0. Write...
For a C program hangman game: Create the function int setup_game [int setup_game ( Game *g,...
For a C program hangman game: Create the function int setup_game [int setup_game ( Game *g, char wordlist[][MAX_WORD_LENGTH], int numwords)] for a C program hangman game. (The existing code for other functions and the program is below, along with what the function needs to do) What int setup_game needs to do setup_game() does exactly what the name suggests. It sets up a new game of hangman. This means that it picks a random word from the supplied wordlist array and...
Code Example 8-1 1. int count = 1; 2. int item_total = 0; 3. int item...
Code Example 8-1 1. int count = 1; 2. int item_total = 0; 3. int item = 0; 4. while (count < 4) { 5.      cout << "Enter item cost: "; 6.      cin >> item; 7.      item_total += item; 8.      ++count; 9. } 10. int average_cost = round(item_total / count); 11. cout << "Total cost: " << item_total << "\nAverage cost: " << average_cost; (Refer to Code Example 8-1.) If the user enters 5, 10, and 15 at the prompts, the output is: Total...
You are coding a simple game called Pig. Players take turns rolling a die. The die...
You are coding a simple game called Pig. Players take turns rolling a die. The die determines how many points they get. You may get points each turn your roll (turn points), you also have points for the entire game (grand points). The first player with 100 grand points is the winner. The rules are as follows: Each turn, the active player faces a decision (roll or hold): Roll the die. If it’s is a: 1: You lose your turn,...
Python Blackjack Game Here are some special cases to consider. If the Dealer goes over 21,...
Python Blackjack Game Here are some special cases to consider. If the Dealer goes over 21, all players who are still standing win. But the players that are not standing have already lost. If the Dealer does not go over 21 but stands on say 19 points then all players having points greater than 19 win. All players having points less than 19 lose. All players having points equal to 19 tie. The program should stop asking to hit if...
Part A. Input Validation (Name your C program yourLastName_yourFirstName_Lab4a.c) 1. Place the code you developed in...
Part A. Input Validation (Name your C program yourLastName_yourFirstName_Lab4a.c) 1. Place the code you developed in Lab 2 to obtain a diameter value from the user and compute the volume of a sphere (we assumed that to be the shape of a balloon) in a new program, and implement the following restriction on the user’s input: the user should enter a value for the diameter which is at least 8 inches but not larger than 60 inches. Using an if-else...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT