PSYCH 20A (Prof. Frane) – Winter 2022 – Homework 4
IMPORTANT: Upload your submission as a Matlab script (a .m file) that includes your name in the filename.
Remember, there should be no spaces or other punctuation in the filename (underscores are okay). In your
program, suppress computational output to the command window using semicolons. Use comments throughout
your program. And be sure to use the exact variable names specified in the instructions, with the same
capitalization, so that your work will be scored correctly.
In this script, the user is presented with either an addition test or a multiplication test (the user chooses which
test to take) and then receives their score. There are 8 questions in the test.
• Begin your script with opening comments.
Then clear the variables from the workspace.
And clear the command window.
• Define questionValues as the following 8 × 2 matrix, in which each row contains the numbers used
in a given question:
4 8
3 9
7 7
5 6
9 4
8 7
6 3
4 5
• Define numQuestions as the number of questions. Don’t hard-code this as 8. Have Matlab determine
it from the questionValues vector.
• Define testType as the user’s response to the following prompt (put line-breaks at the beginning and
end of the prompt):
Type “a” to practice addition or “m” to practice multiplication.
• If the response is not “a”, “A”, “m”, or “M”, keep re-prompting the user until a valid response is given.
• Use a for-loop to ask the questions. For example, if testType is ‘a’ or ‘A’, the prompt for the first
question should be:
What is 4 plus 8?
If testType is ‘m’ or ‘M’, the prompt for the first question should be:
What is 4 times 8?
Put a line-break at the beginning and end of the prompt.
• Responses should be stored in an 8 × 1 cell array called responses. The responses should be inputted
as character arrays so that the user can’t “cheat” by, for example, typing 4+8 instead of 12 as their
response to “What is 4 plus 8?”.
• After each question, display ‘Correct!’ if the answer is correct, or ‘Incorrect!’ if the answer is incorrect.
You may consider nonsensical answers (such as empty or non-numeric responses) as incorrect.
• If the user types the word exit (using any capitalization) in response to any question, display the
following message (with a line-break at the beginning and end), and then abort the program immediately
without asking any more questions and without reporting the number of correct answers:
Test aborted.
• Otherwise, after the last question, report the user’s score in the command window as the number correct
out of numQuestions. For example, if the user got 7 answers correct, the report should say:
You got 7 out of 8 answers correct.