sum of elements in c++ stl
accumulate(a.begin(), a.end(), 0)
Array sum in c++ stl
// C++ program to demonstrate working of accumulate()
#include <iostream>
#include <numeric>
using namespace std;
// User defined function that returns sum of
// arr[] using accumulate() library function.
int arraySum(int a[], int n)
{
int initial_sum = 0;
return accumulate(a, a+n, initial_sum);
}
int main()
{
int a[] = {5 , 10 , 15} ;
int n = sizeof(a)/sizeof(a[0]);
cout << arraySum(a, n);
return 0;
}
sum function in c++
// C++ program to demonstrate
// example of sum() function.
#include <bits/stdc++.h>
using namespace std;
int main()
{
// Initializing valarray
valarray<int> varr = { 15, 10, 30, 33, 40 };
// Displaying sum of valarray
cout << "The sum of valarray is = "
<< varr.sum() << endl;
return 0;
}
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us