Function for calculating the average value of the double [] array with accumulation

This should be the most common function so that everyone has a piece of code somewhere, but I really spent at least 1.5 hours searching on SO, as well as on other C ++ sites and did not find a solution.

I would like to calculate the average of a double array[] using a function . I would like to pass an array of function as a reference . There are millions of examples where the average is calculated in the main () loop, but what I'm looking for is a function that I can put in an external file and use it anytime later.

So far, here is my latest version that gives a compilation error:

 double mean_array( double array[] ) { int count = sizeof( array ) / sizeof( array[0] ); double sum = accumulate( array, array + count, 0 ); return ( double ) sum / count; } 

Compilation Error:

error C3861: "accumulate": identifier not found

Can you tell me how to fix this feature? What does this compilation error mean?

If I use std::accumulate (as already defined using namespace std ), I get the following error:

 'accumulate' : is not a member of 'std' 'accumulate': identifier not found 

Why is "accumulate" not a member of "std"?

ps: I know that I can do "sum + = array [i]" and not use, accumulate, but I would like to understand what is happening here and how I can get my example to work.

+6
source share
4 answers

Try to add

 #include <numeric> 

It will lead to the function "std :: accumulate" that you are looking for.

Going further, you will have a problem to find out the number of elements in your array. Indeed, an array cannot be passed to a function in the hope that the function will be able to find out the size of the array. It will decay to a pointer. Therefore, your calculation of count will be incorrect. If you want to pass an array with a specific size, you should use a template function.

 template <int N> double mean_array( double ( & array )[N] ) { return std::accumulate( array, array + N, 0.0) / (double)(N); } 
+17
source

Not exactly the question you asked, but there is a simple error in your code example. The initial value in accumulate is a pattern, and in your code, integers are its pattern. If you give him a set of doubles, they will be cast from integers, and you will receive incorrect answers. Having made this mistake before, I made myself a quick warranty character as follows:

  /** Check that not inputting integer type into accumulate * This is considered an error in this program (where a double was expected * @tparam InputIterator The iterator to accumulate * @tparam T The type to accumulate - will fail if integer. * @param first The first iterator to accumulate from. * @param last the iterator to acculate to, * @param init The initial value * @return The accumulated value as evaluated by std::accumulate. */ template<class InputIterator, class T> inline T accumulate_checked(InputIterator first, InputIterator last, T init ) { return std::accumulate(first,last, init); } //Not implemented for integers (will not compile if called). template<class InputIterator> inline int accumulate_checked(InputIterator first, InputIterator last, int init ); 

I thought I would share it if it would be interesting.

Just for completeness, your function might look like this:

 double mean_array( double *array, size_t count ) { double sum = std::accumulate(array,array+count,0.0) return sum / count; } 

or be especially careful

 double mean_array( double *array, size_t count ) { double sum = accumulate_checked(array,array+count,0.0) return sum / count; } 

or even better didier trosset template version

+3
source

To use std::accumulate , you need to include the appropriate header. Add the following to the source file.

 #include <numeric> 
+2
source
 double mean_array( double *array, size_t count ) { double sum = 0.0; for (size_t i = 0; i < count; i++) { sum += array[i]; } return sum / count; } 

or

 double mean_array( double *array, size_t count ) { double sum = 0.0; double *pastLast = array + count; while (array < pastLast) { sum += *array; array++; } return sum / count; } 

If you pass an array to a function, you "lose" its size, so you need to pass it as a parameter (this is a little more complicated than that ... but for now it should be enough)

0
source

Source: https://habr.com/ru/post/900108/


All Articles