### Calculate mean of Array class

Hello,

I am trying to create a small function that can return the mean of an object of array class, not a simple array.

Declare and initiate the array:
 ``12`` ``````array myArray = {}; myArray.fill(1.0);``````

function to calculate mean:
 ``1234567`` ``````double mean_array(double *array) { double sum = 0.0; for(int i=0; i

However, it does not work. It seems it is incompatible with the array definition. Maybe some other problems. Could someone please tell me how to correct it?
That function wouldn't even have worked on a normal array because array in mean_array is a pointer and sizeof(array) gives you the size of the pointer. There is no way you can calculate the size of the array from just the pointer. Normally such functions takes the size of the array as an extra argument.
 ``1234567`` ``````double mean_array(double *array, std::size_t size) { double sum = 0.0; for(int i=0; i < size; i++) sum += array[i]; return sum / size; }``````

To use this function on myArray you can use the data() member function to get a pointer to the first element in the array and the size() member function to get the number of elements in the array.
`mean_array(myArray.data(), mean_array.size());`

You can also change the function to take the std::array<double, 100> object directly.
 ``1234567`` ``````double mean_array(const std::array& array) { double sum = 0.0; for(int i=0; i
You probably want to do somthing like this:

 ``123456789101112131415161718192021222324252627`` ``````#include using namespace std; typedef array ARRAY_TYPE; double mean_array(ARRAY_TYPE& arr) { double sum(0.0); for (ARRAY_TYPE::iterator p = arr.begin(); p != arr.end(); ++p) sum +=*p; return sum / arr.size(); } void add_values(ARRAY_TYPE& arr) { for (int n = 0; n < arr.size(); ++n) arr.at(n) = n +1; } int main() { ARRAY_TYPE myArray; add_values(myArray); double avg = mean_array(myArray); return 0; }``````
Thanks Peter87 and ajh32 for your prompt reply.

Actually the function is a bit more complicated. I have to determine the size of array depending on another constant int. Declare and initiate the array:
 ``123`` ``````const int time = 100; array myArray = {}; myArray.fill(1.0);``````

In this case, if I use your second proposition (which asks only one argument), `double mean_array(const std::array<double, time>& array)` won't work. How should I write the argument to the function, please?
Last edited on
and I think what we do here is just to build a function to calculate the average of an array declared with array class. Is it possible to build a function that can calculate the average of array no matter its type, please?

For ex:
 ``12345`` ``````const time = 100; double myArray_1[time] = {}; ...... array myArray_2 = {}; ......``````
Last edited on
If the size of the array is not known at compile time it's better to use std::vector.

It is possible to write the function to work for any container of any size, containing any arithmetic type using templates.
 ``12345678`` ``````template double mean(const Container& container) { double sum = 0.0; for(auto value : container) sum += value; return sum / container.size(); }``````
Actually the size of arrays are known in advance. I use `const int time = 100;` to make it easier to adjust the size (there are a lot of arrays in my code).

I tried with the function with template. During compiling, the compiler (MSVS) said
"error LNK2019: unresolved external symbol "double __cdecl mean<class std::array<double,100> >(class std::array<double,100> const &)" (??\$mean@V?\$array@N\$0GE@@std@@@@YANABV?\$array@N\$0GE@@std@@@Z) referenced in function _main"

Could it be a small problem somewhere? Maybe it is from `auto value : container` ?