Calculating average of array

adsl2

Expert Member
Joined
Oct 22, 2010
Messages
1,213
Reaction score
2
Hi there,

I have a problem and hope that someone can help me.

I need to calculate the average value of an array, the problem is that the total sum will be to big too big to store as one single variable. How can one calculate the average of this array sequentially using a loop?

Thanks
 
I think you learn this in primary school:

(a + b + c)/3 = (a/3 + b/3 + c/3)

(for example)
 
Language? How big numbers are we talking here? greater than 2^64? or what J25 said.
 
Thanks for the replies guys, I will use J25's suggestion.
 
How big are we talking? Why would you be needing such huge numbers?

In C++, you could make a loop something basic like this to work the average:

Code:
double num[max];
double numAverage;
double numTotal;

for (int i = 0; i < max; i++) {
   numTotal = numTotal + num[i];
   numAverage = numTotal / i;
}

But then again, don't have enough info to work on.
 
Will something like this work? :confused:

a=Array(5,10,15,20,55,16,19)
nElements = UBound(a) - LBound(a) + 1
for each x in a
ave=ave+(x/nElements)
next
 
The question ask me to calculate the average colour in an image with a height of 800 and a width of 600. All the pixels are grayscale with 0 as black and 65535 as white. I have created a multidimensional array that holds all the values. Now I simply want to loop through them and calculate the average colour.

My initial calculations looks a bit wrong as a double will be able to store the sum of the values (2^64 > (2^16 x 800 x 600)) so I can simply get the sum of the values and divide them by the amount.

Lets create a scenario where you have to calculate the average but you know the value will be to big to store in one variable. Will the easiest method be to divide each value by the total items and add them together, like J25 suggested, or are there a better way?

By the way, I am coding in C++ (learning)
Thanks for all the help guys.
 
The question ask me to calculate the average colour in an image with a height of 800 and a width of 600. All the pixels are grayscale with 0 as black and 65535 as white. I have created a multidimensional array that holds all the values. Now I simply want to loop through them and calculate the average colour.

My initial calculations looks a bit wrong as a double will be able to store the sum of the values (2^64 > (2^16 x 800 x 600)) so I can simply get the sum of the values and divide them by the amount.

Lets create a scenario where you have to calculate the average but you know the value will be to big to store in one variable. Will the easiest method be to divide each value by the total items and add them together, like J25 suggested, or are there a better way?

By the way, I am coding in C++ (learning)
Thanks for all the help guys.

Why not just find the average for each row, and then find the average of all the row averages.
 
if its just the two colors, surely you dont need all that other spew
 
Last edited:
Top
Sign up to the MyBroadband newsletter
X