[prev] [index] [next]

2d Arrays (Matrices) (cont)

Iteration can be done row-by-row or column-by-column:

int row, col;   int m[NROWS][NCOLS];

//row-by-row
for (row = 0; row < NROWS; row++) {
    for (col = 0; col < NCOLS; col++) {
        ... m[row][col] ...
    }
}
// colum-by-column
for (col = 0; col < NCOLS; col++) {
    for (row = 0; row < NROWS; row++) {
        ... m[row][col] ...
    }
}

Row-by-row is the most common style of iteration.