# Average of an Array of integer

This is a discussion on Average of an Array of integer within the C++ Programming forums, part of the General Programming Boards category; Something weird is happening with this problem. I am trying to take an array of integers and get its average, ...

1. ## Average of an Array of integer

Something weird is happening with this problem. I am trying to take an array of integers and get its average, but i keep getting this error. Its probably something small i am missing. Can anyone see it?

theres code in there for more that i made not visible

average.h
Code:
```int average(int array[] )
{

int sum=0;

for (int i=0 ; i <= arraysize; i++)
{
sum = sum+ array[i];

float avg = sum/arraysize;

return avg;
}

}

assignement.cpp

#include<iostream>
#include "variables_for_max_and_min.h"
#include "average.h"
//#include "min.h"
//#include "max.h"

using namespace std;

int main() {

//cout << minimumArray(arraysize, 0, (arraysize - 1)) << endl;

//cout << maxArray(arraysize, 0, (arraysize - 1)) << endl;

cout << average(anArray[arraysize]);

system("Pause");
return 0;

}```

2. > #include "variables_for_max_and_min.h"
Like we have any idea what you've stuffed in here.

> for (int i=0 ; i <= arraysize; i++)
Array subscripts run from 0 to N-1
So we usually say < N

> cout << average(anArray[arraysize]);
To pass an array, just use the name, like
cout << average(anArray);

3. The problem is that the function has no way of knowing how big your array is. The proper way to pass in an array to a function is
Code:
```int array[10];
int array_size = 10;

void foo( int arr[], int size )
{
/* implementation */
}

int main()
{
foo(array,array_size);
}```
Second problem, arraysize is not defined in your function. By putting
Code:
`average(anArray[arraysize]);`
you aren't passing the size into the function. In fact the only thing that is passed is the
pointer( memory address ) of the first element of the array.

4. Originally Posted by Emeighty
i keep getting this error.
What error? How can you says this error without actually showing us what it is? Furthurmore, surely it was not only one error you were getting.

Header files are not for arbitrarily putting code into. If other cpp files don't need to know about every line of code in that function then it doesn't belong in the header file. At most, just the function prototype would be appropriate in this case.

In C++ we use std::accumulate