# EASY Function: Array Average

• 08-02-2007
benjamin923
EASY Function: Array Average
What is an example of a function I would use to find the average value if I have an array of known size?

i.e.

Code:

```const int LENGTH = 5; double a[LENGTH] = {1,5,6,8,10}; double avg; double sum;```
any ideas? I know this is probably super easy...

thanks
• 08-02-2007
matsp
How would you do the calculation if you were doing it by hand?

Generally, the same method that you'd use by hand works on computer. In this case, you'd add the numbers together and divide by the number of "samples" (elements in the array).

--
Mats
• 08-02-2007
benjamin923
Yea, i can do it by hand easily...what I don't understand is how to add each element to the next one. If you can help me with that I would really appreciate it. Here's what I have:
Code:

```double avg = 0;         for(int i=0; i<npts; i++)                 avg = (avg + a[i])/5;         return avg;```
is this even close?
• 08-02-2007
Salem
Would you divide by 5 each time if you were doing it by hand?

Or would you only divide the total at the end just the once?
• 08-02-2007
benjamin923
I figured out how to do the avg:
Code:

```int main() {         const int SIZE = 5; //max array size         double x[SIZE] = {1,5,6,8,10};         int num_points = 0;         double sum = 0;         double avg;         for(int i=0;i<SIZE;i++)         {         sum = sum + x[num_points];         num_points++;         }         avg = sum/SIZE;         cout<<"the average of the values in the array is:"<<endl;         cout<<avg<<endl;         //for(int i=0; i<SIZE; i++)         //{         return 0; }```
now the problem gets even more fun ;)

thanks for the help
• 08-02-2007
Quote:

Originally Posted by benjamin923
I figured out how to do the avg:
Code:

```int main() {         const int SIZE = 5; //max array size         double x[SIZE] = {1,5,6,8,10};         int num_points = 0;         double sum = 0;         double avg;         for(int i=0;i<SIZE;i++)         {         sum = sum + x[num_points];  /* delete this line, num_points is an unneeded variable, just use i */           /* indenting inside curly braces is a BIG help for us poor humans - please do it */             sum += x[i];  /* this is C standard idiom, learn it well */         num_points++;  /* delete this line */         }         avg = sum/SIZE;         cout<<"the average of the values in the array is:"<<endl;         cout<<avg<<endl;         //for(int i=0; i<SIZE; i++)         //{         return 0; }```
now the problem gets even more fun ;)

thanks for the help

The advice to do your program similarly to how you'd do it by hand, is especially good for beginner's. If you can't do something, it's a rare day indeed that you'll be able to program a computer to do that something.

If you can do a task by hand, it gives a very fertile soil from the description of exactly how you do it, to let the seed of a program, grow.
• 08-02-2007
benjamin923
Thanks for that advice - anything to save time and space helps a bunch.
now if i can only figure out the other part....(next post on board) AHHHHH
• 08-03-2007
MacGyver
Why post C++ code in the C section? At least learn what language you're learning before you start learning it.
• 08-03-2007
Salem
Agreed - moved.