accumulate value in a vector : accumulate « STL Algorithms Helper « C++






accumulate value in a vector

  
#include <numeric>
#include <vector>
#include <cmath>
#include <iostream>
using namespace std;

double arithmeticMean(const vector<int>& nums)
{
  double sum = accumulate(nums.begin(), nums.end(), 0);
  return (sum / nums.size());
}

int product(int num1, int num2)
{
  return (num1 * num2);
}

int main(int argc, char** argv)
{
  vector<int> myVector;

  myVector.push_back(1);
  myVector.push_back(2);
  myVector.push_back(3);
  myVector.push_back(4);

  cout << "The arithmetic mean is " << arithmeticMean(myVector) << endl;

  return (0);
}
  
    
  








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.Finding the Mean Value