// Input a matrix of size 3x3. For each column and row, find the sum of the // elements in it. Output the matrix such that both the columns and rows are // re-arranged in the increasing order of their sums. If two columns or two rows // have the same sum, output them in the order they appeared in the original // matrix. // // author:rahule@cse.iitk.ac.in #include main() { int a[3][3],temp[3],i,j; printf("Enter the 3x3 matrix :\n"); //enter the 3x3 matrix for(i=0;i<3;i++) for(j=0;j<3;j++) scanf("%d",&a[i][j]); //sorting the rows in the increasing order of their sum for(i=0;i<2;i++) { if(a[i][0]+a[i][1]+a[i][2] > a[i+1][0]+a[i+1][1]+a[i+1][2]) //check wether the sum of ith row is greater than the (i+1)th row { temp[0]=a[i][0]; //save the ith row in a temporary array,temp temp[1]=a[i][1]; temp[2]=a[i][2]; a[i][0]=a[i+1][0]; //copy (i+1)th row to ith row a[i][1]=a[i+1][1]; a[i][2]=a[i+1][2]; a[i+1][0]=temp[0]; //copy the ith row (which was saved in "temp" ) back to (1+1)th row a[i+1][1]=temp[1]; a[i+1][2]=temp[2]; } } //sorting the columns in the increasing order of their sum for(j=0;j<2;j++) { if(a[0][j]+a[1][j]+a[2][j] > a[0][j+1]+a[1][j+1]+a[2][j+1]) //check wether the sum of jth column is greater than the (j+1)th column { temp[0]=a[0][j]; //save the jth column in a temporary array,temp temp[1]=a[1][j]; temp[2]=a[2][j]; a[0][j]=a[0][j+1]; //copy (j+1)th column to jth column a[1][j]=a[1][j+1]; a[2][j]=a[2][j+1]; a[0][j+1]=temp[0]; //copy the jth column (which was saved in "temp" ) back to (j+1)th column a[1][j+1]=temp[1]; a[2][j+1]=temp[2]; } } for(i=0;i<3;i++) //print the resulting array { printf("\n"); for(j=0;j<3;j++) printf("%d ",a[i][j]); } }