Question

In: Advanced Math

USE MATLAB Write a program in Matlab that would continuously ask the user for an input...


USE MATLAB

Write a program in Matlab that would continuously ask the user for an input between 1 and 6, which would represent the result of rolling a die. The program would then generate a random integer between 1 and 6 and compare its value to the value entered by user.
If the user’s die is larger, it should display, “Mahahanap mo na ang forever mo. Sana all!”
If the user’s die is smaller, it should display, “Gising na friend, di ka niya mahal!”
If the results are the same, it should display, “Move on na ghorl”.
The program should ask if the user wants to play another game (to be answered by either ‘Y’ or ‘N’). If the user answers ‘Y’, the game continues. If the user answers ‘N’, the game ends and it will display, “Mabuti naman at natauhan ka na!”.
The program should have a main script named lovedicegame.m and calls on the following functions:
a. A function readdice.m to prompt the user and read in the integer input.
b. A function genrandomdice.m to generate a random integer between 1 and 6.
c. A function comparedice.m to compare the input and the generated integer and display the results.
Sample run:
>> lovedicegame
>> Do you want to play the lovedice game (Y/N)? Y
>> Enter your dice: 5
>> My dice result: 6
>> Gising na friend, di ka niya mahal!
>> Play again (Y/N)? Y
>> Enter your dice: 3
>> My dice result: 1
>> Mahahanap mo na ang forever mo. Sana all!
>> Play again (Y/N)? Y
>> Enter your dice: 2
>> My dice result: 2
>> Move on na ghorl!
>> Play again? N
>> Mabuti naman at natauhan ka na!
Run the game for values:
3​,1,​4​,1​,6

Solutions

Expert Solution

Here is the code for lovedicegame.m

clc; clear all;
if input('Do you want to play lovedice game? (Y/N) ', 's') == 'N'
return
end
out = 'Y';
while out == 'Y'
nuser = readdice(); % User input.
ndice = genrandomdice(); % Dice number.
comparedice(nuser, ndice);
  
out = input('Play again (Y/N)? ', 's');
if out == 'N'
disp('Mabuti naman at natauhan ka na!')
end
end

First we ask the player if he/she wants to play, If so, we continue the game, otherwise the code stops. Next we ask for the input and generate the random dice. Later we compare the results and print the corresponding message. Finally, we ask if the player wants to play again.

Here are the codes for readice.m,genrandomdice.m and comparedice.m respectively

function nuser = readdice()
nuser = input('Enter your dice: ');
end

function ndice = genrandomdice()
ndice = randi(6);
disp(['My dice result: ' num2str(ndice)])
end

function comparedice(nuser, ndice)
if nuser > ndice
disp('Mahahanap mo na ang forever mo. Sana all!')
elseif nuser < ndice
disp('Gising na friend, di ka niya mahal!')
else
disp('Move on na ghorl')
end
end

We use the function input('prompt') to ask for an input and the function disp('message') to display a message.

We run the code for the values 3,1,4,1,6 and get the output

Do you want to play lovedice game? (Y/N) Y
Enter your dice: 3
My dice result: 6
Gising na friend, di ka niya mahal!
Play again (Y/N)? Y
Enter your dice: 1
My dice result: 4
Gising na friend, di ka niya mahal!
Play again (Y/N)? Y
Enter your dice: 4
My dice result: 1
Mahahanap mo na ang forever mo. Sana all!
Play again (Y/N)? Y
Enter your dice: 1
My dice result: 6
Gising na friend, di ka niya mahal!
Play again (Y/N)? Y
Enter your dice: 6
My dice result: 6
Move on na ghorl
Play again (Y/N)? N
Mabuti naman at natauhan ka na!


Related Solutions

Write a program that will ask for the user to input a filename of a text...
Write a program that will ask for the user to input a filename of a text file that contains an unknown number of integers. And also an output filename to display results. You will read all of the integers from the input file, and store them in an array. (You may need to read all the values in the file once just to get the total count) Using this array you will find the max number, min number, average value,...
Write a Python program that has a loop to continuously ask the user for a number,...
Write a Python program that has a loop to continuously ask the user for a number, terminating the loop when the number entered is -1. Inside the loop, 1.) display one asterisk(*) if the number is 1, 2.) two asterisk(**) if the number is 2 and 3.) "OTHER" if the number is any other number.
Write a program in C, that uses standard input and output to ask the user to...
Write a program in C, that uses standard input and output to ask the user to enter a sentence of up to 50 characters, the ask the user for a number between 1 & 10. Count the number of characters in the sentence and multiple the number of characters by the input number and print out the answer. Code so far: char sentence[50]; int count = 0; int c; printf("\nEnter a sentence: "); fgets(sentence, 50, stdin); sscanf(sentence, %s;    for(c=0;...
Ask the user to input a series of numbers, write a C# program to output the...
Ask the user to input a series of numbers, write a C# program to output the sum, max, and min. Be sure to do error checking if the user input is not a number.
Python Program Write a program that will ask a user on how many input colored balls...
Python Program Write a program that will ask a user on how many input colored balls of the following codes: R-red, B-blue, W-white, G-green and O-orange -will he or she would like to enter in the program and print the total number of Red balls were encountered. Assume an uppercase and lower case letter will be accepted.
Write java program that will ask for the user for 2 input lines and print out...
Write java program that will ask for the user for 2 input lines and print out all words that occur 1 or more times on both lines (case sensitive). Write this without arrays and method. Here is a sample run: <Output> Enter two lines to process. The quick brown fox jumps over a lazy dog The fox hound outruns the lazy dog The words that occur on both lines are: The fox lazy dog
Write a program to find the prime numbers IN JAVA Ask user to input the integer...
Write a program to find the prime numbers IN JAVA Ask user to input the integer number test the number whether it is a prime number or not Then, print “true” or “false” depending on whether the number is prime or isn’t. Hint: number is prime when is has exactly 2 factors: one and itself. By this definition, number 1 is a special case and is NOT a prime. Use idea of user input, cumulative sum, and loop to solve...
Question: Conversion Program * This program will ask the user to input meters * After input,...
Question: Conversion Program * This program will ask the user to input meters * After input, a menu will be displayed: * 1. Convert to kilometers * 2. Convert to inches * 3. Convert to feet * 4. Quit * * The user will select the conversion and the converted answer will be displayed. * The program should redisplay the menu for another selection. * Quit program when user selects 4. Hi, my instructor requirement fix the bug in the...
Create a small program that contains the following. ask the user to input their name ask...
Create a small program that contains the following. ask the user to input their name ask the user to input three numbers check if their first number is between their second and third numbers
MATLAB QUESTION Write a python program to request a positive float input from the user, x....
MATLAB QUESTION Write a python program to request a positive float input from the user, x. The program should check for the value of x. If x is larger than or equal to 1.0, the program should display the value of x in addition to the string “is larger than or equal to 1” and then terminate the program. If x is less than 1 the program should calculate y such that: y = 1-x+x^2/2-x^3/3+x^4/4-x^5/5……-x^99/99+x^100/100 The program should then display...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT