Start with console input, then an array helper, and finish with std::accumulate.
Example 1 — Average from User Input
Validate N, loop N times with cin, keep a running total, then divide.
C++
#include <iostream>
#include <iomanip>
double averageOfNNumbers(int n) {
if (n <= 0) {
return 0.0;
}
double total = 0.0;
std::cout << "Enter " << n << " numbers:\n";
for (int i = 0; i < n; i++) {
double value;
std::cin >> value;
total += value;
}
return total / n;
}
int main() {
int n = 5;
double result = averageOfNNumbers(n);
std::cout << "The average of the entered numbers is: "
<< std::fixed << std::setprecision(6) << result << "\n";
return 0;
}
Output
Enter 5 numbers:
1
2
3
4
5
The average of the entered numbers is: 3.000000
How It Works
Return 0.0 if n <= 0 (documented sentinel).
Prompt and read each value with std::cin.
Add every value into a double running total.
Divide by n so the mean keeps decimals.
Sample input 1…5 prints 3.000000.
Example 2 — Average from an Array
Loop the array once — short and interview-friendly.
C++
#include <iostream>
#include <iomanip>
double averageFromArray(const double* values, int n) {
if (n <= 0) {
return 0.0;
}
double total = 0.0;
for (int i = 0; i < n; i++) {
total += values[i];
}
return total / n;
}
int main() {
double data[] = { 10, 15, 20, 25, 30 };
int n = sizeof(data) / sizeof(data[0]);
double avg = averageFromArray(data, n);
std::cout << "Average = " << std::fixed << std::setprecision(6) << avg << "\n";
return 0;
}
Output
Average = 20.000000
How It Works
Guard empty or invalid length with a sentinel return.
Sum every element in one forward pass.
Divide by n for the arithmetic mean.
sizeof(data) / sizeof(data[0]) gives the count safely.
10+15+20+25+30 = 100 → average 20.
Example 3 — std::accumulate
Prefer this in application code when a non-empty array is guaranteed.
C++
#include <iostream>
#include <numeric>
int main() {
double data[] = { 1, 2, 3, 4, 5 };
int n = sizeof(data) / sizeof(data[0]);
if (n == 0) {
std::cout << "No average for an empty array.\n";
} else {
double sum = std::accumulate(data, data + n, 0.0);
std::cout << (sum / n) << "\n";
}
return 0;
}
Output
3
How It Works
Detect an empty array before any division.
std::accumulate (from <numeric>) sums the range with a 0.0 seed.
Divide by n for the mean.
Prints 3 for 1…5.
Same math as the helper — less boilerplate for fixed arrays.
Edge Cases & Pitfalls
Check these before calling the solution done.
Zero
Never divide by N = 0
Empty input is undefined. Return an error or a documented sentinel.
int / int
Avoid integer division
10 / 4 is 2 in integer math. Use double (or seed accumulate with 0.0).
Overflow
Sum can grow large
Prefer double (or a wider type) when values or N are large.
Median
Different from median
Average is sum/count. Median needs a sorted middle value.
Analysis
Time and Space Complexity
Program
Time
Extra space
Input / array average
O(N)
O(1) beyond the data
std::accumulate
O(N)
O(1)
Storing all inputs
O(N)
O(N)
One pass is enough. You do not need to sort for an arithmetic mean.
Remember
Key Takeaways
Formula: average = sum / N.
Guard: never divide when N is zero.
Type: use double for fractional results.
Library:std::accumulate(..., 0.0) then divide.
One line: add every value, divide by the count — that is the average.
Frequently Asked Questions
Add all N numbers, then divide by N. Formula: average = sum / N.
Division by zero is invalid. If N is 0, the average is undefined.
Use double for the sum and result when you want decimals. Integer division truncates (10 / 4 becomes 2).
Yes. Sum with std::accumulate from <numeric>, then divide by size. Guard empty containers first.
No. Average is sum/count. Median is the middle value after sorting.