Find sum of elements in a C++ array
This post will discuss how to find the sum of elements in a C++ array.
1. Using STL’s accumulate()
function
The standard solution is to use the std::accumulate
provided by the standard library. It is defined in the header file numeric
. The default operation is to add the elements up to but to make the context more clear. We can also pass a function in the optional fourth parameter, which performs the addition operation on two numbers and return the result.
1 2 3 4 5 6 7 8 9 10 11 12 |
#include <iostream> #include <numeric> int main() { int arr[] = { 5, 3, 7, 9, 2 }; int sum = std::accumulate(std::begin(arr), std::end(arr), 0, std::plus<int>()); std::cout << sum; return 0; } |
2. Using Boost’s accumulate()
function
Another good alternative is to use boost’s accumulate()
function, which is defined in the header file boost/range/numeric.hpp
.
1 2 3 4 5 6 7 8 9 10 11 12 |
#include <iostream> #include <boost/range/numeric.hpp> int main() { int arr[] = { 5, 3, 7, 9, 2 }; int sum = boost::accumulate(arr, 0); std::cout << sum; return 0; } |
3. Using STL’s for_each()
function
We can also write the summation logic in the predicate to the std::for_each
standard algorithm. This is demonstrated below using lambdas:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
#include <iostream> #include <algorithm> int main() { int arr[] = { 5, 3, 7, 9, 2 }; int sum = 0; std::for_each(std::begin(arr), std::end(arr), [&] (int &i) { sum += i; }); std::cout << sum; return 0; } |
4. For-loop / Range-based for-loop
We can also write our own routine for this simple task. The idea is to traverse the array using simple for-loop or range-based for-loop and accumulate the sum of each element in a variable.
⮚ For-loop
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
#include <iostream> int main() { int arr[] = { 5, 3, 7, 9, 2}; int n = sizeof(arr) / sizeof(int); int sum = 0; for (int i = 0; i < n; i++) { sum += arr[i]; } std::cout << sum; return 0; } |
⮚ Range-based for-loop
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
#include <iostream> int main() { int arr[] = { 5, 3, 7, 9, 2 }; int sum = 0; for (int i: arr) { sum += i; } std::cout << sum; return 0; } |
5. Using valarray sum()
function
If the valarray
class is used instead of the standard array, the sum operation can be applied directly to the valarray
object by calling its member function sum()
.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
#include <iostream> #include <valarray> int main() { int arr[] = { 5, 3, 7, 9, 2 }; int n = sizeof(arr) / sizeof(int); std::valarray<int> valarray (arr, n); std::cout << valarray.sum(); return 0; } |
6. C++17 – Fold Expressions
With C++17, we can use fold expressions, as shown below:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
#include <iostream> template<typename ...T> auto sum(T ...args) { return (args + ...); } int main() { std::cout << sum( 5, 3, 7, 9, 2 ); return 0; } |
That’s all about finding the sum of elements in a C++ array.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)