Question

In Matlab Create a single script (.m file) to solve these problems. Unless directed otherwise, use...

In Matlab

Create a single script (.m file) to solve these problems. Unless directed otherwise, use meaningful variable names for each variable; do not use the default variable ans to store your results. For this project, suppress your output with semi-colons (;). Each problem should be in a separate cell, using the cell mode feature of MATLAB.

Problem 4

Video games are rather complicated to program, not least of which because of the graphics work that needs to be completed to finish a video game. Still, even relatively simple games have historically had a chance of becoming popular (e.g. Tetris®). Since you are learning to program for the first time, let's look at a text-only game.

Write a program that has the computer generate a pseudorandom integer between -100 and +100, and ask the user to guess what the number is. To generate the pseudorandom number, research and use the randi command. If the user's guess is higher than the computer-generated pseudorandom number, print a statement to that effect. If the user's guess is lower than the computer-generated pseudorandom number, print a statement to that effect. Keep track of how many guesses it takes for the user to guess the right number, and print that information to the screen when the program terminates. Do not re-initialize the computer-generated pseudorandom number between iterations, otherwise the user will have a hard time trying to guess the computer-generated pseudorandom number.

Validate the user's input; if the user enters a number greater than 100 or less than -100, prompt the user to enter a number within the guess range. Do not count guesses out of range (i.e. greater than 100 or less than -100) as an iteration. Do not concern yourself with testing if the user entered non-numeric input, and assume that the user will enter an integer value. If the user enters the value inf, terminate the program (though count that iteration as a valid iteration for the purposes of seeing how many times the program iterated).

Use no more than two while loops to solve this problem, and emulate the output format in these two sample runs:

Sample Run #1 (with a computer-generated value of 9):

Enter your guess: 8
Sorry, your guess was too low. Please try again.

Enter your guess: 10
Sorry, your guess was too high. Please try again.

Enter your guess: 9

You guessed the correct value!

The correct value was 9.
The program iterated 3 times.


Sample Run #2 (with a computer-generated value of -3):

Enter your guess: 100
Sorry, your guess was too high. Please try again.

Enter your guess: -100
Sorry, your guess was too low. Please try again.

Enter your guess: inf

You asked to terminate the program.

The correct value was -3.
The program iterated 3 times.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code:

clear all;
clc;

upperLimit = 100;
lowerLimit = -100;
n = randi([lowerLimit,upperLimit]);

iterations = 0;

while(1)
iterations++;
inp = input('Enter your guess:');
if inp==inf
disp('You asked to terminate the program.');
break
elseif inp > upperLimit || inp < lowerLimit
fprintf('Please enter the number within the range [%d, %d], try again.\n', lowerLimit, upperLimit);
elseif inp>n
disp('Sorry, your guess was too high. Please try again.');
elseif inp<n
disp('Sorry, your guess was too low. Please try again.');
else
disp('You guessed the correct value!');
break
end
end

fprintf('The correct value was %d.\n', n);
fprintf('The program iterated %d times.\n',iterations);

Code:

HomeworkLib_rand_game.m clear all; clc; 1 2 3 4 5 6 upper Limit = 100; lowerLimit = -100; n = randi([lowerLimit, upper Limit]); . 8

Results:

Workspace Filter Name ans inp iterations lowerLimit n upperLimit A Class double double double double double double Command WiWorkspace Filter Name ans inp iterations lowerLimit |n upperLimit A Class double double double double double double Command WWorkspace Filter 0 Name ans inp iterations lowerLimit n upperLimit A Class double double double double double double Command

Feel free to ask if you have any queries, Thanks.

Add a comment
Know the answer?
Add Answer to:
In Matlab Create a single script (.m file) to solve these problems. Unless directed otherwise, use...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Solve Question using While loops-MATLAB Write a program that generates a random number and asks the...

    Solve Question using While loops-MATLAB Write a program that generates a random number and asks the user to guess what the number is. If the user's guess is higher than the random number, the program should display "Too high, try again." If the user's guess is lower than the random number, the program should display "Too low, try again." The program should use a loop that repeats until the user correctly guesses the random number.

  • Hello! we are using Python to write this program. we are supposed to use loops in...

    Hello! we are using Python to write this program. we are supposed to use loops in this assignment. I would greatly appreciate the help! Thank you! Write a program that allows the user to play a guessing game. The game will choose a "secret number", a positive integer less than 10000. The user has 10 tries to guess the number. Requirements: we would have the program select a random number as the "secret number". However, for the purpose of testing...

  • In c# create a program that generates a random number from 1 to 1000. Then, ask...

    In c# create a program that generates a random number from 1 to 1000. Then, ask the user to guess the random number. If the user's guess is too high, then the program should print "Too High, Try again". If the user's guess is too low, then the program should print "Too low, Try again". Use a loop to allow the user to keep entering guesses until they guess the random number correctly. Once they figure it, congratulate the user....

  • Can someone upload a picture of the code in matlab Write a "Guess My Number Game"...

    Can someone upload a picture of the code in matlab Write a "Guess My Number Game" program. The program generates a random integer in a specified range, and the user (the player) has to guess the number. The program allows the use to play as many times as he/she would like; at the conclusion of each game, the program asks whether the player wants to play again The basic algorithm is: 1. The program starts by printing instructions on the...

  • Create a single script (.m file) to complete this assignment. Unless directed otherwise, use meaningful variable...

    Create a single script (.m file) to complete this assignment. Unless directed otherwise, use meaningful variable names for each variable: do not use the default variable ans to store your results. For this assignment do not suppress your output with semi- colons (). Each problem (ie. Problem 1. Problem 2, and Problem 4) should be in a separate cell, using the cell mode feature of MATLAB; the subparts should all be contained in the same cell as the parent problem....

  • Can you add code comments where required throughout this Python Program, Guess My Number Program, and...

    Can you add code comments where required throughout this Python Program, Guess My Number Program, and describe this program as if you were presenting it to the class. What it does and everything step by step. import random def menu(): #function for getting the user input on what he wants to do print("\n\n1. You guess the number\n2. You type a number and see if the computer can guess it\n3. Exit") while True: #using try-except for the choice will handle the...

  • (c++) Write a program that generates a random number between 1 and 100 and asks the...

    (c++) Write a program that generates a random number between 1 and 100 and asks the user to guess what the number is. If the user’s guess is higher than the random number, the program should display “Too high, try again.” If the user’s guess is lower than the random number, the program should display “Too low, try again.” The program should use a loop that repeats until the user correctly guesses the random number. Be sure that your program...

  • Write a Java application program that plays a number guessing game with the user. In the...

    Write a Java application program that plays a number guessing game with the user. In the starter code that you are given to help you begin the project, you will find the following lines of code: Random generator = args.length == 0 ? new Random() :                    new Random(Integer.parseInt(args[0])); int secret = generator.nextInt(100); You must keep these lines of code in your program. If you delete these lines, or if you change thse lines in any way, then your program...

  • Hello Please, don't answer with your handwriting, Thank you.. This question is related to java language...

    Hello Please, don't answer with your handwriting, Thank you.. This question is related to java language Write a full Java program that: 1. Generate an integer random number between 0 and 6. 2. Then make a loop that keeps asking the user to guess the generated random number. If the user input is not equal to the generated random number, then print, "Try again" and allow the user to input another guess until the user guess correctly 3. If the...

  • C++ Guessing game. Create a project titled Lab4_Guess with a single file titled guess.cpp Program the...

    C++ Guessing game. Create a project titled Lab4_Guess with a single file titled guess.cpp Program the following game. The computer selects a random number between 1 and 100 and asks the user to guess the number. If the user guesses incorrectly, the computer advises to try larger or smaller number. The guessing repeats until the user guesses the number. The dialog should look as follows (user input is shown in bold): Guess a number between 1 and 100: 50 try...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
Active Questions
ADVERTISEMENT