I don't know about using a stack, but with some help from algebra, we can derive a formula for the new average, using the old average.
Let's say you have already averaged n - 1
items, and you have that average in oldAvg
.
oldAvg = (x1 + x2 + .. + xn - 1) / (n - 1)
The new average would be represented by newAvg
:
newAvg = (x1 + x2 + .. + xn - 1 + xn) / n
With some algebraic manipulation, we can represent the new average using the old average the number of items averaged, and the next item.
newAvg = (x1 + x2 + .. + xn - 1) / n + xn / n
= ((n - 1)/(n - 1)) * (x1 + x2 + .. + xn - 1) / n + xn / n
= oldAvg / n * (n - 1) + xn / n
This can avoid overflow by dividing by n
before multiplying by n - 1
. Then all you have to do is add in the next item, xn, divided by n
.
The first loop would establish the average as equal to the first element, but each subsequent loop would use the formula above to derive the new average.
n++;
newAvg = oldAvg / n * (n - 1) + it.next() / n;