-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHammingCode.cpp
181 lines (143 loc) · 4.55 KB
/
HammingCode.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
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#include <bits/stdc++.h>
using namespace std;
#define mx 100
int main ()
{
//freopen("in.txt", "r", stdin);
int k;
cout << "Enter the No of Data Bits you want to Enter (Maximum 20) : ";
cin >> k;
int row = k, col = 1;
int dataWord[mx][mx];
cout << "Enter the Data Bits One by One:\n";
for(int i=1; i<=row; i++)
for(int j=1; j<=col; j++)
cin >> dataWord[i][j];
cout << "\nData bits entered(block wise):\n";
for(int i=1; i<=row; i++){
for(int j=1; j<=col; j++){
cout << dataWord[i][j] << " ";
}
cout << endl;
}
int r = 0;
while(col+1+r > pow(2, r)) r++;
int codeWord[mx][mx];
int parityBit = 0;
int y = 0;
col += r;
for(int i=1; i<=row; i++){
parityBit = 0;
y = 1;
for(int j=1; j<=col; j++){
if (j == pow(2, parityBit)){
codeWord[i][j] = 0;
parityBit++;
}
else
codeWord[i][j] = dataWord[i][y++];
}
}
cout << "\nData Bits are Encoded with Parity bits(0):\n";
for(int i=1; i<=row; i++){
for(int j=1; j<=col; j++)
cout << codeWord[i][j] << " ";
cout << endl;
}
for(int i=1; i<=row; i++){
parityBit = 0;
for(int j=1; j<=col; j=pow(2, parityBit)){
parityBit++;
int parity = 0;
int t = j, cnt = 0;
while(t <= col){
parity = parity ^ codeWord[i][t];
t++;
cnt++;
if(cnt == j){
t = t+j;
cnt = 0;
}
}
codeWord[i][j] = parity;
}
}
cout << "\nHamming codeword bits for even parity are(block wise):\n";
for (int i=1; i<=row; i++){
for(int j=1; j<=col; j++)
cout << codeWord[i][j] << " ";
cout << endl;
}
cout << endl;
cout << "Do you want to send this Data (Y/N): ";
char c; cin >> c;
if(c == 'y' or c == 'Y'){
cout << endl << "Transmitted Data:\n";
for(int j=1; j<=col; j++){
for(int i=1; i<=row; i++)
cout << codeWord[i][j] << ' ';
}
cout << "\n\nEnter received codeword one by one (According to transmitted data):\n";
int receivedData[mx][mx];
for(int j=1; j<=col; j++){
for(int i=1; i<=row; i++)
cin >> receivedData[i][j];
}
cout << endl << "Received code word:\n";
for(int i=1; i<=row; i++){
for(int j=1; j<=col; j++)
cout << receivedData[i][j] << " ";
cout << endl;
}
cout << endl;
vector <int> parityData[mx];
bool error = false;
cout << "Position to be XOR:\n\n";
for(int i=1; i<=row; i++){
parityBit = 0;
for(int j=1; j<=col; j=pow(2, parityBit)){
parityBit++;
int parity = 0;
int t = j, cnt = 0;
cout << "Position " << i << "," << j << ": ";
while(t <= col){
cout << t << ' ';
parity = parity ^ receivedData[i][t];
t++;
cnt++;
if(cnt == j){
t = t+j;
cnt = 0;
}
}
cout << endl;
parityData[i].push_back(parity);
if(parity ^ receivedData[i][j] != receivedData[i][j])
error = true;
}
cout << endl;
}
if(error){
cout << "Data bits are corrupted!!!" << endl << endl;
cout << "Correct Data bits are:\n";
for(int i=1; i<=row; i++){
int pos = 0;
for(int j=0; j<=parityData[i].size(); j++){
if(parityData[i][j] == 1)
pos = pos + pow(2, j);
}
receivedData[i][pos] = receivedData[i][pos] ^ 1;
}
cout << endl;
for(int i=1; i<=row; i++){
for(int j=1; j<=col; j++)
cout << receivedData[i][j] << " ";
cout << endl;
}
}
else
cout << "Data are fine" << endl;
cout << endl << endl;
}
return 0;
}