Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to check if the matrix cell is zero.
DSA C++
if (matrix[i][j] [1] 0) { // mark row and column }
Attempts:
3 left
💡 Hint
Common Mistakes
Using '!=' instead of '==' causes wrong cells to be marked.
Using '>' or '<' will not correctly identify zero cells.
✗ Incorrect
We check if the current cell equals zero to mark its row and column.
2fill in blank
mediumComplete the code to mark the first cell of the row when a zero is found.
DSA C++
matrix[i][0] [1] 0;
Attempts:
3 left
💡 Hint
Common Mistakes
Using '+=' or '*=' changes the value incorrectly.
Using '-=' may cause unexpected negative values.
✗ Incorrect
We assign zero to the first cell of the row to mark it.
3fill in blank
hardFix the error in the loop condition to iterate over columns correctly.
DSA C++
for (int j = 1; j [1] matrix[0].size(); j++) { if (matrix[0][j] == 0) { // set entire column to zero } }
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<=' causes runtime errors due to out-of-bounds access.
Using '>' or '>=' prevents the loop from running correctly.
✗ Incorrect
The loop should run while j is less than the number of columns to avoid out-of-bounds.
4fill in blank
hardFill both blanks to set the entire row and column to zero based on markers.
DSA C++
for (int i = 1; i [1] matrix.size(); i++) { for (int j = 1; j [2] matrix[0].size(); j++) { if (matrix[i][0] == 0 || matrix[0][j] == 0) { matrix[i][j] = 0; } } }
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<=' causes out-of-bounds access.
Using '>' or '>=' prevents loops from running properly.
✗ Incorrect
Both loops should run while indices are less than the size to avoid out-of-bounds errors.
5fill in blank
hardFill all three blanks to update the first row and column if they originally contained zeros.
DSA C++
if (firstRowZero) { for (int j = 0; j [1] matrix[0].size(); j++) { matrix[0][j] = 0; } } if (firstColZero) { for (int i = 0; i [2] matrix.size(); i++) { matrix[i][0] = 0; } } // Reset flags firstRowZero = [3];
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<=' causes index errors.
Resetting flag to true causes infinite loops or repeated resets.
✗ Incorrect
Loops use '<' to avoid out-of-bounds. The flag is reset to false after use.
