< previous page page_711 next page >

Page 711
0711-01.gif
Figure 13-3
hiTemp Array (Alternate Form)
Here, hiTemp is declared the same as before, but we use an expression of type DayType for the column index. hiTemp[2][SUNDAY] corresponds to the same component as hiTemp[2][6] in the first example. (Recall that enumerators such as MONDAY, TUESDAY, are represented internally as the integers 0, 1, 2, .) If day were of type DayType and week of type int, the code fragment shown in Figure 13-3 would set the entire array to 0. (Notice that by using DayType, the temperature values in the array begin with the first Monday of the year, not necessarily with January 1.)
If the declaration of hiTemp listed the sizes of the two dimensions in reverse order, the rows and columns would be reversed; that is, the declaration
int hiTemp[7][52];
declares an array like that shown in Figure 13-4. Notice how the code fragment in this figure differs from the equivalent code in Figure 13-3: the two index expressions are reversed. The order in which we define the rows and columns doesn't matter to the computer as long as we're consistent. To help visualize a two-dimensional array, we always use the convention of letting the first dimension define the rows and the second dimension define the columns.
Another way of looking at a two-dimensional array is to see it as a structure in which each component has two features. For example, in the following code,

 
< previous page page_711 next page >