Program For Hamming code generation,Error detection and correction:----------
#include<iostream.h>
#include<conio.h>
void main()
{
int data[7], c0, c1, c2, op,i, loc;
char a[4], b[7];
clrscr();
do
{
printf("\n 1. Hamming code generation");
printf("\n 2. Error detection and correction");\
printf("\n 3. Quit");
printf("\n 4. Enter choice :-");
scanf("%d", &op);
switch(op)
{
case 1:
printf("\n Enter a 4 bit data");
scanf("%s", a)
for(i=0; i<7;i++)
{
data[i]=0;
}
data[0]=a[0]-'0';
data[1]=a[1]-'0';
data[2]=a[2]-'0';
data[4]=a[3]-'0';
data[6]=(data[0]+data[2]+data[4])%2;
data[5]=(data[4]+data[1]+data[0])%2;
data[3]=(data[0]+data[1]+data[2])%2;
printf("\n data as encoded with hamming parity bits :\n");
printf("\n d3 d2 d1 p2 d0 p1 p0");
printf("\n");
for(i=0; i<7; i++)
{
printf("%d", data[i]);
}
break;
case 2:
printf("\nEnter a 7 bit hamming coded data :")
scanf("%s", b);
for(i=0;i<7;i++)
data[i]=b[i]-'0';
c0=(data[6]+data[0]+data[2]+data[4])%2;
c1=(data[5]+data[4]+data[1]+data[0])%2;
c2=(data[3]+data[0]+data[1]+data[2])%2;
loc=7-(4*c2+2*c1+c0);
if(loc==0)
printf("\n No Error");
else
{
printf("\nError at the location = %d", loc);
printf("\nCorrected data :");
data[loc]=(!data[loc]);
printf("\n");
for(i=0;i<7;i++)
printf("%d", data[i]);
}
break;
}
}while(op!=3);
}
#include<iostream.h>
#include<conio.h>
void main()
{
int data[7], c0, c1, c2, op,i, loc;
char a[4], b[7];
clrscr();
do
{
printf("\n 1. Hamming code generation");
printf("\n 2. Error detection and correction");\
printf("\n 3. Quit");
printf("\n 4. Enter choice :-");
scanf("%d", &op);
switch(op)
{
case 1:
printf("\n Enter a 4 bit data");
scanf("%s", a)
for(i=0; i<7;i++)
{
data[i]=0;
}
data[0]=a[0]-'0';
data[1]=a[1]-'0';
data[2]=a[2]-'0';
data[4]=a[3]-'0';
data[6]=(data[0]+data[2]+data[4])%2;
data[5]=(data[4]+data[1]+data[0])%2;
data[3]=(data[0]+data[1]+data[2])%2;
printf("\n data as encoded with hamming parity bits :\n");
printf("\n d3 d2 d1 p2 d0 p1 p0");
printf("\n");
for(i=0; i<7; i++)
{
printf("%d", data[i]);
}
break;
case 2:
printf("\nEnter a 7 bit hamming coded data :")
scanf("%s", b);
for(i=0;i<7;i++)
data[i]=b[i]-'0';
c0=(data[6]+data[0]+data[2]+data[4])%2;
c1=(data[5]+data[4]+data[1]+data[0])%2;
c2=(data[3]+data[0]+data[1]+data[2])%2;
loc=7-(4*c2+2*c1+c0);
if(loc==0)
printf("\n No Error");
else
{
printf("\nError at the location = %d", loc);
printf("\nCorrected data :");
data[loc]=(!data[loc]);
printf("\n");
for(i=0;i<7;i++)
printf("%d", data[i]);
}
break;
}
}while(op!=3);
}
No comments:
Post a Comment