Recursively determine average value
- by theva
I have to calculate an average value of my simulation. The simulation is ongoing and I want (for each iteration) to print the current average value. How do I do that?
I tried the code below (in the loop), but I do not think that the right value is calculated...
int average = 0;
int newValue; // Continuously updated value.
if(average == 0) {
average = newValue;
}
average = (average + newValue)/2;
I also taught about store each newValue in an array and for each iteration summarize the whole array and do the calculation. However, I don't think that's a good solution, because the loop is an infinity loop so I can't really determine the size of the array.
There is also a possibility that I am thinking too much and that the code above is actually correct, but I don't think so...