In: Electrical Engineering
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.
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