Opening the reading…
Opening the reading…
DSA FUNDAMENTALS › MATRIX BASICS
The matrix has four columns, but finding their extrema means performing four independent searches. A minimum and maximum from column 0 must not survive into column 1, because values in one column do not belong to another. The initialization therefore belongs inside the outer loop that selects a column, before the inner loop scans that column's rows.
// Wrong: one pair is shared by every column
int minValue = matrix[0][0];
int maxValue = matrix[0][0];
for (int column = 0; column < 4; column++) {
for (int row = 0; row < 3; row++) {
minValue = min(minValue, matrix[row][column]);
maxValue = max(maxValue, matrix[row][column]);
}
}
// Right: a new pair starts each column's search
for (int column = 0; column < 4; column++) {
int minValue = matrix[0][column];
int maxValue = matrix[0][column];
for (int row = 1; row < 3; row++) {
minValue = min(minValue, matrix[row][column]);
maxValue = max(maxValue, matrix[row][column]);
}
}Where should minValue and maxValue be initialized when each column is searched independently?
Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.
To scan one column from top to bottom, keep column fixed and change row. For column 0, matrix[row][column] visits matrix[0][0], matrix[1][0], and matrix[2][0], which are 4, 6, and 0. Only after those three cells have been processed does the outer loop change column to 1. The same orientation then visits all three rows of column 1, then column 2, then column 3.
for (int column = 0; column < 4; column++) {
int minValue = matrix[0][column];
int maxValue = matrix[0][column];
for (int row = 1; row < 3; row++) {
int value = matrix[row][column];
minValue = min(minValue, value);
maxValue = max(maxValue, value);
}
cout << minValue << " " << maxValue << endl;
}The first cell in a column already is a real value, so use matrix[0][column] as both the initial minimum and the initial maximum. The remaining rows only need to be compared against that seed. For the matrix, column 2 starts with 7, then compares -5 and 2. Starting from an arbitrary constant such as 0 is not valid: column 1 has values -2, 3, and 9, while column 3 has 1, 8, and -4. A constant can pretend to be part of the column and produce an answer that no cell supports.
int minValue = matrix[0][column];
int maxValue = matrix[0][column];
for (int row = 1; row < 3; row++) {
if (matrix[row][column] < minValue) {
minValue = matrix[row][column];
}
if (matrix[row][column] > maxValue) {
maxValue = matrix[row][column];
}
}For one selected column, type the initialization and inner-loop header that seed both extrema correctly and start after row 0.
int minValue = ???;
int maxValue = ???;
for (int row = ???; row < 3; row++) {Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.
Column 0 begins with 4, changes its maximum to 6, then changes its minimum to 0. Column 1 begins with -2, keeps -2 as its minimum, and changes its maximum from -2 to 3 and then 9. Column 2 begins with 7, changes its minimum to -5, and keeps 7 as its maximum when it sees 2. Column 3 begins with 1, changes its maximum to 8, then changes its minimum to -4.
| COLUMN | VALUES VISITED | MINIMUM | MAXIMUM |
|---|---|---|---|
| 0 | 4, 6, 0 | 0 | 6 |
| 1 | -2, 3, 9 | -2 | 9 |
| 2 | 7, -5, 2 | -5 | 7 |
| 3 | 1, 8, -4 | -4 | 8 |
Store each finished minimum and maximum at the current column position. The resulting minima are [0, -2, -5, -4], and the resulting maxima are [6, 9, 7, 8]. Position 2 in either result describes column 2 of the matrix, and position 3 describes column 3. No later matrix operation is needed: the answer is already ordered by the outer column scan.