Jan 18, 2013 at 11:10am Jan 18, 2013 at 11:10am UTC
So i have to write this:
For a given matrix of n rows and m columns ,write an app that will show a new matrix that replaces with 0 the elements on the column which has the lowest sum value.
so far ive wrote:
#include<iostream.h>
int a[20][20],b[30],i,j,n,m,min;
void main()
{
cout<<"n=";cin>>n;
cout<<"m=";cin>>m;
for(i=0;i<=m-1;i++)
for(j=0;j<=n-1;j++)
{
cout<<"a["<<i<<"]["<<j<<"]=";
cin>>a[i][j];
}
for(i=0;i<=m-1;i++)
b[i]=0;
for(i=0;i<=m-1;i++)
for(j=0;j<=n-1;j++)
b[i]=b[i]+a[i][j];
min=b[0];
for(i=0;i<=m-1;i++)
if(b[i]<min)
min=b[i];
}
i'm stuck at figuring out how to determine how to replace the lowest collum with 0.
Last edited on Jan 18, 2013 at 12:09pm Jan 18, 2013 at 12:09pm UTC
Jan 18, 2013 at 12:23pm Jan 18, 2013 at 12:23pm UTC
Your code determines the lowest sum of the columns. You need to teak your code a little bit to find out the column which has the least sum. You could do like this.
#include<iostream.h>
int a[20][20],b[30],i,j,n,m,min, minSumColumn = 0;
void main()
{
cout<<"n=";cin>>n;
cout<<"m=";cin>>m;
for(i=0;i<=m-1;i++)
for(j=0;j<=n-1;j++)
{
cout<<"a["<<i<<"]["<<j<<"]=";
cin>>a[i][j];
}
for(i=0;i<=m-1;i++)
b[i]=0;
for(i=0;i<=m-1;i++)
for(j=0;j<n-1;j++)
b[i]=b[i]+a[i][j];
min=b[0];
for(i=0;i<=m-1;i++)
if(b[i]<min)
{
min=b[i];
minSumColumn = i;
}
//Now as we know the column number whose sum is minimum, we can run through all the rows for the column and replace their element values by 0
for(i = 0; i< n; i++)
{
a[i][minSumColumn] = 0;
}
}