Using accumulate in C++, we can find array sum efficiently
A linear data structure called an array contains identical data-type elements in a continuous stream of memory.
The sum of all the items in an array is known as array sum.
There are several ways in the C++ programming language to find the array sum.
// 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;
}
Output:
Time Complexity: O(n)
Space Complexity: O(n) where n is the size of the array.
Sum of vector
// C++ program to demonstrate working of accumulate()
#include <iostream>
#include <vector>
#include <numeric>
using namespace std;
// User defined function that returns sum of
// arr[] using accumulate() library function.
int arraySum(vector<int> &v)
{
int initial_sum = 0;
return accumulate(v.begin(), v.end(), initial_sum);
}
int main()
{
vector<int> v{5 , 10 , 15} ;
cout << arraySum(v);
return 0;
}
Output:
Time Complexity: O(n)
Space Complexity: O(n) where n is the size of the array.
Classical technique
The simplest way to calculate the sum of an array's elements is to loop through them while adding the element's value to the sum variable.
Algorithm
Step 1 : For i from 0 to n-1, follow step 2 ;
Step 2 : sum = sum + arr[i]
Step 3 : print sum.
Example
Live Demo
#include <iostream>
using namespace std;
int main (){
int arr[] = { 2, 5, 7, 8, 2, 6, 9 };
int n = 7, sum = 0;
for(int i = 0; i<n ; i++){
sum+=arr[i];
}
cout<<"The array sum is "<<sum;
return 0;
}
Output:
The array sum is 39
Using Accumulate Method
The accumulate method in c++ used to find the array sum. This function can be accessed from the numeric library in c++.The numeric> header file, which needs to be included at the beginning of the code in order to use this method, contains the definition for the accumulate function:
Syntax
accumulate(array_name , array_name+length , sum);
Example
Live Demo
#include <iostream>
#include <numeric>
using namespace std;
int main (){
int arr[] = { 2, 5, 7, 8, 2, 6, 9 };
int n = 7, sum = 0;
sum = accumulate(arr, arr+n, sum);
cout<<"The array sum is "<<sum;
return 0;
}
Output:
The array sum is 39
Using Sum Of Vectors
You can use the accumulate function on vectors too. It will return the sum of array which is it vector form.
Example
Live Demo
#include <iostream>
#include <vector>
#include <numeric>
using namespace std;
int arraySum(vector<int> &v){
int initial_sum = 0;
return accumulate(v.begin(), v.end(), initial_sum);
}
int main(){
vector<int> v{12, 56, 76, 2, 90 , 3} ;
int sum = 0;
sum=accumulate(v.begin(), v.end(), sum);
cout<<"The sum of array is "<<sum;
return 0;
}
Output:
The sum of array is 239
Use a for loop
We can use a for loop to traverse the array. All the elements can be added up one by one:
Initialize sum = 0.
Run a for loop from i = 0 to i = size - 1.
At every iteration of the for loop, add sum and the current element of the array, i.e., sum = sum + arr[i].
At the end of the for loop, the sum of all the elements will be stored in the sum variable.
#include <iostream>
using namespace std;
int main() {
// initialise array
int arr[] = {2, 4, 6, 8};
int size = 4;
// initialise sum to zero
int sum = 0;
// for loop runs from 0 to size - 1
for(int i = 0; i < size; i++)
{
sum = sum + arr[i];
}
cout << "The sum of the elements in the array: " << sum;
}
Output:
The sum of the elements in the array: 20
Using valarray sum function
By invoking the member function sum of the valarray class, which is used in place of the normal array, the sum operation can be performed on the valarray object directly .