Question

In: Electrical Engineering

In MATLAB, the script should use both for and while loops. Game Mode: Randomly generates 10...

In MATLAB, the script should use both for and while loops.

Game Mode: Randomly generates 10 unique (e.g. non-repeating) multiplication problems that involve multiplying the integer selected in the previous step by an integer value between 2 and 12, inclusive, after which the following report should be displayed in the Command Window.

You correctly answered __ out of __ problems (__%) involving __s.

The number of correctly answered problems appears in the first blank, the total number of problems appears in the second blank, the percentages of problems answered correctly appears in the third blank, and the integer the user selected in step 1 appears in the last blank.

-- After completing a round of the game, the user should have the option to play another round or quit the game.

Solutions

Expert Solution

clear all;clc; % it will clear the work space and commnd window

while 1 %
% prompt the user for input the 1st integer to multiply
int1 = input('Enter the intiger to multiply : ');
% generate 10 unique intigers in a vector using randperm.
% randperm(n,10) generates 10 unique random numbers from 1 to n, so i
% added +1 to make it to 2 to 12, inclusive
Vec = 1+randperm(11,10);
count = 0; % the count for correct answers
N = 10; % total number of problems
for i = 1:N
int2 = Vec(i); % get the 2nd integer from the generated vector
fprintf('\nMultiply %d x %d : ',int1,int2);
int3 = input('Answer = ');
if int3 == int1*int2 % verifying wether the entered answer is correct or not
count = count+1; % if it is correct the counter incriments by 1
end
end
Percent = count*100/N; % calculating the percentage of correct answers
% printing the result of the round
fprintf('You correctly answered %d out of %d problems ( %d%% ) involving %ds',count, N, Percent,int1)
  
% asking the user for another round
Repeat = input('\nDo you want to play another round or quit - Y/N :','s');
if isequal(lower(Repeat),'y')
continue
else break
end
end


Related Solutions

Can you create a player vs computer Hangman game on MATLAB using nested loops, for loops,...
Can you create a player vs computer Hangman game on MATLAB using nested loops, for loops, if loops, while loops, arrays and conditional execution, as well as creating an image of the game board. The list below must be considered in the code. - Selecting a word from a dictionary (a text file) - Reading a single letter from the user - Building a character array showing the letters matched so far - Keeping count of the number of guessed...
Matlab script that uses loops to create a multiplication table (matrix) where the user enters 'n'...
Matlab script that uses loops to create a multiplication table (matrix) where the user enters 'n' (nxn chart).
using matlab Write a script that simulates a card game that works as follows: A dealer...
using matlab Write a script that simulates a card game that works as follows: A dealer places 5 cards face down on the table and flips the first card. The player goes down the line, one at a time, and guesses if the next card is higher or lower than the card displayed, and then the next card is revealed. In the end, the player is awarded a point for each correct guess. In terms of coding, your script should...
In MATLAB FOR loops: Repeat a block command a certain number of times Use a loop...
In MATLAB FOR loops: Repeat a block command a certain number of times Use a loop index Can use a non-unit stride Are required for any kind of animations An accumulator: MUST start at zero Can be used to keep track of a quantity each time through the loop Can be used to calculate how a sum changes with each loop Should be coded, for loops are more efficient For loops: Perform a set task a set number of times...
This assignment will acquaint you with the use of while loops and boolean expressions. You will...
This assignment will acquaint you with the use of while loops and boolean expressions. You will create a program that acts as a score keeper of a racquet ball game between two players. The program will continually ask the user who the winner of every point is as the game is played. It will maintain the scores and declare the match is over, using these rules: (1) A game is over when a. one of the players wins 7 points...
2. Vector Element Calculation Write a MATLAB script which will randomly generate a 20-element vector with...
2. Vector Element Calculation Write a MATLAB script which will randomly generate a 20-element vector with integer values ranging between −100 to +100. Using LOOP command, determine the number of negative elements in the vector. For example, given A = [15 −6 0 -8 −2 5 4 −10 -3 −5], the number of negative numbers are 6.
Use MATLAB to create a script which solves for problem 5.9 in the book (5.11 in...
Use MATLAB to create a script which solves for problem 5.9 in the book (5.11 in the 4th edition). Given are the equations for the moment, as a function of x, starting from the leftmost side of the beam with x=0 and ending at the other end of the beam with x=12. This piecewise function together makes up the moment equation for the beam given. 0 ≤ ? ≤ 3 ?(?) = 265? − 5.56?3 , 3 < ? ≤...
Using MATLAB or Octave, use documenting code to Write a script that prompts the user for...
Using MATLAB or Octave, use documenting code to Write a script that prompts the user for a minimum and maximum real number, then generates and prints a random number in the requested range. The script should then do the same for an integer range. Sample output: Enter a minimum real value: 0.5 Enter a maximum real value: 30 A random number in the range ( 0.5000, 30.0000 ) is 1.7851 Enter a minimum integer value: -10 Enter a maximum integer...
In the given instruction, I am to use while loops only. The goal is to prompt...
In the given instruction, I am to use while loops only. The goal is to prompt the user to select an acceptable input. If the wrong input is given, the program forces the user to select an appropriate input. The program also keeps running until the user chooses to exist by selecting a very specific input which in my case is the upper or lower case "E". The problem is even after selecting upper or lower case "E", my program...
Develop a matlab script file to calculate the practical single phase transformer parameters. The program should...
Develop a matlab script file to calculate the practical single phase transformer parameters. The program should ask the user to provide the following individually: S_rated V1_rated V2_rated With secondary shorted P1 V1 With primary open P2 I2 The program should calculate Req Xeq Zeq Gc Ym Bm Make sure your code will pronmpt a message like: “Enter the real power at the primary winding when the secondary winding is shorted, P1=). Do the same for all parameters S, V1_rated, V2_rated,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT