Programming assignment question
Question 1.
Your task is to write a program to repeatedly generate random multiplications to quiz the user, and keep score.
Generate the two multiplicands (e.g. 6 and 7) and compute the correct answer (42). Then ask the user for his/her answer, and compare it to the correct answer. If the user gets it right, increment the total score. Display the score as a fraction (e.g. 5/8 correct) and a percentage, displayed to the nearest percent (e.g. 63%). Continue generating questions as long as the user indicates he/she would like to keep playing.
Constraint: you must include functions in your program. Include at least these 4 functions, fitting the supplied prototypes:
- A function called NewRandomNumber() that receives an integer n (e.g. 10), and returns a new random number between 2 and n. We don't want to make questions like 1x3 or 0x8 because these are too easy.
int NewRandomNumber(int n);
Your function should make use of rand() (part of the cstdlib library) . rand() generates a pseudo-random integer between 1 and 32767. use % and + to convert this integer to an integer between 2 and n.
- A function MakeQuestion() to create the question to ask the user. This function should call NewRandomNumber() twice to make two new random numbers; it should figure out what the answer should be, and then return the two numbers and the answer. MakeQuestion()needs to know about n so it can tell NewRandomNumber() what kind of numbers to make. Because this function needs to return 3 values, it should use reference parameters.
void MakeQuestion(int n, int& a, int& b, int& atimesb);
- A function UserAnswerIsCorrect() that uses the two operands and the correct
answer which were generated in MakeQuestion() and presents the question to the user via cout. It then receives the user’s answer via cin and compares it against the correct answer. If the user’s answer is correct, it returns True. Otherwise, it returns False.
bool UserAnswerIsCorrect(int a, int b, int correctAnswer);
- A function PrintScore() that receives the total number of questions the user has
answered correctly, as well as the total number of questions, and computes and displays the user’s score as a fraction and as a percentage.
void PrintScore(int numCorrect, int numAsked);
At the beginning of the program, the user should be able to choose their difficulty by entering a number. For example, if the user enters "4", the computer should only generate questions up to 4 times 4. If the user enters "12", the program should generate questions up to 12 times 12.
Your program should also generate a log file of important data, such as the generated question, the user's response, and the cumulative score. This log should be a comprehensive record of the program run, but should not be a duplication of every user interaction. Further, you must format this log file nicely. For example:
QUIZ PROGRAM LOG FILE
User name: John Smith
Difficulty: 8
Question Response Cumul. Score
===== ==== ===== -- =====
3 * 5 = 15 1/1 (100%)
8 * 2 = 16 16 2/2 (100%)
6 * 9 = 54 45 2/3 (66%)
END OF SESSION
Make sure to fully test your program, and show your testing in your log file. Justify your choice of test cases in your comments.
BONUS: Research and make use of a random number seed, so that the random numbers are different every time the program is run.
MORE BONUS: Research the clock() function in the <ctime> library (http://www.cplusplus.com/reference/ctime/clock/) to implement a timer to measure how long the user takes to answer the question. Maybe you want to reward the user for quick answers, and insult the user if they take too long. Maybe the user has to answer within 10 seconds. Maybe you want to display a countdown timer to show the user how long they have left. Maybe you want to track the time in the log and compare to the last time the user saw the question to see if they are improving. Be creative.
SUPER AMAZING BONUS: Keep a file with a list of users and their score, and each time a user opens the program and enters their name, check against the file to see if they have played before. If they have, continue their score from where they left off.
Question 2.
PRACTICE QUESTION ONLY
You do not have to do this question. This question will not be marked. It is included here as a practice exercise for those of you who feel you need more practice on loops and files. A solution to this question WILL be given to allow you to see how you are doing. You MAY submit your solution to this question, and if you do, it MIGHT count toward a participation mark.
- Write a program that calculates the mean and variance of a set of numbers found in a file. First, open the file and read in the numbers. As each number is read, add it to a sum variable and add 1 to a count variable. After you've read every number in the file (via an EOF-controlled loop), calculate the mean by dividing the sum by the count. To calculate the variance, open the file and read the numbers again. The second time, as each number is read, subtract it from the mean value, square the result, and add it to a sum variable. After you've done this for every number in the file, calculate the variance by dividing the new sum by the count.
To summarize:
The mean is the sum of all the numbers, divided by how many numbers there are. µ = 1/n∑x
The variance is the sum of the squares of the differences between each number and the mean, divided by how many numbers there are. v = 1/n∑(µ-x)^2
- Do the same thing as in part a, but instead of reading from a file, ask the user for the numbers. Continue asking for numbers until the user enters something other than a number, or forces a stream fail state using "Ctrl-C".
- Do the same thing as in part b, but instead of having the user enter numbers until they are done, you should first ask the user how many numbers they will enter, and then ask for exactly that many numbers.