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:
- Declare an array to store the matrix elements and variables to store the number of rows and columns.
- Take the number of rows and columns as input from the user.
- Take the matrix elements as input from the user and store them in the array using the for loop.
- For every row, check whether the column elements are zero or one.
- If it is zero, the next element is checked.
- If it is one, all the elements of that row are set to have value one.
- Steps 4 to 7 are repeated for each element of the array using the nested for and if loops.
- The modified matrix is displayed as the output.
#SPJ3