Program to set zeros to rows and columns


Write a program for a two-dimensional array consisting only of zeros and ones. If you encounter a zero in this matrix, you must set all elements in that zero's row and column to zero.


Supposing you take a 3x3 matrix, for example :

1 1 0  
1 1 1  
1 1 1 



In the above-given matrix, you can see a zero in row 1 and column 3. Now set all elements in row 1 and column 3 to zero.

Now convert it into the following matrix:

0 0 0  
1 1 0  
1 1 0  



So, what should be our approach?

  1. First, take the input from the user, which consists of merely zeros and ones.
  2. Here I am using a vector; you can also use arrays.
  3. Now iterate through the 2D matrix to find where the zeros are positioned.
  4. After correctly recognizing a zero, remember its row number and column number.
  5. Now first set all the elements in the identified row to zero.
  6. After that, set the elements in the identified column to zero.


First try to write code by yourself by looking at this approach if there is any issue then only look at the solution.


Source code :


#include<iostream>
#include<vector>

using namespace std;

void exchangeRowAndColumn(vector<vector <int>> &v,int rows,int cols){
    
    vector<int> rowVisit(rows,1);
    vector<int> colVisit(cols,1);
    
    for(int i=0;i<rows;i++){
        
        for(int j=0;j<cols;j++){
            if(v[i][j]==0){
                rowVisit[i] = 0;
                colVisit[j] = 0;
            }
        }
    }
    
    for(int i=0;i<rows;i++){
        for(int j=0;j<cols;j++){
            if(rowVisit[i]==0){
                v[i][j] = 0;
            }
        }
    }
    
     for(int i=0;i<rows;i++){
        for(int j=0;j<cols;j++){
            if(colVisit[j]==0){
                v[i][j] = 0;
            }
        }
    }
}

int main(){
    
    int rows;
    cout<<"enter number of rows : ";
    cin>>rows; 
    int columns;
    cout<<"enter number of columns : ";
    cin>>columns;
    vector<vector<int>> v(rows,vector<int>(columns));
    int num;
    for(int i=0;i<rows;i++){
        for(int j=0;j<columns;j++){
            cin>>v[i][j];
            
        }
    }
    
    exchangeRowAndColumn(v,rows,columns);
    
    for(int i=0;i<rows;i++){
        for(int j=0;j<columns;j++){
            cout<<v[i][j]<<" ";
        }
        cout<<endl;
    }
    return 0;
}


Input code :

enter number of rows : 3
enter number of columns : 3
1 1 0 
1 1 1 
1 1 1


Output :

0 0 0 
1 1 0 
1 1 0 


Now let me explain what is a purpose of following line in code:


vector<vector<int>> v(rows, vector<int>(columns));.

Goal:

We are creating a 2D vector (matrix), which has rows number of rows and columns number of columns.

In this line of code:

vector<vector<int>> v(rows, vector<int>(columns));


Here’s what is happening :

1. vector<int>(columns):
    This creates a 1D vector of integers (vector<int>), with the size of columns.

    Numbers in this vector are automatically set to 0. 
So suppose if we take columns = 3 as an input, this creates a vector like this:

vector<int> row = {0, 0, 0}; // A row with 3 columns

This is for just one row with column elements, all initialized to 0.


2. vector<vector<int>> :

    This creates a 2D matrix that we want. With each row and column we have provided above earlier.


3. Putting all this together:

    When we combine these two parts:


vector<vector<int>> v(rows, vector<int>(columns));

Rows is the number of rows you want in the matrix.
vector<int>(columns) creates a single row (1D vector) with column elements, all initialized to 0.

v(rows, ...) creates a 2D vector v with rows rows, where each row is initialized to vector<int>(columns) (a vector of size columns).

Post a Comment

If you have any doubt or suggestions.please let me know.

Previous Post Next Post

Ads by google

Ads by google