- Page 548 through the end of the chapter.
- We have been dealing with one dimensional arrays.
- But we can add higher dimensions.
- Think chess board.
- Or even a rubix cube
- Or a change in space (3d) over time (4d).
- Careful, these can grow very large very fast.
- type identifier [const int expression][const int expression] ...
- In two dimensions
- The first dimension is usually considered the rows
- The second is considered the columns.
-
const size_t ROWS=5;
const size_t COLS = 3;
...
char board[ROWS][COLS];
| 0 | 1 | 2 |
---|
0 | board[0][0] | board[0][1] | board[0][2] |
---|
1 | board[1][0] | board[1][1] | board[1][2] |
---|
2 | board[2][0] | board[2][1] | board[2][2] |
---|
3 | board[3][0] | board[3][1] | board[3][2] |
---|
4 | board[4][0] | board[4][1] | board[4][2] |
---|
- We could initialize the entire thing to be 0.
-
size_t i,j;
for(i=0; i<ROWS; i++) {
for(j=0; j<COLS; j++) {
board[i][j] = 0;
}
}
- Passing multi dimensional arrays
- The typedef command.
- This command allows the program to nickname an existing data type.
- typedef known_type identifier;
- For example, this MIGHT in a header file somewhere
- typedef unsigned long size_t;
- This just means that anywhere the compiler sees size_t, it replaces it with unsigned long.
- So this is common
const int ROWS=5;
const int COLS=3;
typedef int BoardArrayT[ROWS][COLS];
void PrintBoard(BoardArrayT board);
- Sometimes it is easier to think a little different.
- I have a class of up to 40 names.
-
const size_t MAX_STUDENTS = 40;
typedef string ClassT[MAX_STUDENTS];
- Then I can declare a set of classes
-
const size_t MAX_CLASES;
ClassT classes[MAX_CLASSES];
size_t classSizes[MAX_CLASSES];