We have the key and a set of student answers for a multiple choice quiz. We deal with a lot of these quizzes and would like to write some code to help score quizzes and identify problems that may be too easy or hard.
There are two different tasks we are looking to write code for. The first is to calculate the score for each student. The second is to calculate the average score for each question (how hard it was). The two tasks likely will share some logic, but it does not seem like either one depends on the other.
And the student answers as a list where each โrowโ is a different studentโs responses to the. A set of four studentโs responses to a 5 question quiz might look like this (where _ represents an unanswered question):
The key looks like a single dimensional vector of characters. The student answers a 2-D grid of characters. Letโs write some typedefs to name vectors with the appropriate dimensions:
To compute student grades, we need to compare each studentโs answers to the key and count how many match. I think that whenever I calculate the grades I will want to do so for all the students. So letโs build a function that calculates all the grades and returns a vector of them.
This function will need to accept the grid of student answers and the key. It should return a vector of scores. We will represent the score as number of correct answers (not as percents) to avoid having to worry about rounding issues.
Most of this function is written. All that is missing (look for ???) are the indexes to use when comparing a studentโs answer to the key. Fill in those blanks with the correct counting variable - either studentNumber or questionNumber.
We did not do any error checking to make sure that each student has enough answers. If the key is longer than a studentโs row of answers, we will end up going out of bounds on the student grid. In that case the vector will generate an exception. If we wanted, we could try to catch that exception. But it is not clear what we should do to fix the problem. So we will just let any possible exception propagate to whatever program calls this.
Now we need to build a function to calculate the difficulty of questions. This time, letโs build a function that just focuses on one question at a time. We will need to iterate through all of the students, but we will just be looking at one question. So we will only need a single loop.