Recursively determine average value
Posted
by
theva
on Stack Overflow
See other posts from Stack Overflow
or by theva
Published on 2013-10-21T15:52:21Z
Indexed on
2013/10/21
15:53 UTC
Read the original article
Hit count: 149
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...
© Stack Overflow or respective owner