Programming and Problem Solving with Java

(やまだぃちぅ) #1

(^588) | Multidimensional Arrays and Numeric Computation
data


[ 0 ]

[ 0 ]

[ 1 ]

[ 2 ]

[rowsFilled– 1 ]

[data.length– 1 ]

[ 1 ][ 2 ]

[colsFilled– 1 ] [data[i].length– 6 ]

Figure 12.4 Subarray Processing by Row.

// Array is not ragged
total = 0;
for (int col = 0; col < colsFilled; col++)
total = total + data[row][col];
outFile.println("Row sum: "+ total);
}

This is an example of subarray processing by row. Figure 12.4 illustrates subarray process-
ing by row.

Sum the Columns


Suppose we want to sum and print each column.The code to perform this task follows. Again,
we have generalized the code to sum only the portion of the array that contains valid data.

for (int col = 0; col < colsFilled; col++)
{
// Array is not ragged
total = 0;
for (int row = 0; row < rowsFilled; row++)
total = total + data[row][col];
outFile.println("Column sum: "+ total);
}
Free download pdf