-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHamming Code.cpp
45 lines (44 loc) · 1.17 KB
/
Hamming Code.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/* Hamming Code */
#include<stdio.h>
#include<conio.h>
int main ()
{int data[20];
int dataatrec[20],c,c1,c2,c3,i;
printf("Enter 4bits data one by one");
scanf("%d",&data[0]);
scanf("%d",&data[1]);
scanf("%d",&data[2]);
scanf("%d",&data[4]);
data[6]=data[0]^data[2]^data[4];
data[5]=data[0]^data[1]^data[4];
data[3]=data[0]^data[1]^data[2];
printf("Encoded data is \n:");
for(i=0;i<7;i++)
printf("%d",data[i]);
printf("\nEnter received data bits one by one \n");
for(i=0;i<7;i++)
scanf("%d",&dataatrec[i]);
c1=dataatrec[6]^dataatrec[4]^dataatrec[2]^dataatrec[0];
c2=dataatrec[5]^dataatrec[4]^dataatrec[1]^dataatrec[0];
c3=dataatrec[3]^dataatrec[2]^dataatrec[1]^dataatrec[0];
c=c3*4+c2*2+c1;
if(c==0 &&dataatrec[6]==data[6]&&dataatrec[5]==data[5]&&dataatrec[3]==data[3])
{printf("\nNo error while transmit the data");
}
else { printf("\n on possitio %d", c);
printf("\n data sent ");
for(i=0;i<7;i++)
printf("%d",dataatrec[i]);
printf("\n correct message is");
if(dataatrec[7-c]==0)
dataatrec[7-c]=1;
else
dataatrec[7-c]=0;
for(i=0;i<7;i++)
{
printf("%d", data[i]);
}
}
return 0;
getch();
}