Capping an Array

Compared to other programming languages, C is weak when it comes to dealing with arrays. The array has a starting point and a variable type. That’s pretty much it. Your code determines where the array ends. That type of programming discipline terrifies coders of other languages.

When you declare an array in C, you specify its maximum size — its allocated memory. You don’t have to use all that storage, but the program can’t easily expand the array size while it’s running. So, like many C programmers, you guess when you set an array’s size. For example:

float grades[32];

In your grade-tracking program, you hope that the Legislature keeps class size to a maximum of 32 students. If that value changes, the code must be recompiled. Rather than do that all the time, you fudge and pull a number out of your binary hat, a comfortable value not too big:

float grades[64];

A student-teacher ratio of 64:1 isn’t going to happen (let’s hope), but in this instance you’re wasting storage. That may not be a big issue for single variables, but consider an array of complex structures or other values that consume lots of storage.

Regardless of memory or mass storage capacity, always be mindful not to waste system resources.

Wherever you set the mark, in the C language the number of elements in the array is specified as a literal value or constant. Once that’s done, you can use a variable to manipulate the array’s elements. As long as the variable’s value doesn’t exceed the size of the array, you’re okay.

Another approach is to borrow from a character array, and mark the array’s last element with a zero value; just as strings in C use the null character, \0, you could use the integer value 0 (zero) to cap a numeric array. The following code demonstrates:

#include <stdio.h>

#define MAX 10

int main()
{
    int number[MAX];
    int count;

    count = 0;
    while(count < MAX)
    {
        printf("Type your favorite number, 0 to end: ");
        scanf("%d",&number[count]);
        if( number[count] == 0 )
            break;
        count++;
    }

    count = 0;
    puts("Your favorite numbers are:");
    while(count < MAX)
    {
        if( number[count] == 0 )
            break;
        printf("%d\n",number[count]);
        count++;
    }

    return(0);
}

This code fills an array with up to 10 integer values. Fewer than 10 values can be specified. To do so, the user types 0 to cap the array. To accommodate for that condition, each while loop in the code tests first for count < MAX but also for number[count] == 0. Similar to a character array, the zero caps the input.

Here’s a sample run:

Type your favorite number, 0 to end: 2
Type your favorite number, 0 to end: 3
Type your favorite number, 0 to end: 5
Type your favorite number, 0 to end: 7
Type your favorite number, 0 to end: 9
Type your favorite number, 0 to end: 0
Your favorite numbers are:
2
3
5
7
9

This approach may seem boring and unnecessary. After all, the count variable keeps track of the array elements. You can use that variable in the rest of the code to ensure that only the proper number of elements are read. Would it work? Sure, but it might not be the best way to deal with a bunch of numbers.

In next week’s Lesson, I’ll show how the zero cap can be used with an average() function.