C Arrays

In C, an array is a special type of variable that contains multiple values of the same type. The following integer variable, ‘age’, has been defined and initialised with the value 30.

int age = 30;

If multiple ages need to be stored, instead of having a different variable for each, an array could be used.

int age[4];
age[0] = 30;
age[1] = 24;
age[2] = 53;
age[3] = 19;

The first line in the above example declares an array called ‘age’, which holds four integer elements. Each element in an array has an index number, which is used in lines two to five above to populate the four elements. Notice that the first index number is a zero and not a one.

The above example can be shortened by declaring and initialising the array in one statement.

int age[4] = {30, 24, 53, 19};

If an array is declared and initialised in this way, then the size in the square brackets can be omitted. In this case the size of the array is determined by the number of values that it is being initialised with.

int age[] = {30, 24, 53, 19};

It should be noted that if the array size is specified within the square brackets and only some of the elements are initialised, then the remainder are automatically initialised to zero.

As well as being able to use the index number to populate a particular element in an array, it can also be used to extract the value of an element in an array, in order to carry out a particular task, or simply just to display it.

printf("%d\n", age[2]);

Here, the element with an index number of two would be displayed, in this case 53.

In order to display all of the elements in an array a ‘for’ loop can be used.

int age[] = {30, 24, 53, 19};

for (int i = 0; i < sizeof(age)/sizeof(age[0]); ++i)
{
    printf("%d\n", age[i]);
}

The above example uses the ‘sizeof’ function to help determine the length, or size of the array. The ‘sizeof’ function returns the number of bytes of memory that the array takes up, which is why it needs to be divided by the size, or number of bytes, for one of the elements, in order to determine the number of elements that there are. The result of this calculation dictates the number of iterations through the loop. The resulting output will list all the elements of the array on a separate line.

30
24
53
19

Multidimensional Arrays

Multidimensional arrays are useful where you have more than one piece of information, so, to continue with the above example, if it were necessary to store the ages of different groups of people, with four people in each group.

int age[][4] = {
    {30, 24, 53, 19},
    {69, 48, 17, 50},
    {21, 62, 78, 44},
    {32, 41, 36, 39},
    {27, 63, 47, 22}
};

A two dimensional array, such as the one above is in effect an array of arrays. It should be noted that the size of the inner arrays must be specified, in the above case, four, but it isn’t necessary to with the outer array.

In order to process the information in a two dimensional array, nested ‘for’ loops need to be used, one to process the rows and another for the columns within each row. In the example below, the calculations to compute the number of rows and columns have been taken out of the ‘for’ loop definitions for simplicity sake, however, there is no reason why these can’t be directly inside the loop, as in the above example.

The inner loop displays the group values, separated by a space, on the same line, in the console. With each iteration through the loop, the age is added to the group total and once all the values in the group have been displayed, an average is calculated and displayed. The average is rounded to the nearest whole number.

int age[][4] = {
    {30, 24, 53, 19},
    {69, 48, 17, 50},
    {21, 62, 78, 44},
    {32, 41, 36, 39},
    {27, 63, 47, 22}
};

// Number of rows = Total array size / size of first inner array.
int rows = sizeof(age)/sizeof(age[0]);

// Number of columns = Size of first inner array / size of first element in inner array.
int cols = sizeof(age[0])/sizeof(age[0][0]);

float groupTotal = 0.0;

// Process the rows in the outer array.
for (int i = 0; i < rows; ++i)
{
    // Process the columns in the inner array.
    for (int j = 0; j < cols; ++j)
    {
        // Add the age to the group total.
        groupTotal += age[i][j];

        // Display the age.
        printf("%d ", age[i][j]);
    }
    // Calculate and display the group average age.
    printf("   Average Group Age: %.0f\n", groupTotal / cols);

    // Reset the group total.
    groupTotal = 0.0;
}

The output from this is shown below.

30 24 53 19   Average Group Age: 32
69 48 17 50   Average Group Age: 46
21 62 78 44   Average Group Age: 51
32 41 36 39   Average Group Age: 37
27 63 47 22   Average Group Age: 40