Multi-Dimensional Arrays


Multi-dimensional Arrays:

The individual elements of an array are "indexed" (individually referenced) using a subscript. In C++, we can declare and initialize an array named N to hold four integer elements (subscripted 0 through 3) with a statement like:

int  N[4] = {10,  15, 20, 25};

The array could be visualized by the programmer as a linear group of storage locations, as in the illustration below, with the first (lowest numbered) element at the bottom and the last (highest numbered) element at the top. In the example above, such programmers would refer to the element containing the 25 value as the "top of the array" and the element containing the 10 value as the "bottom of the array". It does not matter if we visualize the array as a vertical group of storage locations (like the illustration below) or as a horizontal group of storage locations. The important thing is that we are consistent in our use of the subscripts when referring to the values.

 
Top Element: 3
2
1
Bottom Element: 0
N
25
20
15
10

An array like the one above is referred to as a "one-dimensional array" because it has only one dimension: height (or if it was horizontal, length). As such, its elements are indexed using a single subscript value. For example, the element that contains the value 15 would be referred to as N[1], with 1 being the subscript. But arrays can be indexed in other ("multi-dimensional") ways. Consider a situation where we need to store 16 integers in an array named M. We could declare a one-dimensional array, as in:

int  M[16];

Notice that the statement above declares array M, but does not initialize any of its elements. The result is that these elements are unknown. Do not assume that they are automatically set to 0.

The statement above declares M as a "one-dimensional array" because it has only one dimension: a height of 16. We can use one subscript with any value from 0 to 15 to index M's elements, as in writing M[0] to refer to its first element. However, if we wanted to, we could have declared M as a multi-dimensional array, which would give us more control over how the elements are indexed. For example, instead of handling M as one group of 16 elements, we could instead handle the array as two groups of eight elements and use two subscripts to index the array, one to index the group and the other to index an element with that group. Such an array would be declared like this:

int  M [2] [8];

The statement above declares M as a "two-dimensional array", with 2 groups of 8 elements. A programmer could choose to visualize such an array as a table with 2 rows and 8 columns; or alternatively as a table with 2 columns and 8 rows. It does not matter how the array is visualized, as long as it is visualized the same way every time. If the programmer visualized the array as 2 rows of 8 columns (as shown in the table below), then the first (pink) element on the bottom row would be referred to as M[0][0], and last (blue) element on the top row would be referred to as M[1][7]. Again, it does not matter whether we visualize the bottom row as row 0 or visualize the top row as row 0, as long as we are consistent and always handle our subscripts the same way in any program.

 
Group 1
Group 0
Element:
M
         
         
01 23 45 67

The ability to use more than one subscript to index an array provides many possibilities. If we wanted to, we could have declared M as a two-dimensional array with 4 groups of 4 elements. Such an array would be declared like this:

int  M [4] [4];

The statement above declares M as a "two-dimensional array" that could be visualized as:

 
Group 3
Group 2
Group 1
Group 0
Element:
M
    
    
    
    
01 23

In this array, the first (pink) element on the bottom row would be referred to as M[0][0], and last (blue) element on the top row would be referred to as M[3][3]. Again, it does not matter whether we visualize the bottom row as row 0 or visualize the top row as row 0, as long as we are consistent and always handle our subscripts the same way in any program.

This two-dimensional data model can be extended to as many dimensions as necessary to effectively index groups of data. If you wanted to model 3 groups of 4 sub-groups of 5 elements, you could declare a "three-dimensional array" named T, as in:

int  T [3] [4] [5];

This array could be visualized as 3 sheets containing tables with 4 rows and 5 columns each. It is not essential to visualize arrays, but it is useful. The higher the number of dimensions, the more difficult it is to visualize arrays, but it can be done if you are creative in your thinking. The important thing is that you keep the use of the subscripts consistent. If in one place in a program, (1) the first subscript is being used to represent a group and (2) the second subscript is used to represent a sub-group and (3) the third subscript is used to represent an element, then the subscripts must be used in that same relative order in all references to that array within that program.

For information about how to manipulate multi-dimentional arrays, see your textbook.

PATH: Instructional Server> COP 2000> Examples>