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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
| #include <iostream>
//#include<conio.h>
using namespace std;
// HAMMING CODE FOR 4-bit data. 2^2 => 2 + 1 = 3
// Error detection bits req = 3
// Total bits required = 4 + 3 = 7 bits
// R holding the error detection bits.
// 2^0 = 1
// 2^1 = 2
// 2^2 = 4
// Frame: d7 d6 d5 r4 d3 r2 r1
int i=0;
int d3, d5, d6, d7;
int r1, r2, r4;
void sender()
{
cout<<" Enter the 4-bit word: ";
cin>>d7;
cin>>d6;
cin>>d5;
cin>>d3;
// What all positions it affects:
// r1: 1, 3, 5, 7
// r2: 2, 3, 6, 7
// r4: 4, 5, 6, 7
// Then, check whether it contains even number of 1s or not.
// If even, put 0, else put 1.
r1= (d3 + d5 + d7) %2;
r2= (d3 + d6 + d7) %2;
r4= (d5 + d6 + d7) %2;
// FRAME:
int frame[] = { r1, r2, d3, r4, d5, d6, d7 };
cout <<"\n The code at SENDER is: ";
for(i=6; i>=0; i--)
cout<<frame[i]<<" ";
}
void receiver()
{
int frame[7];
cout<<"\n Enter the code at RECEIVER: ";
for(i=6; i>=0; i--)
cin>>frame[i];
d7 = frame[6];
d6 = frame[5];
d5 = frame[4];
r4 = frame[3];
d3 = frame[2];
r2 = frame[1];
r1 = frame[0];
// What all positions Rs it affects:
// r1: 1, 3, 5, 7
// r2: 2, 3, 6, 7
// r4: 4, 5, 6, 7
//If not even
r1= (r1 + d3 + d5 + d7) %2;
r2= (r2 + d3 + d6 + d7) %2;
r4= (r4 + d5 + d6 + d7) %2;
//Find position by converting binary no r4 r2 r1 to decimal
int dec=0;
if( r1 == 0 && r2 == 0 && r4 == 0 )
cout<<" No Errors. Message is = "<<d7<<d6<<d5<<d3;
else
{
if( r4 == 1)
dec += 4;
if(r2 == 1)
dec += 2;
if(r1 == 1)
dec += 1;
cout<<"\n Error found at position "<<dec;
}
}
int main ( )
{
// clrscr();
cout<<"\n FRAME: d7 d6 d5 r4 d3 r2 r1 "<<endl;
cout<<endl;
sender();
cout<<endl;
receiver();
cout<<endl;
// getch();
}
|