logo CodeStepByStep logo

zeroRowCol

Language/Type: C++ Grid traversals
Related Links:

Write a function named zeroRowCol that accepts a reference to a Grid 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:

Grid<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 a grid of any size, even one with 0 rows or columns.

Function: Write a C++ function as described, not a complete program.

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.