PSYCH 20A (Prof. Frane) – Spring 2022
PSYCH 20A (Prof. Frane) – Spring 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.
This program gives the user a quiz on state capitals, in alphabetical order by state, and then reports their score.
- Begin your script with opening comments that briefly describe what the program does and include your name and the date.
- Then clear the variables from the workspace.
- Then clear the command window.
- Then input the data from usStates.csv into a table called stateCapitalTable.
- Divide the rest of your script into 4 sections with prominent headings: INPUT NUMBER OF STATES, DEFINE QUIZ ITEMS, GIVE QUIZ, REPORT SCORE.
In the INPUT NUMBER OF STATES section:
- Get keyboard input from the user, using the following prompt (with a space after the colon):
Enter the number of states to include on the quiz:
- That keyboard input should be collected inside a while-loop so that the user is prompted over and over until they give a valid response (an integer between 1 and 50). HINT: Use the ismember function in the while-statement.
In the DEFINE QUIZ ITEMS section:
- Define state as a cell array containing the state names, and define capital as a cell array containing the respective capitals of those states. These cell arrays should only include the requested number of items. For example, if the user requested 5 states to be included on the quiz, then the state and capital vectors should only contain the first 5 states and the first 5 capitals (respectively), extracted from stateCapitalTable.
In the GIVE QUIZ section:
- Initialize a cell array called response to hold the user’s responses.
- Initialize a logical vector called scoreCard to hold true/false values indicating whether each answer is correct or incorrect, respectively. You can initialize this vector as all false values by using the false function (which works just like the ones or zeros function).
- Use a for-loop to give the quiz and collect the responses. Each prompt should be in the form of a question (with a space after the question mark). For example, the first prompt should be:
What is the capital of Alabama?
- After each question, display ‘Correct!‘ if the answer is correct, or ‘Incorrect!‘ if the answer is incorrect, in the command window. Consider empty answers as incorrect.
- If the user types the word stop (using any capitalization) in response to any quiz question, display ‘Quiz aborted‘ in the command window, and then abort the program immediately (without reporting ‘Incorrect!‘, without asking any more questions, and without executing anything in the REPORT SCORE section).
In the REPORT SCORE section:
- After the last question (if the quiz wasn’t aborted), display the user’s score in the command window as the number correct out of the number of items, followed by the percent correct rounded to 1 decimal place in parentheses. For example, if the user got 7 out of 8 items correct, the report should say:
You got 7 out of 8 answers correct (87.5%).
HINT: See page 6 of the Lecture 5 handout for how to specify a number of decimal places, and how to display the % symbol, when using the fprintf function.