Opening the reading…
Opening the reading…
DSA FUNDAMENTALS › MATRIX BASICS
In the 4 x 5 matrix, row indices run from 0 to 3 and column indices run from 0 to 4. A cell belongs to the boundary if it is in the first row, last row, first column, or last column. For position (i, j), the condition is i == 0 || i == rows - 1 || j == 0 || j == cols - 1. The logical OR matters because meeting any one edge condition is enough.
int rows = 4;
int cols = 5;
bool isBoundary = (i == 0 || i == rows - 1 ||
j == 0 || j == cols - 1);Value 1 is at position (0, 0), so i == 0 and j == 0 are both true. Value 7 is at (1, 1), so none of the four comparisons is true, making it an interior value. Value 15 is at (2, 4), so j == cols - 1 is true because 4 == 4, making it a boundary value even though it is not in the first or last row.
Which combined condition decides whether position (i, j) is on the boundary of the 4 x 5 matrix?
Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.
You can visit all 20 positions with nested loops and let the boundary condition decide which values reach the print statement. The scan moves across each row from left to right, then continues with the next row. It prints the complete first row, the two edge values of each middle row, and the complete last row.
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
if (i == 0 || i == rows - 1 ||
j == 0 || j == cols - 1) {
cout << matrix[i][j] << " ";
}
}
}For the running matrix, the first row contributes 1 2 3 4 5. The second row contributes 6 and 10, the third row contributes 11 and 15, and the last row contributes 16 17 18 19 20. The resulting row-wise order is 1 2 3 4 5 6 10 11 15 16 17 18 19 20. Each position reaches the print statement at most once because the nested loops visit each position once.
The row variable i must be compared with rows - 1, while the column variable j must be compared with cols - 1. In this matrix, rows - 1 is 3, the last valid row index, and cols - 1 is 4, the last valid column index. These values differ because the matrix has four rows but five columns.
i == 0 || i == rows - 1
j == 0 || j == cols - 1
// For this matrix:
i == 0 || i == 3
j == 0 || j == 4If you compare i with cols - 1, you treat row index 4 as an edge even though row 4 does not exist. If you compare j with rows - 1, you treat column index 3 as the last column and fail to recognize column 4 as the right edge. Swapping the bounds therefore misclassifies cells, and a later access using an invalid row index can go outside the matrix.
Repair this condition so i uses the last row index and j uses the last column index in the 4 x 5 matrix.
i == 0 || i == cols - 1 || j == 0 || j == rows - 1Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.
A corner belongs to two edges. For example, value 1 at (0, 0) satisfies both the first-row test and the first-column test. If you print the top row and then print the left column as separate edge passes, value 1 is encountered twice. The combined condition uses one if statement inside one scan, so the position is visited once and its value is printed once.
The same scan also remains valid when the dimensions shrink. A one-row matrix makes every existing position satisfy the first-row and last-row tests, but each position is still visited once. A one-column matrix makes every position satisfy the first-column and last-column tests, but the single if statement still prints each existing value once.