-
Notifications
You must be signed in to change notification settings - Fork 101
/
hamingcode.cpp
51 lines (50 loc) · 1.31 KB
/
hamingcode.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
#include<bits/stdc++.h>
using namespace std;
int main(){
int data[10];
int data_[10],c,c1,c2,c3,i;
cout<<"enter 4 bits of data"<<endl;
cin>>data[0]>>data[1]>>data[2]>>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];
cout<<"Encoded data is: "<<endl;
for(int i=0;i<7;i++){
cout<<data[i];
}
cout<<endl;
cout<<"Enter recieved data: "<<endl;
for(int i=0;i<7;i++){
cin>>data_[i];
}
c1=data_[6]^data_[4]^data_[2]^data_[0];
c2=data_[5]^data_[4]^data_[1]^data_[0];
c3=data_[3]^data_[2]^data_[1]^data_[0];
c=c3*4+c2*2+c1;
if(c==0 && data_[6]==data[6] && data_[5]==data[5] && data_[3]==data[3]){
cout<<"NO ERROR"<<endl;
}
else{
cout<<"Error on position: "<<c<<endl;
cout<<"Data sent"<<endl;
for(int i=0;i<7;i++){
cout<<data[i];
}
cout<<endl;
cout<<"data recieved: "<<endl;
for(int i=0;i<7;i++){
cout<<data_[i];
}
cout<<endl;
cout<<"Correct message is: "<<endl;
if(data_[7-c]==0){
data_[7-c]==1;
}
else{
data_[7-c]=0;
}
for(int i=0;i<7;i++){
cout<<data[i];
}
}
}