Answers for "how sum in c++"

7

sum of elements in c++ stl

accumulate(a.begin(), a.end(), 0)
Posted by: Guest on May-17-2020
1

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;
}
Posted by: Guest on January-30-2022
-1

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;
}
Posted by: Guest on April-26-2022

Code answers related to "TypeScript"

Browse Popular Code Answers by Language