# Question & Answer: Writing a program that if an element in an m*n matrix is 0, it's entire row and column is set to 0……

Writing a program that if an element in an m*n matrix is 0, it’s entire row and column is set to 0.

The user must enter the number of rows, columns and contents of the array.
The code attached doesn’t work but the code in the comments works which has a manually entered matrix. I need a user input matrix

Vodacom令 1:43 PM Done 1 of 2 #include #include #include

## Expert Answer

#include<iostream>
#include<memory.h>
#include<cstdlib>
using namespace std;

void SetZeros(int** matrix,int m,int n)
{
int row[m];
int column[n];
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
if(*((int*)matrix+n*i+j)==0)
{
row[i]=1;
column[j]=1;
}
}
}
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
if(row[i]==1 || column[j]==1)
{
*((int*)matrix+n*i+j)=0;
}
}
}
}

void fillArray(int** matrix,int m,int n)
{
cout<<“Enter Data in Array “<<endl;
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
cout<<“Enter element [“<<i<<“][“<<j<<“]: “<<endl;
cin>>*((int*)matrix+n*i+j);
}
}
}

void printArray(int** matrix,int m,int n)
{
for(int i=0;i<m;i++)
{
for(int j=0;j<n;j++)
{
cout<<*((int*)matrix+n*i+j)<<” “;
}
cout<<endl;
}
}

int main()
{
int m;
int n;
cout<<“please enter number of rows: “<<endl;
cin>>m;
cout<<“Please enter the number of columns: “<<endl;
cin>>n;
int matrix[m][n];
fillArray((int**)matrix,m,n);
cout<<“nEntered Matrix is: “<<endl;
printArray((int**)matrix,m,n);

SetZeros((int**)matrix,m,n);
cout<<“The answer is: “<<endl;
printArray((int**)matrix,m,n);
return 0;
}