|
|
|
|
|
|
|
#include <iomanip.h> // For setw()
.
.
.
for (row = 0; row < NUM_ROWS; row++)
{
// Invariant (prior to test):
// Rows 0 through row-1 of table have been output
// && <= row <= NUM_ROWS
for (col = 0; col < NUM_COLS; col++)
// Invariant (prior to test):
// table[row][0..col-1] have been output
// && 0 <= col <= NUM_COLS
cout << setw(15) << table[row][col];
cout << endl;
} |
|
|
|
|
|
|
|
|
This code fragment prints the values of the table in columns that are 15 characters wide. As a matter of proper style, this fragment should be preceded by code that prints headings over the columns to identify their contents. |
|
|
|
|
|
|
|
|
There's no rule that we have to print each row on a line. We could turn the table sideways and print each column on one line simply by exchanging the two For loops. When you are printing a table, you must consider which order of presentation makes the most sense and how the table fits on the page. For example, a table with 6 columns and 100 rows would be best printed as 6 columns, 100 lines long. |
|
|
|
|
|
|
|
|
Almost all processing of data stored in a two-dimensional array involves either processing by row or processing by column. In most of our examples the index type has been int, but the pattern of operation of the loops is the same no matter what types the indices are. |
|
|
|
|
|
|
|
|
The looping patterns for row processing and column processing are so useful that they are summarized below. To make them more general, we use minRow for the first row number and minCol for the first column number. Remember that row processing has the row index in the outer loop, and column processing has the column index in the outer loop. |
|
|
|
|
|
|
|
|
for (row = minRow; row < rowLength; row++)
for (col = minCol; col < colLength; col++)
.
.
. // Whatever processing is required |
|
|
|
|
|