Find Sum of Array in C++
What you’ll learn
- The simplest way to compute a total using an accumulator (
sum += arr[i]). - How to write a clean C++ program for a fixed array and for user input.
- Common edge cases: empty input, negatives, and overflow.
Overview
To find the sum of an array, you visit each element exactly once and keep adding it to a running total. This is one of the most common patterns in programming, and it teaches you how loops and arrays work together.
Two programs
Example 1 sums {1,2,3,4,5}. Example 2 reads n and then n integers from the user.
Live preview
Paste numbers like 10, -2, 7 and see the computed sum instantly.
Safe sum type
We use long long for the total so it is harder to overflow than with plain int.
Live preview
Enter integers separated by commas or spaces (example: 1 2 3 or 1, 2, 3).
Algorithm
Goal: compute a[0] + a[1] + ... + a[n-1].
Initialize
Set sum = 0.
Loop through the array
For every index i from 0 to n-1, add a[i] into sum.
Return or print
After the loop, sum is the total.
📜 Pseudocode
function arraySum(a, n):
sum ← 0
for i from 0 to n-1:
sum ← sum + a[i]
return sum Sum of a fixed array
This matches the classic example: {1,2,3,4,5} sums to 15.
#include <iostream>
long long sum_array(const int arr[], int size) {
long long sum = 0;
for (int i = 0; i < size; ++i) {
sum += arr[i];
}
return sum;
}
int main() {
int array[] = {1, 2, 3, 4, 5};
int size = (int)(sizeof(array) / sizeof(array[0]));
long long sum = sum_array(array, size);
std::cout << "Sum of the array elements: " << sum << "\\n";
return 0;
} Sum of an array (user input)
Reads n, then reads n integers. Uses long long for the running total.
#include <iostream>
int main() {
int n;
std::cout << "Enter number of elements: ";
if (!(std::cin >> n) || n < 0) {
std::cout << "Invalid n.\\n";
return 0;
}
long long sum = 0;
for (int i = 0; i < n; ++i) {
long long x;
std::cout << "Enter element " << (i + 1) << ": ";
if (!(std::cin >> x)) {
std::cout << "Invalid input.\\n";
return 0;
}
sum += x;
}
std::cout << "Sum of the array elements: " << sum << "\\n";
return 0;
} ❓ FAQ
🔄 Input / output examples
| Array | Sum |
|---|---|
{1,2,3,4,5} | 15 |
{10,-2,7} | 15 |
{} (n=0) | 0 |
Edge cases and pitfalls
Summing is simple, but these details keep your solution correct in interviews.
Empty array
The sum of zero numbers is 0. The loop runs zero times and sum stays 0.
Total may exceed int
If numbers are large (or there are many), int can overflow. Using long long is safer.
Invalid reads
Always validate std::cin reads in interactive programs so you don’t use bad values.
⏱️ Time and space complexity
| Approach | Time | Extra space |
|---|---|---|
| One loop through n elements | O(n) | O(1) |
Summary
- Pattern:
sum = 0, thensum += arr[i]for each element. - Use: choose
long longfor safer totals. - Complexity:
O(n)time,O(1)extra space.
The “sum of an array” program is the simplest example of the accumulator pattern: start with sum = 0, then add each element once. This idea appears everywhere: totals, averages, dot products, and prefix sums.
9 people found this page helpful
