Skip to content
Laviyx Learn

Try

PPS Lab · Experiment 17

Minimum, maximum and average of an array

CArrays, pointers and functionsJNTUH R22 · CS108ES

Aim

To write a C program that reads n integers into an array and finds the smallest element, the largest element and the average of all the elements.

Algorithm

  1. Read n, the number of elements. If n is not between 1 and 100, print an error and stop.
  2. Read the n integers into the array.
  3. Set min and max to the first element and sum to 0.
  4. For each element, add it to sum. If it is smaller than min, make it the new min. If it is larger than max, make it the new max.
  5. Find the average as sum / n, using real (not integer) division.
  6. Print the minimum, the maximum and the average.

Program

C
#include <stdio.h>

#define MAX 100

int main(void) {
    int a[MAX], n, i;

    printf("Enter the number of elements (1 to %d): ", MAX);
    if (scanf("%d", &n) != 1 || n < 1 || n > MAX) {
        printf("Invalid number of elements\n");
        return 1;
    }

    printf("Enter %d integers: ", n);
    for (i = 0; i < n; i++) {
        if (scanf("%d", &a[i]) != 1) {
            printf("Invalid input\n");
            return 1;
        }
    }

    int min = a[0], max = a[0];
    long sum = 0;
    for (i = 0; i < n; i++) {
        sum += a[i];
        if (a[i] < min) {
            min = a[i];
        }
        if (a[i] > max) {
            max = a[i];
        }
    }
    double average = (double) sum / n;

    printf("Minimum = %d\n", min);
    printf("Maximum = %d\n", max);
    printf("Average = %.2f\n", average);
    return 0;
}

Output

Positive numbers

Output 1
Enter the number of elements (1 to 100): 6
Enter 6 integers: 12 45 7 89 23 56
Minimum = 7
Maximum = 89
Average = 38.67

With negative numbers

Output 2
Enter the number of elements (1 to 100): 5
Enter 5 integers: -4 10 0 -15 8
Minimum = -15
Maximum = 10
Average = -0.20

Recorded by compiling and running this exact program. Typed input appears after each prompt.

How it works

min and max start at the first element, not at 0. If they started at 0, an array of only negative numbers would wrongly report a maximum of 0.

sum is cast to double before dividing, so the average keeps its fraction: 232 / 6 is 38.67, not 38.

Common mistakes

  • Starting min at 0. For an array like 5 8 3 the minimum would wrongly be 0. Start both min and max at a[0].

  • Writing average = sum / n; with sum and n both integers. This is integer division and the fraction is lost. Cast one side: (double) sum / n.

  • Printing the average with %d. The average is a double, so %d prints a wrong number. Use %f (or %.2f for two decimal places).

  • Not checking that n fits in the array. If n is larger than the array size, the program writes past the end of the array.

Viva questions

Try to answer each one aloud before you open it.

  • Why are min and max set to the first element and not to 0?

    Every value in the array is a valid candidate, so starting with a real element is always correct. Starting at 0 gives a wrong answer when all elements are positive (for min) or all are negative (for max).

  • What is the result of 7 / 2 in C, and how do you get 3.5?

    Both are integers, so 7 / 2 is integer division and gives 3. Making one operand a double, as in 7.0 / 2 or (double) 7 / 2, gives 3.5.

  • What is the index of the first and the last element of an array of n elements?

    The first element is at index 0 and the last is at index n − 1.

  • What happens if you access a[n] in an array of size n?

    It is outside the array. C does not check array bounds, so the behaviour is undefined: the program may print garbage, change another variable or crash.

  • How many comparisons does this program make to find both min and max?

    Two comparisons for each element, so 2n comparisons in total.

Learn the concepts

All Programming for Problem Solving Lab experiments →