Accumulate And Partial Sum In C++ STL Numeric Header - C++ Programming Tutorial
C++ Course / STL Basics / Accumulate And Partial Sum In C++ STL Numeric Header

Accumulate And Partial Sum In C++ STL Numeric Header

BLUF: Mastering Accumulate And Partial Sum In C++ STL Numeric Header is a critical step in becoming a proficient C++ developer. This lesson provides a deep dive into the syntax, performance considerations, and real-world applications of this concept.
Key Performance Insight: Accumulate And Partial Sum In C++ STL Numeric Header

C++ is renowned for its efficiency. Learn how Accumulate And Partial Sum In C++ STL Numeric Header enables low-level control and high-performance computing in the tutorial below.

What is C++ STL?

In C++, there is the Standard Template Library (STL), a collection of pre-implemented functions that can be easily utilized by including the library in our codebase.

Similarly, there is a numerical library available in the STL, with the numeric header being a component of this library.

The numerical header contains numerous built-in functions designed for mathematical computations, effectively reducing the time required for programming tasks.

For example:

  • accumulate
  • partial_sum
  • iota
  • inner_product
  • reduce
  • range
  • incluse_scan
  • exclusive_scan

We will examine the subsequent functions within this article:

1. accumulate

If there is a need to calculate the sum of elements within a specific range of an array, utilizing this function directly is the most efficient approach. Otherwise, resorting to iterating through the array using a loop becomes necessary to compute the sum.

There are two ways of using this function:

i) With three arguments

In this scenario, the function will accept three parameters: initial index, final index, and a variable total, which will contribute its starting amount to the cumulative sum of elements within that range.

Syntax:

Example

accumulate (starting_position,ending_position,sum_variable)

C++ Example:

Example

#include <iostream>
#include<numeric>
using namespace std;
int main() {
   
   int sum_variable = 12;
   int arr[]={1,2,3,4,5,6,7,8,9};
   cout<<accumulate(arr+0,arr+3,sum_variable)<<endl;
   cout<<sum_variable<<endl;
}

Output:

Explanation

In the provided code snippet, the sumvariable is set to 12 initially. Subsequently, there exists an array comprising 9 elements, and a function is employed from arr+0 to arr+3. This indicates that it will accumulate the values from index 0 up to index 2 (the final index is not included), followed by the addition of the sumvariable value to the resultant sum.

So when we sum from index 0 to index 2, the total is six. After adding 12, the result will be 18 when printed.

ii) With four arguments

In this function, the fourth parameter can be utilized to specify an additional function to execute concurrently with the main function.

Syntax:

Example

accumulate (starting_position,ending_position,sum_variable,special_function)

C++ Example:

Example

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

int mult(int a,int b){
    return a*b;
}
int main() {
   
   int sum_variable = 12;
   int arr[]={1,2,3,4,5,6,7,8,9};
   cout<<accumulate(arr+0,arr+4,sum_variable,mult)<<endl;
   cout<<sum_variable<<endl;
}

Output:

Explanation:

In the provided code snippet, a function named mult was defined to calculate and return the product of two numerical values.

So, we will calculate the product of the values from index 0 to index three, resulting in 24. This outcome will then be further multiplied by our sum_variable, yielding the final value of 24x12, which equals 288.

1. partial_sum

If there is a need to calculate the sum of a specific range within an array and save this outcome in a separate array, this function can be conveniently employed for that purpose.

We can use this function in two ways:

i) With three arguments

In this scenario, we will work with three parameters: the initial position, the end index (exclusive), and the target array where the output will be stored.

Syntax:

Example

partial_sum(starting_position,ending_position,resultant_array);

Suppose there are n elements in a given array, numbered from 0 to n-1. To calculate the sum of a specific range from index L to index R, a new array of size R-L+1 is generated. The elements in this new array will be populated in the following manner:

Let res is the resultant array then:

Example

res[0]=arr[L];
res[1]=arr[L]+arr[L+1];
res[2]=arr[L]+arr[L+1]+arr[L+2];

res[R-L]=arr[L]+arr[L+1]+arr[L+2].....arr[R];

C++ Example:

Example

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

int main() {
   int arr[]={1,2,3,4,5,6,7,8,9};
   int L=2;
   int R=5;
   int b[R-L+1];
   partial_sum(arr+L,arr+R+1,b);
   for(int i=0;i<=R-L;i++){
       cout<<b[i]<<endl;
   }
}

Output:

Explanation:

In the preceding illustration, we will calculate the sum of elements from index 2 to index five, and then we will save these four values in the array b.

So b[0]=arr[2] = 3

b[1]=arr[2]+arr[3]=7

b[2]=arr[2]+arr[3]+arr[4]=12

b[3]=arr[2]+arr[3]+arr[4]+arr[5]=18

ii) With four arguments

We have the option to utilize the fourth parameter as a different function, allowing us to specify our custom calculation for the partial sum.

Syntax:

Example

partial_sum(starting_position,ending_position,resultant_array,special_function);

C++ Example:

Example

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

int myFun(int a,int b){
    return 2*b-a;
}
int main() {
   int arr[]={1,2,3,4,5,6,7,8,9};
   int L=2;
   int R=5;
   int b[R-L+1];
   partial_sum(arr+L,arr+R+1,b,myFun);
   for(int i=0;i<=R-L;i++){
       cout<<b[i]<<endl;
   }
}

Output:

Explanation:

In the provided code snippet, a custom function is utilized to generate the output in the resulting array based on specific criteria defined within the function.

b[0]=arr[2] =3

b[1]=2*arr[3]-b[0]=5

b[2]=2*arr[4]-b[1]=5

b[3]=2*arr[5]-b[2]=7

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience