You are given a matrix Mat of m rows and n columns. The matrix is boolean so the elements of the matrix can only be either 0 or 1. Now, if any row of the matrix contains a 1, then you need to fill that whole row with 1. After doing the mentioned operation, you need to print the modified matrix.InputThe first line of contains m and n denoting number of rows and number of columns. Then next m lines contain n elements denoting the elements of the matrix.Constraints:1 <= m, n <= 700Mat[I][j] ∈ {0,1}OutputIn a new line, print the modified matrix.ExampleInput:5 41 0 0 00 0 0 00 1 0 00 0 0 00 0 0 1Output:1 1 1 10 0 0 01 1 1 10 0 0 01 1 1 1Explanation:rows = 5 and columns = 4The given matrix is1 0 0 00 0 0 00 1 0 00 0 0 00 0 0 1Evidently, the first row contains a 1 so fill the whole row with 1. The third row also contains a 1 so that row will be filled too. Finally, the last row contains a 1 and therefore it needs to be filled with 1 too. The final matrix is1 1 1 10 0 0 01 1 1 10 0 0 01 1 1 1 i need in python

Answers 1

Answer:

#include <iostream>

using namespace std;

#define MAX 50

int main() {

   int mat[MAX][MAX];

   int m,n,i,j;

   cout<<"\nEnter the number of rows and columns: ";

   cin>>m>>n;

   for(i=0;i<m;i++)

   {

       cout<<"\nEnter elements of row "<<i+1<<": ";

       for(j=0;j<n;j++)

       {

           cin>>mat[i][j];

       }

   }

   for(i=0;i<m;i++)

   {

       for(j=0;j<n;j++)

       {

           if(mat[i][j]==1)

           {

               for(j=0;j<n;j++)

               mat[i][j]=1;

           }

       }

   }

   cout<<"\nModified matrix is: "<<endl;

   for(i=0;i<m;i++)

   {

       for(j=0;j<n;j++)

       {

           cout<<mat[i][j];

       }

       cout<<endl;

   }

   return 0;

}

Explanation:

  1. Declare an array to store the matrix elements and variables to store the number of rows and columns.
  2. Take the number of rows and columns as input from the user.
  3. Take the matrix elements as input from the user and store them in the array using the for loop.
  4. For every row, check whether the column elements are zero or one.
  5. If it is zero, the next element is checked.
  6. If it is one, all the elements of that row are set to have value one.
  7. Steps 4 to 7 are repeated for each element of the array using the nested for and if loops.
  8. The modified matrix is displayed as the output.

#SPJ3

If you know the answer add it here!

Can't find the answer?

Log in with Google

or

Forgot your password?

I don't have an account, and I want to Register

Choose a language and a region
How much to ban the user?
1 hour 1 day 100 years