C Program to find the average of all elements in an array
Learn how to find the average of all elements in an array in C, with source code, examples, a dry run, and an explanation of integer division.
The average of an array tells us the central value of its elements. We calculate it by adding all the elements and dividing the sum by the number of elements.
For an array containing 8, 12, 7, 15, 10:
Sum = 8 + 12 + 7 + 15 + 10 = 52
Average = 52 / 5 = 10.4
Because an average can contain a fractional part, the program should store the result in a floating-point type such as double.
C Program to Find the Average of Array Elements
#include <stdio.h>
#define MAX_SIZE 100
int main(void) {
int array[MAX_SIZE];
int size;
long long sum = 0;
double average;
printf("Enter the number of elements: ");
if (scanf("%d", &size) != 1 || size < 1 || size > MAX_SIZE) {
printf("Please enter a size between 1 and %d.\n", MAX_SIZE);
return 1;
}
printf("Enter %d elements:\n", size);
for (int index = 0; index < size; index++) {
if (scanf("%d", &array[index]) != 1) {
printf("Invalid array element.\n");
return 1;
}
sum += array[index];
}
average = (double)sum / size;
printf("Sum = %lld\n", sum);
printf("Average = %.2f\n", average);
return 0;
}
Sample Output
Enter the number of elements: 5
Enter 5 elements:
8 12 7 15 10
Sum = 52
Average = 10.40
The elements may also be entered on separate lines. When reading integers, scanf() treats spaces and line breaks as separators.
How the Program Works
array[MAX_SIZE]creates space for up to 100 integers.sumstarts at0and uses thelong longtype so it can hold a larger total than a regularinton most systems.- The
forloop reads each array element and immediately adds it tosum. (double)sumconverts the sum to a floating-point value before division.%.2fdisplays the average with two digits after the decimal point.
Here is a dry run for the sample array:
| Iteration | Current element | Sum after addition |
|---|---|---|
| 1 | 8 | 8 |
| 2 | 12 | 20 |
| 3 | 7 | 27 |
| 4 | 15 | 42 |
| 5 | 10 | 52 |
After the loop, the program divides 52 by 5 and obtains an average of 10.4.
Why Convert the Sum to double?
In C, dividing one integer by another performs integer division. Integer division removes the fractional part of the result.
For example:
int sum = 52;
int size = 5;
double wrongAverage = sum / size; // 10.0
double correctAverage = (double)sum / size; // 10.4
In the first calculation, 52 / 5 is evaluated as integer division and becomes 10 before it is assigned to wrongAverage. Casting sum to double makes the division floating-point division, preserving the fractional part.
Using a Function to Calculate the Average
A separate function makes the calculation reusable and keeps main() focused on input and output.
#include <stdio.h>
double findAverage(const int array[], int size) {
long long sum = 0;
for (int index = 0; index < size; index++) {
sum += array[index];
}
return (double)sum / size;
}
int main(void) {
int numbers[] = {4, 9, 11, 6};
int size = sizeof(numbers) / sizeof(numbers[0]);
printf("Average = %.2f\n", findAverage(numbers, size));
return 0;
}
Output
Average = 7.50
The const keyword indicates that findAverage() reads the array but does not modify its elements. This function expects size to be greater than zero so that it never divides by zero.
Handling Negative Numbers
The same program works with negative numbers. For example, the average of -5, 10, -3, 6 is:
Sum = -5 + 10 - 3 + 6 = 8
Average = 8 / 4 = 2.0
No special condition is needed because addition and floating-point division already handle negative values correctly.
Time and Space Complexity
- Time complexity:
O(n), because every array element is visited once. - Extra space complexity:
O(1), because the calculation uses onlysum,average, and a loop variable in addition to the input array.
Common Mistakes
- Performing integer division and losing the fractional part of the average.
- Forgetting to initialize
sumto0before the loop. - Dividing by zero when the array contains no elements.
- Dividing by
MAX_SIZEinstead of the actual number of entered elements. - Using
%dto print adouble; floating-point results should use a format such as%.2f.
The complete solution requires only one traversal: add every element to the running sum, convert the sum to double, and divide it by the number of elements.