Saturday, October 5, 2013

Removal of ‘Zero’ Rows and Columns in a Matrix

Given an integer matrix of size, say, M x N, you have to write a program to remove all the rows and columns consisting of all zeros. Your program must remove those rows and columns that consist of zero valued elements only. That is, if all the elements in a row (column) are zeros, then remove that row (column). All the remaining rows and columns should be output.

Input Specification:

  • The first line of input will have two integers, say, M and N. M specifies the number of rows in the matrix and N specifies the number of columns in the matrix.
  • This will be followed by M lines, each consisting of N integers.
Note: Assume 1 ≤ M ≤ 10 and 1 ≤ N ≤ 10. Also, you may assume that there is at least one nonzero element in the given matrix.


Output Specification:

The output should be the resulting matrix. Each row of the output matrix should be terminated by a newline character. There should be exactly one space (not tab) between successive elements in each row.

Sample Input and Output:

Input:
44
1234
0000
5678
0003
Output:
1234
5678
0003
Input:
45
12035
00000
45069
78091
Output:
1235
4569
7891

Solution:

#include <stdio.h>
#include <stdlib.h>

void main()
{
    int mat[10][10],mat2[10][10];
    int sum[10],flag_r,flag_c;
    int i,i2,j,j2,k;
    int r,c;
    
    scanf("%d %d",&r,&c);

    //Reading matrix
    for(i=0;i<r;i++)
    {
        for(j=0;j<c;j++)
        {
            scanf("%d",&mat[i][j]);
        }
    } 

    //For rows
    for(i=0;i<10;i++)    //Clearing sum
    sum[i]=0;
   
    for(i=0;i<r;i++)    //Adding rows
    {
        for(j=0;j<c;j++)
        {
            if(mat[i][j]!=0)
            sum[i]=2;
        }
    }
    
    flag_r=0;
    for(i=0,i2=0;i<r;i++)    //Removing zeros from rows
    {         
        if(sum[i]==0)
        {flag_r++;continue;}
        
        for(j=0;j<c;j++)
        {
            mat2[i2][j]=mat[i][j];
        }
        i2++;
    }
    r=r-flag_r;    //Decrementing row size.

    //Copying mat2 to mat 
    for(i=0;i<r;i++)
    {
        for(j=0;j<c;j++)
        {
            mat[i][j]=mat2[i][j];
        }
    }

    //For columns
    for(i=0;i<10;i++)    //Clearing sum
    sum[i]=0;
    
    for(j=0;j<c;j++)    //Adding columns
    {
        for(i=0;i<r;i++)
        {if(mat[i][j]!=0)sum[j]=2;}
    }
    
    flag_c=0;
    for(j=0,j2=0;j<c;j++)    //Removing zeros from cols
    {                
        if(sum[j]==0)
        {flag_c++;continue;}
        
        for(i=0;i<r;i++)
        {
            mat2[i][j2]=mat[i][j];
        }
        j2++;
    }
    c=c-flag_c;    //Decrementing col size.

    //Copying mat2 to mat 
    for(i=0;i<r;i++)
    {
        for(j=0;j<c;j++)
        {
            mat[i][j]=mat2[i][j];
        }
    }

    //Output matrix
    for(i=0;i<r;i++)
    {
        for(j=0;j<c;j++)
        {
            if(j==0)            
            printf("%d",mat[i][j]);
            else
            printf(" %d",mat[i][j]);
        }
        printf("\n");
    }
}

No comments:

Post a Comment