Finding the Mean Value : accumulate « STL Algorithms Helper « C++






Finding the Mean Value

  
#include <iomanip>
#include <iostream>
#include <numeric>
#include <vector>

using namespace std;

int main( )
{
   // miles per gallon for different cars in fleet
   const float mpg_data[] = { 1.1, 9.9, 8.8, 3.3, 2.2, 2.2,4.4 };

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

   // mean
   float fleet_average = accumulate( mpg.begin(), mpg.end(), 0.0 )/ mpg.size();
   cout << fleet_average << endl;
}
  
    
  








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.Compute the mean float mean with accumulate function
9.accumulate value in a vector