formula to calculate average quiz score I'm creating a Quiz web application for an assignment. There is a requirement to show a user the average score for a particular quiz (from pool of all users who have tried it before) upon successfully completing it. 
A quiz has 10 questions and each question gets 10 marks.
I have not implemented a user registration mechanism. Instead, I keep track of the number of successful attempts for a particular quiz, in the quiz database. This is where I'm getting a little confused, as to how to calculate the average quiz score for that particular quiz.
After every successful quiz attempt, I send an API call to the server recording the attempt and the user score. Every call will increment the attempts column by 1, but I have no idea what to do with the score. Do I keep adding scores in the database (and go with sum of all scores / attempts approach) or is there any other efficient way I could calculate this average score per quiz?
Your insights are highly appreciated.
 A: What I interpret you as asking is this: you can just store all the scores for the quiz, ever, and calculate the average when some user asks for it, but you're wondering if you can provide the same data without having to store all the scores.
If I understand correctly, you can calculate a "running average" (called a "cumulative moving average" here) like this: let the $n$-th score be $s_n$. The average of the first $n$ scores is:
$$
\overline{s_n} = \frac{s_0 + s_1 + \dots + s_n}{n}
$$
Now you've got a new score, $s_{n+1}$. The new average should be:
$$
\overline{s_{n+1}} = \frac{s_0 + s_1 + \dots + s_n + s_{n+1}}{n + 1}
$$
Doing some algebra, we can try to rearrange this to involve values we know:
$$
\begin{aligned}
\overline{s_{n+1}} &= \frac{s_0 + s_1 + \dots + s_n + s_{n+1}}{n + 1}\\
&= \frac{s_0 + s_1 + \dots + s_n}{n + 1} + \frac{s_{n+1}}{n+1}\\
\end{aligned}
$$
The right-hand term we can calculate. The left-hand term is almost $\overline{s_n}$, but it has that pesky $n + 1$ in the denominator. So we multiply by $\frac{n+1}{n}$ to get rid of it.
$$
\frac{s_0 + s_1 + \dots + s_n}{n + 1} \cdot \frac{n + 1}{n} = \frac{s_0 + s_1 + \dots + s_n}{n} = \overline{s_n}
$$
Rearranging that:
$$
\frac{s_0 + s_1 + \dots + s_n}{n + 1} = \overline{s_n} \cdot \frac{n}{n+1}
$$
And substituting back in:
$$
\begin{aligned}
\overline{s_{n+1}} &= \overline{s_n}\cdot \frac{n}{n+1} + \frac{s_{n+1}}{n+1}\\
\overline{s_{n+1}} &= \frac{n\overline{s_n} + s_{n+1}}{n+1}\\
\end{aligned}
$$
So that's your formula for the new average, in terms of the old average, $n$, and the new score.
Now, what I just found with algebra can also be found with common sense. We think, if we think of the new score as an "average" (of a data set with one element) then we're asking: how do I combine two averages to make a new one?
Seems like that should be some kind of weighted sum. We should weight the old average by $n$, since it was around for $n$ scores, (treat is as a big fat data point) and weight the new average by just $1$, since it was around for one score. Then just divide by the total number of scores.
(You might do a similar calculation to figure out your new GPA given your current GPA, your new grades, and the number of classes you've taken so far).
