< previous page page_716 next page >

Page 716
    total = 0;
    for (col = 0; col < colLength; col++)

            // Invariant (prior to test):
            //     total == table[row][0] +  + table[row][col-1]
            //  && 0 <= col <= colLength

        total = total + table[row][col];
    cout << Row sum:  << total << endl;
}
Figure 13-5 illustrates subarray processing by row.
Sum the Columns
Suppose we want to sum and print each column. The code to perform this task is given below. Again we have generalized the code to sum only the portion of the array that contains valid data.
for (col = 0; col < colLength; col++)
{

        // Invariant (prior to test):
        //     Columns 0 through col-1 have been summed and printed
        //  && 0 <= col <= colLength
0716-01.gif
Figure 13-5
Partial Table Processing by Row

 
< previous page page_716 next page >