Experiment 6 - Array Statistics (Sum, Average, Min, Max)
Aim
To read an array of numbers, calculate and display statistical measures including sum, average, maximum, and minimum values using array operations and loops in C programming. This experiment helps students understand array traversal, accumulation operations, and finding extreme values in datasets.
Through this experiment, students will learn to process arrays efficiently, implement common statistical calculations, and understand how to find maximum and minimum values through iteration.
Algorithm
The algorithm for calculating array statistics involves reading elements, accumulating sum, and finding extremes:
Algorithm Steps:
- Start: Begin program execution
- Declare: Array, size variable, sum, max, min, average, loop counter
- Input Size: Read number of elements n
- Read Array: Use loop to read n elements into array
- Initialize: Set sum = 0, max = arr[0], min = arr[0]
- Process Array: For each element from index 0 to n-1
- Add element to sum: sum = sum + arr[i]
- If arr[i] > max, update max = arr[i]
- If arr[i] < min, update min = arr[i]
- Calculate Average: average = (float)sum / n (type cast for decimal result)
- Display Results: Print sum, average, maximum, and minimum
- Stop: End program execution
Procedure
- Include stdio.h header file
- Declare an array and variables for size, sum, average, max, min
- Read the size of array from user
- Read array elements using a loop
- Calculate sum by iterating through array and adding all elements
- Calculate average: average = sum / size
- Find maximum:
- Initialize max = arr[0]
- Compare each element with max, update if larger
- Find minimum:
- Initialize min = arr[0]
- Compare each element with min, update if smaller
- Display all calculated statistics
Flowchart
START | +-> Declare: arr[], n, sum=0, avg, max, min, i | +-> Read n (size of array) | +-> FOR i = 0 to n-1 | | | +-> Read arr[i] | +-> Initialize: max = arr[0], min = arr[0] | +-> FOR i = 0 to n-1 | | | +-> sum = sum + arr[i] | | | +-> IF (arr[i] > max) | | | | | +-> max = arr[i] | | | +-> IF (arr[i] < min) | | | +-> min = arr[i] | +-> avg = sum / n | +-> Display sum, avg, max, min | +-> END
Program Code
#include <stdio.h>
int main() {
int arr[100], n, i;
int sum = 0, max, min;
float avg;
printf("Enter the number of elements: ");
scanf("%d", &n);
printf("Enter %d elements:\n", n);
for (i = 0; i < n; i++) {
printf("Element %d: ", i + 1);
scanf("%d", &arr[i]);
}
max = arr[0];
min = arr[0];
for (i = 0; i < n; i++) {
sum += arr[i];
if (arr[i] > max) {
max = arr[i];
}
if (arr[i] < min) {
min = arr[i];
}
}
avg = (float)sum / n;
printf("\n=== ARRAY STATISTICS ===\n");
printf("Array elements: ");
for (i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n\n");
printf("Sum: %d\n", sum);
printf("Average: %.2f\n", avg);
printf("Maximum: %d\n", max);
printf("Minimum: %d\n", min);
return 0;
}Sample Input and Output
Sample 1:
Input:
Enter the number of elements: 5 Enter 5 elements: Element 1: 10 Element 2: 25 Element 3: 15 Element 4: 30 Element 5: 20
Output:
=== ARRAY STATISTICS === Array elements: 10 25 15 30 20 Sum: 100 Average: 20.00 Maximum: 30 Minimum: 10
Use Case / Real-world Relevance
Array statistics are fundamental operations used extensively in real-world applications:
- Data Analysis: Calculating averages, finding peaks and valleys in datasets
- Business Intelligence: Analyzing sales data, finding best/worst performing products
- Gaming: Tracking scores, finding highest/lowest scores, calculating average performance
- Weather Applications: Finding maximum/minimum temperatures, average rainfall
- Financial Software: Analyzing stock prices, calculating average returns, finding peaks
Viva Questions
Q1: Why do we initialize max and min with arr[0] instead of 0?
Initializing with arr[0] ensures the algorithm works correctly even if all array elements are negative. If we initialized max with 0 and all elements were negative, max would remain 0 (incorrect). Using arr[0] guarantees that max and min will be actual array values.
Q2: Why do we use (float)sum instead of just sum when calculating average?
Type casting to float ensures floating-point division instead of integer division. Without casting, sum/n performs integer division (truncates decimal part). For example, 7/3 = 2, but (float)7/3 = 2.33. This gives accurate average with decimal precision.
Q3: Can we find max and min in a single loop? How?
Yes, we can find both in one loop by checking both conditions in each iteration: if (arr[i] > max) max = arr[i]; if (arr[i] < min) min = arr[i];. This is more efficient than using separate loops. The provided code already does this efficiently.
Q4: What is the time complexity of finding sum, average, max, and min?
All operations have O(n) time complexity where n is the array size, because we need to examine each element once. We can calculate all statistics in a single pass through the array, making it O(n) overall. Space complexity is O(1) as we only use a few extra variables.
Q5: What happens if the array is empty (size = 0)?
If size is 0, accessing arr[0] for initialization would be invalid, and division by zero would occur when calculating average. Always check if size > 0 before processing. Add validation: if (n <= 0) { printf("Invalid size"); return 1; }.
🔹 Author: Dr. J. Siva Ramakrishna
🔹 Institution: Narayana Engineering College, Gudur
🔹 Last Updated: 9 January 2026