Logo 
Search:

C++ Programming Articles

Submit Article
Home » Articles » C++ Programming » Mathematics ProgramRSS Feeds

Program that reads in a matrix and checks if it has the parity property

Posted By: Easy Tutor     Category: C++ Programming     Views: 7171

A boolean matrix has the parity property when each row and each column has an even sum, i.e. contains an even number of bits which are set. Here's a 4 x 4 matrix which has the parity property:

1 0 1 0
0 0 0 0
1 1 1 1
0 1 0 1

The sums of the rows are 2, 0, 4 and 2. The sums of the columns are 2, 2, 2 and 2.
Your job is to write a program that reads in a matrix and checks if it has the parity property. If not, your program should check if the parity property can be established by changing only one bit. If this is not possible either, the matrix should be classified as corrupt.

The input file will contain one or more test cases. The first line of each test case contains one integer n (n<100), representing the size of the matrix. On the next n lines, there will be n integers per line. No other integers than 0 and 1 will occur in the matrix. Input will be terminated by a value of 0 for n.

For each matrix in the input file, print one line. If the matrix already has the parity property, print ``OK". If the parity property can be established by changing one bit, print ``Change bit (i,j)" where i is the row and j the column of the bit to be changed. Otherwise, print ``Corrupt".

Code for Program that reads in a matrix and checks if it has the parity property in C++ Programming

 # include <iostream.h>
 # include <fstream.h>
 # include <string.h>
 # include <stdlib.h>
 # include <conio.h>

 int main( )
    {
       clrscr( );

       fstream File("CP-24.txt",ios::in|ios::nocreate);

       if(!File)
      {
         cout<<"\n Unable to open the input file."<<endl;
         cout<<"\n Press any key to exit.";

         getch( );
         exit(EXIT_FAILURE);
      }

       char Data[220]={NULL};

       do
      {
         strset(Data,NULL);

         File.getline(Data,100);

         if(strcmp(Data,"0")==0)
        break;

         int matrix[101][101]={0};

         int n=atoi(Data);

         int i;
         int j;

         for(i=0;i<n;i++)
        {
           strset(Data,NULL);

           File.getline(Data,200);

           for(j=0;j<n;j++)
              matrix[i][j]=(int(Data[(j*2)])-48);
        }

         for(i=0;i<n;i++)
        {
           for(j=0;j<n;j++)
              {
             matrix[i][n]+=matrix[i][j];
             matrix[n][i]+=matrix[j][i];
              }
        }

         int parity=1;
         int corrupt_rows=0;
         int corrupt_columns=0;

         for(i=0;i<n;i++)
        {
           if((matrix[i][n]%2)!=0 || (matrix[n][i]%2)!=0)
              {
             if((matrix[i][n]%2)!=0)
                corrupt_rows++;

             elseif((matrix[n][i]%2)!=0)
                corrupt_columns++;

             parity=0;
              }
        }

         if(parity==1)
        cout<<"Ok"<<endl;

         elseif(parity==0)
        {
           if(corrupt_rows>1 || corrupt_columns>1)
              cout<<"Corrupt"<<endl;

           else
              {
             int row=0;
             int column=0;

             for(i=0;i<n;i++)
                {
                   if((matrix[i][n]%2)!=0)
                  row=i;

                   if((matrix[n][i]%2)!=0)
                  column=i;
                }

             if(matrix[row][column]==0)
                matrix[row][column]=1;

             elseif(matrix[row][column]==1)
                matrix[row][column]=0;

             matrix[row][n]=0;
             matrix[n][column]=0;

             for(i=0;i<n;i++)
                {
                   matrix[row][n]+=matrix[row][i];
                   matrix[n][column]+=matrix[i][column];
                }

             if((matrix[row][n]%2)!=0 || (matrix[n][column]%2)!=0)
                cout<<"Corrupt"<<endl;

             else
                cout<<"Chamge Bit ("<<(row+1)<<","<<(column+1)<<")"<<endl;
              }
        }
      }
       while(1);

       File.close( );

       getch( );
       return 0;
    }
  
Share: 



Easy Tutor
Easy Tutor author of Program that reads in a matrix and checks if it has the parity property is from United States. Easy Tutor says

Hello Friends,

I am Free Lance Tutor, who helped student in completing their homework.

I have 4 Years of hands on experience on helping student in completing their homework. I also guide them in doing their final year projects.

I have share many programs on this website for everyone to use freely, if you need further assistance, than please contact me on easytutor.2ya [at the rate] gmail [dot] com

I have special discount scheme for providing tutor services. I am providing tutor service to students from various contries, currently most of my students are from United States, India, Australia, Pakistan, Germany, UK and Canada.

I am also here to expand my technical network to receive more opportunity in my career, make friends to help them in resolving their technical problem, learn and share my knowledge, If you like to be my friend, Please send me friend request.

Thanks,
Happy Programming :)

 
View All Articles

Related Articles and Code:


 

Other Interesting Articles in C++ Programming:


 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
No Comment Found, Be the First to post comment!