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
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...
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...
JAVA ASSIGNMENT 1. Write program that opens the file and process its contents. Each lines in...
JAVA ASSIGNMENT 1. Write program that opens the file and process its contents. Each lines in the file contains seven numbers,which are the sales number for one week. The numbers are separated by comma.The following line is an example from the file 2541.36,2965.88,1965.32,1845.23,7021.11,9652.74,1469.36. The program should display the following: . The total sales for each week . The average daily sales for each week . The total sales for all of the weeks .The average weekly sales .The week number...
1.) True or False? For all societies, resources are scarce, and technology is limited, while people’s...
1.) True or False? For all societies, resources are scarce, and technology is limited, while people’s wants and needs for goods and services seem to be unlimited. (2 points) 2.) (1 point) Adam Smith’s “invisible hand” refers to a.) the subtle and often hidden methods that businesses use to profit at consumers’ expense. b.) the ability of free markets to reach desirable outcomes, despite the self-interest of market participants. c.) the ability of government regulations to benefit consumers, even if...
Please answer the following Case analysis questions 1-How is New Balance performing compared to its primary...
Please answer the following Case analysis questions 1-How is New Balance performing compared to its primary rivals? How will the acquisition of Reebok by Adidas impact the structure of the athletic shoe industry? Is this likely to be favorable or unfavorable for New Balance? 2- What issues does New Balance management need to address? 3-What recommendations would you make to New Balance Management? What does New Balance need to do to continue to be successful? Should management continue to invest...
Please read the article and answear about questions. Determining the Value of the Business After you...
Please read the article and answear about questions. Determining the Value of the Business After you have completed a thorough and exacting investigation, you need to analyze all the infor- mation you have gathered. This is the time to consult with your business, financial, and legal advis- ers to arrive at an estimate of the value of the business. Outside advisers are impartial and are more likely to see the bad things about the business than are you. You should...
Sign In INNOVATION Deep Change: How Operational Innovation Can Transform Your Company by Michael Hammer From...
Sign In INNOVATION Deep Change: How Operational Innovation Can Transform Your Company by Michael Hammer From the April 2004 Issue Save Share 8.95 In 1991, Progressive Insurance, an automobile insurer based in Mayfield Village, Ohio, had approximately $1.3 billion in sales. By 2002, that figure had grown to $9.5 billion. What fashionable strategies did Progressive employ to achieve sevenfold growth in just over a decade? Was it positioned in a high-growth industry? Hardly. Auto insurance is a mature, 100-year-old industry...