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:
1
2
array<double, 100> myArray = {};
myArray.fill(1.0);


function to calculate mean:
1
2
3
4
5
6
7
double mean_array(double *array)
{
	double sum = 0.0;
	for(int i=0; i<sizeof(array); i++)
		sum += array[i];
	return sum / sizeof(array);
}


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?
Thanks in advance.
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.
1
2
3
4
5
6
7
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.
1
2
3
4
5
6
7
double mean_array(const std::array<double, 100>& array)
{
	double sum = 0.0;
	for(int i=0; i<array.size(); i++)
		sum += array[i];
	return sum / array.size();
}
You probably want to do somthing like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <array>
using namespace std;

typedef array<double, 100> 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:
1
2
3
const int time = 100;
array<double, time> 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:
1
2
3
4
5
const time = 100;
double myArray_1[time] = {};
......
array<double, time> 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.
1
2
3
4
5
6
7
8
template <class Container>
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 ?
Thanks in advance.
Last edited on
Is that function in the header? that's a message commonly seen when function templates are moved into .cpp files.
That function is in a .cpp file (not the main cpp). Shall I put it in a header? I thought we only put class definitions in header...
Topic archived. No new replies allowed.