The sum of an array is the result of adding every element once. This is the classic accumulator pattern: start at 0, then total += value for each item.
total = 0
+ 1 → total = 1
+ 2 → total = 3
+ 3 → total = 6
+ 4 → total = 10
+ 5 → total = 15
Sum = 15
Code
C++ Programs
Start with a fixed array and a simple helper. Then read values from input, and finish by tracing a running total with negatives.
Example 1 — Sum of a Fixed Array
Use a range-based for to add every element into an accumulator.
C++
#include <iostream>
using namespace std;
int sumArray(int arr[], int n)
{
int total = 0;
for (int i = 0; i < n; i++)
{
total += arr[i];
}
return total;
}
int main()
{
int array[] = { 1, 2, 3, 4, 5 };
int n = sizeof(array) / sizeof(array[0]);
cout << "Sum of the array elements: " << sumArray(array, n) << endl;
return 0;
}
Output
Sum of the array elements: 15
How It Works
total starts at 0.
The loop visits 1, 2, 3, 4, then 5.
Each visit does total += arr[i].
The running values are 1, 3, 6, 10, then 15.
Return the final total after the loop.
Example 2 — Sum with User Input
Read n, fill an array, then reuse the same summing helper.
C++
#include <iostream>
using namespace std;
int sumArray(int arr[], int n)
{
int total = 0;
for (int i = 0; i < n; i++)
{
total += arr[i];
}
return total;
}
int main()
{
int n;
cout << "Enter number of elements: ";
cin >> n;
if (n < 0)
{
cout << "n must be nonnegative." << endl;
return 1;
}
int arr[100];
if (n > 100)
{
cout << "This demo supports at most 100 elements." << endl;
return 1;
}
for (int i = 0; i < n; i++)
{
cout << "Enter element " << (i + 1) << ": ";
cin >> arr[i];
}
cout << "Sum of the array elements: " << sumArray(arr, n) << endl;
return 0;
}
Output
Enter number of elements: 5
Enter element 1: 1
Enter element 2: 2
Enter element 3: 3
Enter element 4: 4
Enter element 5: 5
Sum of the array elements: 15
How It Works
Read the length n and reject a negative size.
Fill a fixed buffer (or a vector in larger programs).
Call sumArray — input and summing stay separate.
If n is 0, the array is empty and the sum is 0.
Same helper as Example 1, so testing stays easy.
Example 3 — Running Total with Negatives
Print the accumulator after each step so negatives are easy to see.
C++
#include <iostream>
using namespace std;
int main()
{
int arr[] = { 10, -2, 7 };
int n = sizeof(arr) / sizeof(arr[0]);
int total = 0;
cout << "Running total:" << endl;
for (int i = 0; i < n; i++)
{
total += arr[i];
cout << " after " << (arr[i] >= 0 ? "+" : "")
<< arr[i] << ": " << total << endl;
}
cout << "Final sum: " << total << endl;
return 0;
}
Output
Running total:
after +10: 10
after -2: 8
after +7: 15
Final sum: 15
How It Works
Start at 0, then add 10 → 10.
Add -2 → 8 (no special case for negatives).
Add 7 → 15.
A small + prefix keeps the added values easy to read.
Tracing the running total is a strong whiteboard habit.
Edge Cases & Pitfalls
Check these before calling the solution done.
Empty
Sum is 0
An empty array never enters the loop, so the accumulator stays at 0.
Negatives
Just add them
Do not take absolute values unless the problem asks for that.
Overflow
Use a wider type if needed
Very large int arrays may need long long for the accumulator.
Size
Pass length with C arrays
C-style arrays decay to pointers — pass n, or prefer vector<int>.
Analysis
Time and Space Complexity
Program
Time
Extra space
Index / range loop
O(n)
O(1)
User-input build + sum
O(n)
O(n) for the array
Running-total trace
O(n)
O(1)
You must look at every element at least once, so O(n) is optimal for the general case.
Remember
Key Takeaways
Pattern: accumulator starts at 0, then adds each element once.