Thursday, 2 July 2015

Matrix problem- Write a C++ program to print only unique rows in a given boolean matrix

 #include<bits/stdc++.h>  
 using namespace std;  
 struct node  
 {  
   bool isend;  
   node *child[2];  
 };  
 void print_unique_matrix(int m[][4],int r,int c);  
 int myinsert(int m[][4],node **root,int row,int col,int col_size);  
 void print_row(int m[][4],int row_num,int col);  
 int main()  
 {  
   int matrix[10][4]=  
         {{1,0,1,0},  
          {1,0,1,1},  
          {1,1,0,0},  
          {1,1,1,0},  
          {1,0,1,0},  
          {1,0,1,1},  
          {1,1,0,0},  
          {1,1,1,0},  
          {1,0,1,0},  
          {1,0,1,1}  
          };  
   print_unique_matrix(matrix,10,4);  
 }  
 int myinsert(int m[][4],node **root,int row,int col,int col_size)  
 {  
   if(*root==NULL)  
   {  
     *root=new node;  
     (*root)->isend=0;  
     (*root)->child[0]=(*root)->child[1]=NULL;  
   }  
   if(col<col_size)  
   {  
     myinsert(m,&((*root)->child[m[row][col]]),row,col+1,col_size);  
   }  
   else  
   {  
     if((*root)->isend==0)  
     {  
       (*root)->isend=1;  
       return 1;  
     }  
     else  
        return 0;  
   }  
 }  
 void print_unique_matrix(int m[][4],int r,int c)  
 {  
   node *root=NULL;  
   for(int i=0;i<r;i++)  
   {  
     if(myinsert(m,&root,i,0,4))  
     {  
       print_row(m,i,c);  
     }  
   }  
 }  
 void print_row(int m[][4],int row_num,int col)  
 {  
   cout<<endl;  
   for(int i=0;i<col;i++)  
     cout<<m[row_num][i]<<"  ";  
   cout<<endl;  
 }  

No comments:

Post a Comment