📖 Scenario: You are working with a grid of numbers representing a game board. Sometimes, certain cells become inactive and are marked with zero. To keep the board consistent, if any cell is zero, the entire row and column of that cell should become zero.
🎯 Goal: Modify the given matrix in place so that if any cell is zero, its entire row and column are set to zero. You will do this without using extra space for another matrix.
📋 What You'll Learn
Create a 2D integer array called matrix with the exact values given.
Create two integer variables rows and cols to store the matrix dimensions.
Implement the in-place algorithm to set matrix zeroes using the first row and first column as markers.
Print the matrix after modification in the specified format.
💡 Why This Matters
🌍 Real World
This technique is useful in image processing, game boards, and spreadsheet applications where certain cells becoming inactive affect entire rows and columns.
💼 Career
Understanding in-place matrix modification is important for software engineers working with memory-efficient algorithms and data manipulation.
Progress0 / 4 steps
1
Create the matrix
Create a 2D integer array called matrix with 3 rows and 4 columns containing these exact values: {{1, 1, 1, 0}, {1, 0, 1, 1}, {1, 1, 1, 1}}.
DSA C
Hint
Use a 2D array with 3 rows and 4 columns and fill it with the exact numbers given.
2
Set matrix dimensions
Create two integer variables called rows and cols and set them to 3 and 4 respectively to represent the matrix size.
DSA C
Hint
Use two integer variables to store the number of rows and columns.
3
Implement in-place zero setting
Write code to set matrix zeroes in place using the first row and first column as markers. Use variables rows and cols for dimensions and modify matrix accordingly.
DSA C
Hint
Use the first row and first column as markers to remember which rows and columns should be zeroed. Then update the matrix accordingly.
4
Print the modified matrix
Print the matrix after modification. Use nested loops with variables i and j to print each element separated by spaces, and print a newline after each row.
DSA C
Hint
Use nested loops to print each element separated by spaces. Print a newline after each row.
Practice
(1/5)
1. What is the main goal of the Set Matrix Zeroes In Place problem?
easy
A. Count the number of zero elements in the matrix.
B. Find the sum of all elements in the matrix.
C. Modify the matrix so that if an element is 0, its entire row and column become 0 without using extra space.
D. Sort each row of the matrix in ascending order.
Solution
Step 1: Understand the problem requirement
The problem asks to change the matrix so that any row or column containing a zero should be set entirely to zero.
Step 2: Note the constraint of in-place modification
The matrix must be modified without using extra space for another matrix or large data structures.
Final Answer:
Modify the matrix so that if an element is 0, its entire row and column become 0 without using extra space. -> Option C
Quick Check:
Set matrix zeroes in place = Modify matrix rows and columns with zeros [OK]
Hint: Remember: zero means zero entire row and column [OK]
Common Mistakes:
Using extra space like another matrix
Only zeroing rows or only columns
Not modifying the matrix in place
2. Which of the following C code snippets correctly declares a 3x3 matrix to be used for the Set Matrix Zeroes problem?
easy
A. int matrix[3][3] = {{1,2,3},{4,5,6},{7,8,9}};
B. int matrix[3] = {1,2,3,4,5,6,7,8,9};
C. int matrix[][] = {{1,2,3},{4,5,6},{7,8,9}};
D. int matrix[3][3] = {1,2,3,4,5,6,7,8};
Solution
Step 1: Check correct 2D array declaration syntax in C
In C, a 2D array must have fixed sizes for both dimensions, and initialization uses nested braces.
Step 2: Validate each option
int matrix[3][3] = {{1,2,3},{4,5,6},{7,8,9}}; correctly declares and initializes a 3x3 matrix. int matrix[3] = {1,2,3,4,5,6,7,8,9}; is a 1D array with too many elements. int matrix[][] = {{1,2,3},{4,5,6},{7,8,9}}; misses size for second dimension. int matrix[3][3] = {1,2,3,4,5,6,7,8}; has too few initializers (matrix[2][2] uninitialized).
Final Answer:
int matrix[3][3] = {{1,2,3},{4,5,6},{7,8,9}}; -> Option A
Quick Check:
Correct 2D array syntax = int matrix[3][3] = {{1,2,3},{4,5,6},{7,8,9}}; [OK]
Hint: Use nested braces for 2D array initialization [OK]
Common Mistakes:
Missing size for second dimension
Using 1D array for 2D data
Incorrect initializer braces
3. What will be the matrix after running the in-place zeroing algorithm on this 3x3 matrix?
Step 2: Determine first row and first column zero flags
First row: no zeros (firstRowZero = false). First column: zero at (3,0) (firstColZero = true).
Step 3: Mark zeros using first row and column (inner matrix)
Scan i=1 to 3, j=1 to 3: (1,1)=0 sets matrix[1][0]=0, matrix[0][1]=0.
Step 4: Zero inner matrix using marks
For i=1 to 3, j=1 to 3: if matrix[i][0]==0 or matrix[0][j]==0, set matrix[i][j]=0. Zeros row 1 (mark at [1][0]), col 1 (mark at [0][1]), row 3 (orig [3][0]=0).
Step 5: Apply flags to borders
First row unchanged (false). First col all zeroed (true): matrix[0][0]=0, matrix[2][0]=0 (among others).