THE SYLLABUS QUESTION
What you need to solve
Write a C program to find the minimum, maximum and average in an array of integers.
Array size n (1–100), followed by n integers between -1,000,000 and 1,000,000.
UNDERSTAND THE IDEA
Explanation
Store the input array, then use its first element to initialize both extrema. Visit every element to update the minimum, maximum and sum.
Cast the sum to double before division so a fractional average is preserved. A long long sum safely accommodates the supported array bounds.
PLAN BEFORE CODING
Algorithm
- Read the size and validate each element.
- Initialize extrema from the first element and sum to zero.
- Scan the array once, updating extrema and sum.
- Divide the floating-point sum by the array size.
- Print all three results.
SEE THE CONTROL FLOW
Flowchart
Follow the arrows from Start. Diamonds ask a question; labeled arrows show the answer. A returning arrow repeats a loop. Function internals are grouped where needed; later input checks follow the rules in the program.
On a phone, scroll sideways to read the diagram at full size. Open full-size flowchart ↗
#include <stdio.h>
#define MAX_SIZE 100
int main(void) {
int a[MAX_SIZE], n, minimum, maximum;
long long sum = 0;
if (scanf("%d", &n) != 1 || n < 1 || n > MAX_SIZE) {
puts("Invalid input."); return 1;
}
for (int i = 0; i < n; ++i) {
if (scanf("%d", &a[i]) != 1 || a[i] < -1000000 || a[i] > 1000000) {
puts("Invalid input."); return 1;
}
}
minimum = maximum = a[0];
for (int i = 0; i < n; ++i) {
if (a[i] < minimum) minimum = a[i];
if (a[i] > maximum) maximum = a[i];
sum += a[i];
}
printf("Minimum: %d\nMaximum: %d\nAverage: %.2f\n",
minimum, maximum, (double)sum / n);
return 0;
}
Code loads into the existing compiler. Enter the sample input there; sign-in and execution rules stay the same.
Compile and run locally
gcc -std=c17 array-statistics.c -o lab
./labOn Windows, run .\lab.exe after compiling with GCC. The interest program requires the math library where applicable.
FOLLOW THE VALUES
Dry run
| Step / state | Operation | Result |
|---|---|---|
| Array: 4, -2, 9, 5 | After first element | min = 4; max = 4; sum = 4 |
| After -2 | Update minimum | min = -2; sum = 2 |
| After 9 and 5 | Update maximum; finish sum | max = 9; sum = 16; average = 4 |
CHECK THE BEHAVIOR
Sample input & output
Each output below was produced by compiling and running this exact program. Input values are entered in the stated order; the examples do not print input prompts.
Sample 1
4
4 -2 9 5
Minimum: -2
Maximum: 9
Average: 4.00
Sample 2
3
1 2 2
Minimum: 1
Maximum: 2
Average: 1.67
Sample 3
1
-7
Minimum: -7
Maximum: -7
Average: -7.00
Common mistakes
- Reject an empty array before accessing element zero.
- Integer sum / n discards the fractional part unless converted first.
WHY THIS GROWTH RATE?
Time and space complexity
Time O(n); O(n) array storage and O(1) additional working space.
Let n be the array size. Reading n values takes O(n). A second scan examines each value once and updates minimum, maximum and sum with constant work per element. The final division takes O(1).
Adding the costs gives O(n) + O(n) + O(1) = O(n), not O(n²): the loops run one after another. The array stores n values (O(n) input storage), while extrema, sum and counters use O(1) additional working space.
Big-O describes how work grows as the stated input quantity grows; fixed factors and lower-order terms are omitted. The analysis treats fixed-width arithmetic as constant cost and the published limits as practical safety bounds.