Question
help please
USING MATLAB
Write a function called mathgame.m that repeatedly asks a user to answer a simple addition question. After each question the user is told if their answer is correct or incorrect and they are given the opportunity to continue or to stop. When they stop a summary of their results for the game is provided. Tackle this problem in small pieces. The following is a suggested strategy but certainly not the only one: Create two random integers between 1 and 10 and store them in separate variables. Compute the sum of the two integers and store the result in another variable. Output the question so the user can see the two random numbers but can not see the result of the sum operation. Prompt the user to enter their guess and collect it using the input function. Determine if the user's guess matches the result that you have stored for the sum of the numbers. If they match print out 'correct'. If they don't print out 'incorrect'. If the above steps are working, wrap them in a while loop. Make sure you loop condition is initially set to true or your program won't run. The last line of your loop should be to ask the user if they want another question. If they enter 'y' the loop repeats. Anything else and the loop ends. Finally, use variables to keep track of the number of questions, the number correct and the number incorrect. Display this information as shown in the output below. >> mathgame() Welcome to the math game! 1: 3 + 4 = 7 Correct! Again? n Correct: 1 (100.00 %), Incorrect 0 (0.00 %)>> >> mathgame Welcome to the math game! 1: 5 + 3 = 8 Correct! Again? y 2: 9 + 2 = 11 Correct! Again? y 3: 3 + 2 = 5 Correct! Again? y
Explanation / Answer
function []=mathgame()
int flag;
flag=1;
count=0;
correct=0;
while(flag==1)
a=10*randi(1);
b=10*randi(1);
x=[num2str(a),'+',num2str(b)];
disp(x)
c=input(x);
count=count+1;
if c==(a+b)
disp('correct!')
correct=correct+1;
else
disp('incorrect')
end
d=input('again ? ');
if d=='y'
continue;
else
count=count+1;
flag=0;
end
end
g=correct/count;
h=1-g;
g=g*100;
h=h*100;
y=['correct : ',num2str(correct),'(',num2str(g),'% ) , incorrect : ',num2str(count-correct),'(',num2str(h),'% )'];
disp(y)
end