Arrays

Hip, Hip, Array!

So far, the variables that we have used have been single values. However, it is often useful to group lots of values together under a single name. This is where arrays come in. An array can be thought of as a sequence of pigeon holes, as shown.

A one-dimensional array

This contains 7 pigeon holes, numbered from 0 to 6. Each of these holes contains a number. The whole of the array is referred to by the same name (for instance, temperatures) and the individual slots are referred to by putting the slot number in square brackets after the name, temperature[0] for the first one, temperature[1] for the second, up to temperature[6] for the last. These slots are usually called array elements.

C++ numbers the array indices from 0. They have to be declared, just as any other variable would be:

#include <iostream.h>
void main ()
    { int temperatures[6]; // array of 6 values (0 to 5)
      int i,total;

      cout << "Please enter temperature 0 : ";
      cin >> temperatures[0];
      cout << "Please enter temperature 1 : ";
      cin >> temperatures[1];
      cout << "Please enter temperature 2 : ";
      cin >> temperatures[2];
      cout << "Please enter temperature 3 : ";
      cin >> temperatures[3];
      cout << "Please enter temperature 4 : ";
      cin >> temperatures[4];
      cout << "Please enter temperature 5 : ";
      cin >> temperatures[5];

      total = 0;
      for (i = 0; i <= 5; i++)
         total += temperatures[i];

      cout << "The sum of the temperatures is "
           << total << endl;
      cout << "The average temperature is " <<
              (total / 6) << endl;
    }

The third line is the one where the array is declared. You use int or float or char or whatever and the name of the array, and put [100] afterwards, where the number inside the brackets is the index of the last element.

The program asks you for each array element in turn, then works out the total and the average. The adding up is done in a loop near the end that uses the variable i as the loop counter. There is a more efficient way of getting the values in from the user. Replace the first section of the program by this:

for (i = 0; i <= 5; i++)
   { cout << "Please enter temperature " << i << " : ";
     cin >> temperatures[i];
   }

You can see that the numbers specifying the element of the array (called the array indices) can be variables, or expressions that produce a number. Just because the array indices start at 0 doesn't mean that you have to use element 0. You might declare an array of 20 elements and then just use the last five, or only the even numbered ones etc.

Expressions as Array Indices

The indices of the array elements (the numbers telling you which array element you are considering) have so far been simple numbers. However, there is no reason why they can't be mathematical expressions. Here's what I mean. This is a fragment from a program (you will notice that the declarations of the array variable and all the other paraphenalia is missing) that shows an expression as an array index:

x = 20;
cout << my_array[52 - 2 * x];

In this case, the value of 52 - 2 * x is 12 so the value displayed on the screen is whatever is in my_array[12].

Two-dimensional Arrays

The array up above is a one-dimensional array. It contains a horizontal line of boxes. You can turn this into a rectangular grid of boxes by adding another dimension, as shown below. You would declare an array like that like this (if you see what I mean) :

int grid_values[3][7];
Two-dimensional array

and you can set and access the elements just as you would do with any other variable:

grid_values[1][5] = 2300;
x = 0;
y = 4;
cout << grid_values[x][y];

Here is a loop (well, one loop nested inside another) that asks you to fill the elements of the array:

for (i = 0; i <= 6; i++)
   for (j = 0; j <= 2; j++)
     { cout << "Type in a value for element "
            << i << " " << j << " : ";
       cin >> grid_values[j][i];
     }

Here, the variable j goes along the rows and the variable i goes down the columns. It produces output like this:

Type in a value for element 0 0 : 23
Type in a value for element 0 1 : 100
Type in a value for element 0 2 : -30
Type in a value for element 0 3 : 12
Type in a value for element 0 4 : 651
Type in a value for element 0 5 : -450
Type in a value for element 0 6 : 533
Type in a value for element 1 0 : 1022
Type in a value for element 1 1 : 204
Type in a value for element 1 2 : 23
               etc.

Multidimensional Arrays!

There is no reason why you can't have arrays of three or more dimensions, although they are difficult to draw (see below). Here's how you declare a three-dimensional array - it shouldn't come as any surprise to you:

char letters[3][3][7];
Three-dimensional array

I decided to make it an array of characters. Each element would hold one letter/space/punctuation symbol. Remember, all array indices start at 0, so the first element in this array is letters[0][0][0] and the last is letters[2][2][6]. This particular array contains 3 x 3 x 7 = 63 elements.


Go back
Top of the page
Main Menu
Questions
Worked Examples
Go on
Go back
Top of the page
Main Menu
Questions
Worked Examples
Go on