C Program to Find Average of Array Elements
To find the average of an array in C, sum all elements using a loop and then divide by the number of elements, like
average = sum / (float)size;.Examples
InputArray: [5, 10, 15]
OutputAverage: 10.00
InputArray: [1, 2, 3, 4, 5]
OutputAverage: 3.00
InputArray: [0, 0, 0, 0]
OutputAverage: 0.00
How to Think About It
To find the average, first add all numbers in the array to get the total sum. Then, divide this sum by how many numbers are in the array. This gives the average value.
Algorithm
1
Get the size of the array.2
Initialize a variable to store the sum as 0.3
Loop through each element of the array and add it to the sum.4
Divide the sum by the size of the array to get the average.5
Print or return the average.Code
c
#include <stdio.h> int main() { int arr[] = {1, 2, 3, 4, 5}; int size = sizeof(arr) / sizeof(arr[0]); int sum = 0; for (int i = 0; i < size; i++) { sum += arr[i]; } float average = sum / (float)size; printf("Average: %.2f\n", average); return 0; }
Output
Average: 3.00
Dry Run
Let's trace the array [1, 2, 3, 4, 5] through the code to find the average.
1
Initialize sum and size
sum = 0, size = 5
2
Add elements to sum
sum = 1 + 2 + 3 + 4 + 5 = 15
3
Calculate average
average = 15 / 5 = 3.0
4
Print average
Output: Average: 3.00
| Iteration | Element | Sum after addition |
|---|---|---|
| 1 | 1 | 1 |
| 2 | 2 | 3 |
| 3 | 3 | 6 |
| 4 | 4 | 10 |
| 5 | 5 | 15 |
Why This Works
Step 1: Summing elements
We add each element of the array using a loop to get the total sum.
Step 2: Calculating average
We divide the total sum by the number of elements using sum / (float)size to get a float result.
Step 3: Printing result
We print the average with two decimal places using printf("%.2f").
Alternative Approaches
Using a function to calculate average
c
#include <stdio.h> float findAverage(int arr[], int size) { int sum = 0; for (int i = 0; i < size; i++) { sum += arr[i]; } return sum / (float)size; } int main() { int arr[] = {10, 20, 30}; int size = sizeof(arr) / sizeof(arr[0]); float avg = findAverage(arr, size); printf("Average: %.2f\n", avg); return 0; }
This approach improves code reuse by separating logic into a function.
Using floating-point array for more precise sum
c
#include <stdio.h> int main() { float arr[] = {1.5, 2.5, 3.5}; int size = sizeof(arr) / sizeof(arr[0]); float sum = 0.0f; for (int i = 0; i < size; i++) { sum += arr[i]; } float average = sum / size; printf("Average: %.2f\n", average); return 0; }
This method handles decimal numbers directly for more accurate averages.
Complexity: O(n) time, O(1) space
Time Complexity
The program loops through all n elements once to sum them, so time complexity is O(n).
Space Complexity
Only a few variables are used regardless of input size, so space complexity is O(1).
Which Approach is Fastest?
All approaches have similar O(n) time; using a function adds clarity but no speed difference.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Simple loop in main | O(n) | O(1) | Quick scripts or learning |
| Function for average | O(n) | O(1) | Reusable code and clarity |
| Floating-point array | O(n) | O(1) | Handling decimal values accurately |
Always cast the divisor to float to avoid integer division when calculating average.
Forgetting to cast the divisor to float causes integer division and incorrect average.