Reputation: 6745
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...
Upvotes: 0
Views: 168
Reputation: 65889
To do it the oop way:
class Average {
private int count;
private int total;
public void add ( int value ) {
this.total += value;
this.count += 1;
}
public int get () {
return total / count;
}
}
Upvotes: 0
Reputation: 22342
You don't have to keep track of the total, or multiply the average by the number of iterations. You can instead weight the new value and add it directly to the average. Something like:
int count = 0;
double average;
while (/*whatever*/)
{
double weighted = (newValue - average) / ++count;
average += weighted;
}
Upvotes: 0
Reputation: 1084
I would keep a running total and a count of iterations, sadly not recursive.
long total = 0;
int count = 0;
while ((int newValue = getValue()) > -1) // or other escape condition
{
total += newValue;
long average = total / ++count;
System.out.println(average);
}
Upvotes: 6
Reputation: 1890
Another possibility:
double newValue, average;
int i = 1;
while(some_stop_condition)
{
newValue = getValue();
average = (average*(i-1) + newValue)/i;
i++;
}
Upvotes: 1
Reputation: 31206
Or, if you want to do this the easy way, don't actually compute your average until you need to display it or use it. Just keep the sum
, and the count
, and wherever you need an average just do
System.out.println("Average: " + (sum/count));
if you want to prevent division by zero, you can use the Ternary operator
System.out.println("Average: " + (count == 0 ? 0 : sum/count));
Upvotes: 0
Reputation: 13352
Since some of the posters here seem to be mathematically challenged, let me state the obvious:
It's possible to get a relation between Average(n) and Average(n+1):
Average(n+1) = (Average(n)*n + new_value)/(n+1)
assuming that average is calculated with enough precision.
So it should be possible to create a recursion, though for OP purposes it simply not needed.
Upvotes: 1
Reputation: 340
In your loop, just keep adding each newValue
onto a variable which just keeps incrementing by each newValue
for each iteration and keep a record of how many times this happens with a 'counting' variable. Then to get the average for that iteration, just divide the total by the number of values taken into account.
int total = 0;
int count = 0;
// Whatever your loop is...
for(;;) {
// Add the new value onto the total
total += newValue;
// Record the number of values added
count ++;
// Calculate the average for this iteration
int average = total / count;
}
Upvotes: 0
Reputation: 1464
Try below code snippet:
double RecursiveAvg(double A[], int i, int n)
{
//Base case
if (i == n-1) return A[i]/n;
return A[i]/n + RecursiveAvg(A, i + 1, n);
}
Upvotes: 0