logo CodeStepByStep logo

zeroRowCol

Write a method named zeroRowCol that accepts a 2-D array of integers as a parameter and modifies its contents by setting any element value to 0 if there is a 0 in that row and/or in that column. For example, if a variable called matrix stores the following values:

int[][] matrix = {{ 0,  1,  4,  0},
                  { 3,  2,  6,  4},
                  {-1,  3,  1,  8},
                  {15,  7,  2,  0},
                  { 9,  4,  5,  6}};

Then the call of zeroRowCol(matrix) should modify its state to be the following:

                 {{ 0,  0,  0,  0},
                  { 0,  2,  6,  0},
                  { 0,  3,  1,  0},
                  { 0,  0,  0,  0},
                  { 0,  4,  5,  0}}

Your code should work for an array of any size, even one with 0 rows or columns.

Method: Write a Java method as described, not a complete program or class.

You must log in before you can solve this problem.

Log In

Need help?

Stuck on an exercise? Contact your TA or instructor.

If something seems wrong with our site, please

Is there a problem? Contact us.