Compute the mean float mean with accumulate function : accumulate « STL Algorithms Helper « C++






Compute the mean float mean with accumulate function

  
#include <algorithm>
#include <cmath>
#include <functional>
#include <iostream>
#include <list>
#include <numeric>
#include <vector>

using namespace std;

template <class T>
void print(T& c){
   for( typename T::iterator i = c.begin(); i != c.end(); i++ ){
      std::cout << *i << endl;
   }
}

int main( )
{
   const float a[] = { 1, 1.3, 1.5, 0.9, 0.1, 0.2};

   // create and initialize vector with above data
   vector<float> data( a,a + sizeof( a ) / sizeof( a[0] ) );

   cout << "DATA VECTOR HAS " << data.size() << " ELEMENTS\n";
   print( data  );

   // compute the mean
   float mean = accumulate( data.begin(), data.end(), 0.0f )/ data.size();
}
  
    
  








Related examples in the same category

1.Calculate sum of elements in a vector
2.Algorithm: Use accumulate to calculate product
3.Demonstrating the generic accumulate algorithm with a reverse iterator
4.Illustrating the generic accumulate algorithm with predicate
5.accumulate( ) computes a summation of all of the elements within a specified range and returns the result.
6.Use accumulate() and minus()
7.Use accumulate to calculate sum for double array with minus()
8.accumulate value in a vector
9.Finding the Mean Value