|
|
|
|
|
|
|
const int NUM_ROWS = 10;
const int NUM_COLS = 20;
typedef int TableType[NUM_ROWS][NUM_COLS]; |
|
|
|
|
|
|
|
|
and then write the following general-purpose function that initializes all elements of an array to a specified value: |
|
|
|
|
|
|
|
|
void Initialize( /* out */ TableType table, // Array to initialize
/* in */ int initVal) // Initial value
// Initializes each element of table to initVal
// Precondition:
// initVal is assigned
// Postcondition:
// table[0..NUM_ROWS-1][0..NUM_COLS-1] == initVal
{
int row;
int col;
for (row = 0; row < NUM_ROWS; row++)
// Invariant (prior to test):
// table[0..row-1][0..NUM_COLS-1] == initVal
// && 0 <= row <= NUM_ROWS
for (col = 0; col < NUM_COLS; col++)
// Invariant (prior to test):
// table[row][0..col-1] == initVal
// && 0 <= col <= NUM_COLS
table[row][col] = initVal;
} |
|
|
|
|
|
|
|
|
The calling code could then declare and initialize one or more arrays of type TableType by making calls to the Initialize function. For example, |
|
|
|
|
|
|
|
|
TableType delta;
TableType gamma;
Initialize(delta, 0);
Initialize(gamma, -1);
.
.
. |
|
|
|
|
|