Opening the reading…
Opening the reading…
DSA FUNDAMENTALS › MATRIX BASICS
The answer for a row must come only from that row. For the matrix [[3, -1, 7, 2], [-5, -2, -9, -4], [6, 6, 1, 8]], row 0 has the pair (-1, 7), row 1 has (-9, -2), and row 2 has (1, 8). When the outer loop moves to a new row, both candidate values must start again.
The safest starting value is the first element of the current row. For row index i, set both minimum and maximum to matrix[i][0]. This gives row 0 a starting value of 3, row 1 a starting value of -5, and row 2 a starting value of 6. Each start belongs to its own row, so no result can carry into the next row.
for (int i = 0; i < rows; i++) {
int minimum = matrix[i][0];
int maximum = matrix[i][0];
// Scan the remaining values in row i.
}For the second row, [-5, -2, -9, -4], which initialization is correct?
Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.
Zero is not a safe default for either candidate. In the first row, [3, -1, 7, 2], starting minimum at 0 leaves the minimum as 0, even though 0 is not in the row. The value -1 is smaller than 0, but the correct minimum should be found by comparing values from a real position in the row, not by assuming a value that may be absent.
Starting maximum at 0 fails on the all-negative second row, [-5, -2, -9, -4]. None of its values is greater than 0, so maximum remains 0. That answer cannot be correct because 0 is not in the row. The largest value in this row is -2, which is still negative.
// Wrong for the running matrix
int minimum = 0;
int maximum = 0;
// Correct: start from a value that is actually in this row
int minimum = matrix[i][0];
int maximum = matrix[i][0];After both candidates start at the first value, scan the rest of the current row from column index 1. If the next value is smaller than minimum, replace minimum. Independently, if it is larger than maximum, replace maximum. A value can update one tracker, both trackers, or neither tracker, so the comparisons must not be treated as one choice between minimum and maximum.
For row 0, start with minimum = maximum = 3. The value -1 updates minimum to -1, 7 updates maximum to 7, and 2 changes neither candidate. The finished pair is (-1, 7). The first value was already used for initialization, so the scan begins at column 1 rather than comparing 3 with itself.
Row 1 starts fresh at -5. The value -2 is larger, so maximum becomes -2. The value -9 is smaller, so minimum becomes -9. The value -4 is between those candidates and changes neither. The finished pair is (-9, -2), and every update came from a value in row 1.
Row 2 also starts fresh, with both candidates equal to 6. The next 6 equals both candidates, so neither comparison is true and no special case is needed. Then 1 updates minimum, while 8 updates maximum. The finished pair is (1, 8). Equal values do not need to be handled separately because strict comparisons naturally leave the trackers unchanged.
After tracing the second row [-5, -2, -9, -4], enter its minimum and maximum in that order.
minimum = -5; maximum = -5;
// compare -2, then -9, then -4Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.
Initialization belongs before the inner scan, because the first value supplies the starting candidates. The comparisons belong inside the inner loop, because each remaining value must be examined. Output belongs after that inner loop, but still inside the outer loop, because the pair is complete only when the current row has been fully scanned.
for (int i = 0; i < rows; i++) {
int minimum = matrix[i][0];
int maximum = matrix[i][0];
for (int j = 1; j < columns; j++) {
if (matrix[i][j] < minimum) {
minimum = matrix[i][j];
}
if (matrix[i][j] > maximum) {
maximum = matrix[i][j];
}
}
cout << minimum << " " << maximum << endl;
}The inner loop examines columns 1 through columns - 1 after column 0 initializes the trackers. Across all rows, every matrix element is examined once overall, so the time complexity is O(rows * columns). Only the two candidate variables and loop indices are added, so printing each result directly uses O(1) extra space.