Skip to lab content

UNIT 03 · Arrays, Pointers and Functions

Minimum, maximum and average of an array

EXERCISE 03AC173 sample runs

THE SYLLABUS QUESTION

What you need to solve

Write a C program to find the minimum, maximum and average in an array of integers.
Input format & conventions

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

  1. Read the size and validate each element.
  2. Initialize extrema from the first element and sum to zero.
  3. Scan the array once, updating extrema and sum.
  4. Divide the floating-point sum by the array size.
  5. 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.

Flowchart for Minimum, maximum and average of an array: input, decisions, processing, output and loop paths

On a phone, scroll sideways to read the diagram at full size. Open full-size flowchart ↗

C17

Complete C program

Download .c
#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;
}
Open in compiler ↗

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
./lab

On Windows, run .\lab.exe after compiling with GCC. The interest program requires the math library where applicable.

FOLLOW THE VALUES

Dry run

Step / stateOperationResult
Array: 4, -2, 9, 5After first elementmin = 4; max = 4; sum = 4
After -2Update minimummin = -2; sum = 2
After 9 and 5Update maximum; finish summax = 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

INPUT
4
4 -2 9 5
OUTPUT
Minimum: -2
Maximum: 9
Average: 4.00

Sample 2

INPUT
3
1 2 2
OUTPUT
Minimum: 1
Maximum: 2
Average: 1.67

Sample 3

INPUT
1
-7
OUTPUT
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.