5.3 Multi-Dimensional Arrays

All the arrays we have used so far have been one-dimensional. They are like a table with only one row. Sometimes it is useful to create a table with more than one row. A two-dimensional array can be used to represent such a table. A spreadsheet program is another example of a two-dimensional type of table.

Suppose a teacher wanted to keep track of the number of students in his classes that had a particular letter grade. The letter grades range from 'A' to 'E'. There are three classes in a particular course. The array can be thought of as a table with the following form:

'A'
'B'
'C'
'D'
'E'
  1  # of A's in section 1# of B's in section 1# of C's in section 1# of D's in section 1# of E's in section 1
  2# of A's in section 2# of B's in section 2# of C's in section 2# of D's in section 2# of E's in section 2
  3# of A's in section 3# of B's in section 3# of C's in section 3# of D's in section 3# of E's in section 3

We can declare the array like this:

var marks : array 1 .. 3, 'A' .. 'E' of int 

You specify the indices for the rows first and then after a comma you specify the indices for the columns.

When we wanted to do something with an entire 1-D array we used a for loop. To do something with an entire 2-D array we will need to use two-level nested for loops. For example, to initialize the above array so that all elements are 0 we would do the following:

var marks : array 1 .. 3, 'A' .. 'E' of int 

for row : 1 .. 3
   for col : 'A' .. 'E'
      marks(row, col) := 0
   end for
end for 

To refer to elements in a 2-D array you need to specify two indices. For instance, to refer to the top left element in the marks array we would say marks (1, 'A').

You can also do things with individual rows or columns. Suppose we wanted to know how many students got a 'B' in all 3 classes. The following code will calculate that:

var bTotal : int := 0

for row : 1 .. 3
   bTotal := bTotal + marks (row, 'B')
end for 

If we wanted to know how many students there are in class two we could use this code:

var total2 : int := 0

for col : 'A' .. 'E'
   total2 := total2 + marks (2, col)
end for 

To find the total number of students do the following:

var total : int := 0

for row : 1 .. 3
   for col : 'A' .. 'E'
      total := total + marks(row, col)
   end for
end for 

Although we will restrict ourselves to a maximum of 2 dimensions for our arrays, it is possible to use higher numbers of dimensions. However, it is rather rare to see more than 3 dimensions for arrays.

Exercise 5.3

  1. How many elements can be stored in each of the following arrays?
    var a : array 1 .. 12, 1970 .. 1990 of real
    var b : array 'a' .. 'j', 0 .. 20 of char
    var c : array -5 .. 5, -5 .. 5 of int
    var d : array 0 .. 4, 'A' .. 'E', 1985 .. 1989 of int
    var e : array -2 .. 6, 0 .. 5 of char 
  2. State the error(s) in each fragment.
    1. var array 1 .. 10, 0 .. 4 of real 
    2. var low, high : int
      var table : array low .. high, low .. high of char 
    3. var scores array -5 .. 10, 5 .. -10 of int 
    4. var matrix : 1 .. 10, 1..10 of real 
  3. Suppose that an array a has been created by the declaration
    var a : array 1 .. 2, 1 .. 4 of int 
    and that a has been given values by the following fragment:
    var k : int := 0
          					
    for i : 1 .. 2
       for j : 1 .. 4
          k := k + 1
          a(i, j) := k
       end for
    end for 
    Determine what will be written by each fragment.
    1. for i : 1 .. 2
         for decreasing j : 4 .. 1
            put a(i, j):2 ..
         end for
      end for 
    2. for decreasing i : 2 .. 1
         for decreasing j : 4 .. 1
            put a(i, j):2 ..
         end for
      end for 
    3. for i : 1 .. 4
         for j : 1 .. 2
            put a(j, i):2 ..
         end for
      end for 
    4. for decreasing j : 4 .. 1
         for i : 1 .. 2
            put a(i, j):2 ..
         end for
      end for 
  4. Write one or more statements to perform each operation.
    1. Declare an array containing three rows (numbered one to three) and five columns (numbered one to five) of integers.
    2. Set all the elements of the array to zero.
    3. Find the sum of all the elements in the array and store this sum in grandTotal.
    4. Find the sum of the elements in the second row and store this value in row2sum.
    5. Find the sum of the elements in the third column and store this value in col3sum.
    6. Find the sum of all negative elements in the array and store this value in negSum.
    7. Replace each element by its square.
    8. Find the largest value in the array and store this value in largest.
  5. A magic square is a square array of numbers in which the sums of the rows, the columns, and the diagonals are all equal. Write a program that reads sixteen integers into a 4 x 4 array and determines whether or not they form a magic square.