C++ Program code for detecting and correcting error in the 7-bit hamming code
I developed this code to detect and correct the error for a 7-bit hamming code for both even and odd parity.Hope its useful to you!!
Source code:
#include<iostream.h>
#include<conio.h>
void checke();
void checko();
int main()
{
int c;
#include<conio.h>
void checke();
void checko();
int main()
{
int c;
cout<<"even parity-0 & odd parity-1";
cin>>c;
if(c==0)
checke();
else
checko();
return 0;
}
void checke()
{ int x[7];
int n1,n2,n3,n4,p1,p2,p4;
cout<<"enter the 7-bit hamming code";
for(int i=6;i>=0;i--)
cin>>x[i];
cout<<"checking at p1"<<"\n";
if (x[0]==1)
n1=1;
else
n1=0;
if(x[2]==1)
n2=1;
else
n2=0;
if(x[4]==1)
n3=1;
else
n3=0;
if(x[6]==1)
n4=1;
else
n4=0;
if((n4+n3+n2+n1)%2==0)
p1=x[0];
else
{if(x[0]==1)
p1=0;
else
p1=1;
cout<<"error at p1" "\n"<<"hence p1="<<p1<<"\n"; }
cout<<"checking at p2"<<"\n";
if (x[1]==1)
n1=1;
else
n1=0;
if(x[2]==1)
n2=1;
else
n2=0;
if(x[5]==1)
n3=1;
else
n3=0;
if(x[6]==1)
n4=1;
else
n4=0;
if((n4+n3+n2+n1)%2==0)
p2=x[1];
else
{
if(x[1]==1)
p2=0;
else
p2=1;
cout<<"error at p2" "\n"<<"hence p2="<<p2<<"\n";}
cout<<"checking for p4"<<"\n";
if (x[3]==1)
n1=1;
else
n1=0;
if(x[4]==1)
n2=1;
else
n2=0;
if(x[5]==1)
n3=1;
else
n3=0;
if(x[6]==1)
n4=1;
else
n4=0;
if((n4+n3+n2+n1)%2==0)
p4=x[3];
else
{
if(x[3]==1)
p4=0;
else
p4=1;
cout<<"error at p4" "\n"<<"hence p4="<<p4<<"\n"; }
int sum;
sum=4*p4+2*p2+1*p1;
cout<<"error is at"<<sum<<"th position in the hamming code"<<"\n";
cout<<"hence the corrected code is:";
if(x[sum-1]==1)
x[sum-1]=0;
else
x[sum-1]=1;
for(i=6;i>=0;i--)
cout<< x[i];
}
void checko()
{ int x[7];
int n1,n2,n3,n4,p1,p2,p4;
cout<<"enter the 7-bit hamming code";
for(int i=6;i>=0;i--)
cin>>x[i];
cout<<"checking at p1"<<"\n";
if (x[0]==1)
n1=1;
else
n1=0;
if(x[2]==1)
n2=1;
else
n2=0;
if(x[4]==1)
n3=1;
else
n3=0;
if(x[6]==1)
n4=1;
else
n4=0;
if((n4+n3+n2+n1)%2!=0)
p1=x[0];
else
{if(x[0]==1)
p1=0;
else
p1=1;
cout<<"error at p1" "\n"<<"hence p1="<<p1<<"\n"; }
cout<<"checking at p2"<<"\n";
if (x[1]==1)
n1=1;
else
n1=0;
if(x[2]==1)
n2=1;
else
n2=0;
if(x[5]==1)
n3=1;
else
n3=0;
if(x[6]==1)
n4=1;
else
n4=0;
if((n4+n3+n2+n1)%2!=0)
p2=x[1];
else
{
if(x[1]==1)
p2=0;
else
p2=1;
cout<<"error at p2" "\n"<<"hence p2="<<p2<<"\n";}
cout<<"checking for p4"<<"\n";
if (x[3]==1)
n1=1;
else
n1=0;
if(x[4]==1)
n2=1;
else
n2=0;
if(x[5]==1)
n3=1;
else
n3=0;
if(x[6]==1)
n4=1;
else
n4=0;
if((n4+n3+n2+n1)%2!=0)
p4=x[3];
else
{
if(x[3]==1)
p4=0;
cin>>c;
if(c==0)
checke();
else
checko();
return 0;
}
void checke()
{ int x[7];
int n1,n2,n3,n4,p1,p2,p4;
cout<<"enter the 7-bit hamming code";
for(int i=6;i>=0;i--)
cin>>x[i];
cout<<"checking at p1"<<"\n";
if (x[0]==1)
n1=1;
else
n1=0;
if(x[2]==1)
n2=1;
else
n2=0;
if(x[4]==1)
n3=1;
else
n3=0;
if(x[6]==1)
n4=1;
else
n4=0;
if((n4+n3+n2+n1)%2==0)
p1=x[0];
else
{if(x[0]==1)
p1=0;
else
p1=1;
cout<<"error at p1" "\n"<<"hence p1="<<p1<<"\n"; }
cout<<"checking at p2"<<"\n";
if (x[1]==1)
n1=1;
else
n1=0;
if(x[2]==1)
n2=1;
else
n2=0;
if(x[5]==1)
n3=1;
else
n3=0;
if(x[6]==1)
n4=1;
else
n4=0;
if((n4+n3+n2+n1)%2==0)
p2=x[1];
else
{
if(x[1]==1)
p2=0;
else
p2=1;
cout<<"error at p2" "\n"<<"hence p2="<<p2<<"\n";}
cout<<"checking for p4"<<"\n";
if (x[3]==1)
n1=1;
else
n1=0;
if(x[4]==1)
n2=1;
else
n2=0;
if(x[5]==1)
n3=1;
else
n3=0;
if(x[6]==1)
n4=1;
else
n4=0;
if((n4+n3+n2+n1)%2==0)
p4=x[3];
else
{
if(x[3]==1)
p4=0;
else
p4=1;
cout<<"error at p4" "\n"<<"hence p4="<<p4<<"\n"; }
int sum;
sum=4*p4+2*p2+1*p1;
cout<<"error is at"<<sum<<"th position in the hamming code"<<"\n";
cout<<"hence the corrected code is:";
if(x[sum-1]==1)
x[sum-1]=0;
else
x[sum-1]=1;
for(i=6;i>=0;i--)
cout<< x[i];
}
void checko()
{ int x[7];
int n1,n2,n3,n4,p1,p2,p4;
cout<<"enter the 7-bit hamming code";
for(int i=6;i>=0;i--)
cin>>x[i];
cout<<"checking at p1"<<"\n";
if (x[0]==1)
n1=1;
else
n1=0;
if(x[2]==1)
n2=1;
else
n2=0;
if(x[4]==1)
n3=1;
else
n3=0;
if(x[6]==1)
n4=1;
else
n4=0;
if((n4+n3+n2+n1)%2!=0)
p1=x[0];
else
{if(x[0]==1)
p1=0;
else
p1=1;
cout<<"error at p1" "\n"<<"hence p1="<<p1<<"\n"; }
cout<<"checking at p2"<<"\n";
if (x[1]==1)
n1=1;
else
n1=0;
if(x[2]==1)
n2=1;
else
n2=0;
if(x[5]==1)
n3=1;
else
n3=0;
if(x[6]==1)
n4=1;
else
n4=0;
if((n4+n3+n2+n1)%2!=0)
p2=x[1];
else
{
if(x[1]==1)
p2=0;
else
p2=1;
cout<<"error at p2" "\n"<<"hence p2="<<p2<<"\n";}
cout<<"checking for p4"<<"\n";
if (x[3]==1)
n1=1;
else
n1=0;
if(x[4]==1)
n2=1;
else
n2=0;
if(x[5]==1)
n3=1;
else
n3=0;
if(x[6]==1)
n4=1;
else
n4=0;
if((n4+n3+n2+n1)%2!=0)
p4=x[3];
else
{
if(x[3]==1)
p4=0;
else
p4=1;
cout<<"error at p4" "\n"<<"hence p4="<<p4<<"\n";1 }
p4=1;
cout<<"error at p4" "\n"<<"hence p4="<<p4<<"\n";1 }
int sum;
sum=4*p4+2*p2+1*p1;
cout<<"error is at"<<sum<<"th bit in the hamming code"<<"\n";
cout<<"hence the corrected code is:";
if(x[sum-1]==1)
x[sum-1]=0;
else
x[sum-1]=1;
for(i=6;i>=0;i--)
cout<< x[i];
}
sum=4*p4+2*p2+1*p1;
cout<<"error is at"<<sum<<"th bit in the hamming code"<<"\n";
cout<<"hence the corrected code is:";
if(x[sum-1]==1)
x[sum-1]=0;
else
x[sum-1]=1;
for(i=6;i>=0;i--)
cout<< x[i];
}
No comments:
Post a Comment