Skip to content

Instantly share code, notes, and snippets.

@swimmi
Created April 21, 2014 14:02
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save swimmi/11143637 to your computer and use it in GitHub Desktop.
Save swimmi/11143637 to your computer and use it in GitHub Desktop.
Check a matrix if it has the parity property
#include <fstream>
#include <iostream>
using namespace std;
//Check a matrix if it has the parity property.
int main()
{
//ifstream cin("error_correction.txt");
int n;
while(cin>>n)
{
if(n==0) break;
int r[100]={0},c[100]={0}; //each row and column sum
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
{
int num;
cin>>num;
if(num==1)
{
r[i]++;c[j]++;
}
}
int ro=0,co=0;
for(int i=0;i<n;i++)
{
if(r[i]%2==1)
{
if(ro!=0)
{
ro=-1;break; //more than one odd sum in rows
}
ro=i+1; //odd sum in rows
}
if(c[i]%2==1)
{
if(co!=0)
{
co=-1;break; //more than one odd sum in columns
}
co=i+1; //odd sum in columns
}
}
if(ro==co&&ro==0) cout<<"OK"<<endl;
if(ro>0&&co>0) printf("Change bit (%d,%d)\n",ro,co);
if(ro<0||co<0) cout<<"Corrupt"<<endl;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment