Skip to content
C++ Better Explained
Go back
Sum of Array Elements in C++: Loops and std::accumulate

Sum of Array Elements in C++

Adding up the numbers in an array is one of the most common beginner tasks — and it’s the perfect way to learn how loops walk through data. We’ll do it three ways, from the most explicit loop to a clean one-liner.


The Classic Way: A Counting Loop

The idea behind every summing technique is the same: start a total at 0, then add each element to it one at a time. The most explicit version uses an index:

#include <iostream>

int main() {
    int nums[] = {4, 8, 15, 16, 23, 42};
    int n = sizeof(nums) / sizeof(nums[0]);

    int sum = 0;
    for (int i = 0; i < n; ++i) {
        sum += nums[i];
    }

    std::cout << "Sum = " << sum << "\n";  // 108
    return 0;
}

sum += nums[i] is shorthand for sum = sum + nums[i]. After the loop visits all six values, sum holds their total, 108.


The Cleaner Way: A Range-Based For Loop

If you don’t need the index, a range-based for loop reads much more naturally. It hands you each value directly:

#include <iostream>

int main() {
    int nums[] = {4, 8, 15, 16, 23, 42};

    int sum = 0;
    for (int value : nums) {
        sum += value;
    }

    std::cout << "Sum = " << sum << "\n";  // 108
    return 0;
}

No sizeof, no counter, no chance of an off-by-one mistake. For simply visiting every element, this is the clearest choice a beginner can make.

If you're looking to go deeper with C++, the C++ Better Explained Ebook is perfect for you — whether you're a complete beginner or looking to solidify your understanding. Just $19.

The One-Liner: std::accumulate

The standard library already has a function for this. std::accumulate, from the <numeric> header, adds up a range for you. It works beautifully with a std::vector:

#include <iostream>
#include <vector>
#include <numeric>

int main() {
    std::vector<int> nums = {4, 8, 15, 16, 23, 42};

    int sum = std::accumulate(nums.begin(), nums.end(), 0);

    std::cout << "Sum = " << sum << "\n";  // 108
    return 0;
}

The first two arguments say “from the beginning to the end,” and the third — 0 — is the starting total. This is efficient and expresses your intent in a single line: sum everything.


Watch Out: Summing Doubles

There’s one trap worth knowing. That starting value also sets the type of the running total. Pass a plain int 0 to a vector of double, and every partial sum gets squeezed back into an integer, silently losing the decimals:

#include <iostream>
#include <vector>
#include <numeric>

int main() {
    std::vector<double> prices = {1.5, 2.25, 3.75};

    double wrong = std::accumulate(prices.begin(), prices.end(), 0);    // 6, decimals lost!
    double right = std::accumulate(prices.begin(), prices.end(), 0.0);  // 7.5, correct

    std::cout << wrong << " vs " << right << "\n";
    return 0;
}

The rule: match the starting value to your data. Use 0 for integers and 0.0 for doubles.


Bonus: From Sum to Average

Once you have the sum, the average is one more step — but remember to avoid integer division by casting first:

double average = static_cast<double>(sum) / n;

Without the cast, sum / n would truncate the result. (That’s a common gotcha covered in the integer-division guide below.)


Quick Reference

ApproachBest for
index loop (for i)when you also need the position
range-based (for v : arr)clearest way to visit every element
std::accumulate(b, e, 0)concise one-liner for integers
std::accumulate(b, e, 0.0)summing doubles correctly


Take Your C++ Further

If you’re looking to go deeper with C++, the C++ Better Explained Ebook is perfect for you — whether you’re a complete beginner or looking to solidify your understanding. Just $19.

👉 Get the C++ Better Explained Ebook — $19

📋

Free Download: The 10 Mistakes Every C++ Beginner Makes

A free 1-page checklist that shows the exact traps that slow down every C++ beginner — so you can avoid them from day one.

🔒 No spam. Unsubscribe anytime.


Share this post on:

Written by

Sahil Bora

Software Engineer. Author and creator of C++ Better Explained.


Previous Post
The sizeof Operator in C++: Find the Size of Any Type
Next Post
typedef vs using in C++: Type Aliases Explained

Keep Learning