Search
 
SCRIPT & CODE EXAMPLE
 

CPP

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;
}
Comment

sum array c++

//Syntax
accumulate(first, last, sum);
accumulate(first, last, sum, myfun); 

first, last : first and last elements of range 
              whose elements are to be added
sum :  initial value of the sum
myfun : a function for performing any 
        specific task. For example, we can
        find product of elements between
        first and last.
//Example
  int a[] = {5 , 10 , 15} ;
  int res = accumulate(a,a+3,0); // 30
Comment

PREVIOUS NEXT
Code Example
Cpp :: heap buffer overflow in c 
Cpp :: bfs to detect cycle in undirected graph 
Cpp :: lambda function in c++ 
Cpp :: string comparison c++ 
Cpp :: why do we use pointers in c++ 
Cpp :: visual studio cpp compiler 
Cpp :: looping in map c++ 
Cpp :: il2cpp stuck unity 
Cpp :: C++ Pi 4 Decimal 
Cpp :: rock paper scissor c++ 
Cpp :: set of vectors c++ 
Cpp :: c++ program to print odd numbers using loop 
Cpp :: constrain function in arduino 
Cpp :: c++ multiply char 
Cpp :: copy constructor c++ syntax 
Cpp :: creating node in c++ 
Cpp :: google test assert exception 
Cpp :: how to make randomizer c++ 
Cpp :: array 2d to 1d 
Cpp :: c++ classes 
Cpp :: c++ lambda as variable 
Cpp :: ternary operator in c++ 
Cpp :: c++ function pointer 
Cpp :: create vectors of vectors c++ 
Cpp :: c++ main function parameters 
Cpp :: floyd algorithm 
Cpp :: set to vector c++ 
Cpp :: flipperRSocketResponder.cpp command failed react native 
Cpp :: faster solutions 
Cpp :: *= c++ 
ADD CONTENT
Topic
Content
Source link
Name
5+8 =