Once a set of marks is stored in an array, the most common questions follow at once: what is the highest mark, the lowest, the total and the average? All four are answered by one pass through the array.

Traversing an Array

Definition: Traversal. Visiting every element of an array exactly once, in order, usually to examine or process it.

A traversal is written as a for loop whose index runs from 0 to n − 1. The four statistics of this topic are all computed during a traversal.

Sum and Mean

The sum is found with a running total that starts at 0. Each element is added to it in turn.

Definition: Mean. The arithmetic average of a set of values, equal to their sum divided by their number.

The mean is computed after the loop as sum divided by n. If the sum and n are both integers, one of them must be converted to a real type, as in (float)sum / n; otherwise the fractional part is lost.

Maximum and Minimum

The largest value is found by keeping the largest value seen so far.

  1. Assume that the first element, a[0], is the largest: max = a[0].
  2. Compare each remaining element with max. If an element is larger, it becomes the new max.
  3. After the traversal, max holds the largest value.

The minimum is found in the same way with the comparison reversed.

Table 4.4: Trace of the maximum for the array 34, 78, 12, 90, 56

i a[i] Is a[i] > max? max after the step
start 34 — 34
1 78 Yes 78
2 12 No 78
3 90 Yes 90
4 56 No 90

Why Start from the First Element

Starting max at 0 fails when every element is negative, and starting min at 0 fails when every element is positive. Starting both from a[0] is always correct, because that value really is in the array. The loop can then begin at index 1.

One Pass for All Four

The sum, the maximum and the minimum can be updated inside the same loop, so a single traversal is enough. For an array of n elements the work is proportional to n, which is the least possible, since every element must be examined at least once.

Position of the Maximum

Sometimes the position is wanted as well as the value, for example to name the student with the highest mark. The index is then recorded each time max is updated: pos = i.

Example 4.4: Statistics of an array of marks

Problem: Read n marks (n at most 50) into an array and display the maximum, minimum, sum and mean.

Solution:

  1. Read n and the n marks.
  2. Start max and min from the first element and sum from 0.
  3. Update all three in one traversal, then compute the mean.
#include <stdio.h>

int main(void)
{
    int a[50], n, i, max, min, sum = 0;

    printf("How many marks (1-50)? ");
    scanf("%d", &n);
    if (n < 1 || n > 50)
    {
        printf("\nInvalid count\n");
        return 0;
    }
    printf("Enter the marks: ");
    for (i = 0; i < n; i++)
    {
        scanf("%d", &a[i]);
    }
    max = a[0];
    min = a[0];
    for (i = 0; i < n; i++)
    {
        sum = sum + a[i];
        if (a[i] > max)
        {
            max = a[i];
        }
        if (a[i] < min)
        {
            min = a[i];
        }
    }
    printf("\nMaximum = %d\n", max);
    printf("Minimum = %d\n", min);
    printf("Sum     = %d\n", sum);
    printf("Mean    = %.2f\n", (float)sum / n);
    return 0;
}

Input:

5
34 78 12 90 56

Output:

How many marks (1-50)? Enter the marks:
Maximum = 90
Minimum = 12
Sum     = 270
Mean    = 54.00

Result: The sum is 270 and the mean is 270 / 5 = 54.00. The check on n protects the array bounds and also guarantees that the mean never divides by zero.

Common mistakes

  • Starting max or min at 0 instead of the first element.
  • Using integer division for the mean.
  • Reading more values than the declared size of the array allows.

Key takeaways

  • A traversal visits every element once with a loop from 0 to n − 1.
  • The sum uses a running total that starts at 0.
  • max and min start from the first element of the array.
  • The mean is the sum divided by the number of elements, using real division.

Practice questions

  1. Find the mean of the array 3, 8, 5.
  2. With which value should max be started?
  3. How many comparisons are needed to find the maximum of n elements?

Answers to the practice questions

  1. 16 / 3, which is about 5.33.
  2. The first element of the array, a[0].
  3. n − 1 comparisons.

Sources

  • Official BCA syllabus (NEP 2025 scheme), Problem Solving Techniques, course code 25BCA401SEC01, Unit IV