Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Hamming code
- #include<bits/stdc++.h>
- using namespace std;
- int main(){
- int a[12];
- cout << "Enter 7 data bits to be transmitted\n";
- for(int i = 11; i > 0; i--){
- if(i!=1 && i!=2 && i!=4 && i!=8){
- cin >> a[i];
- }
- }
- a[1] = (a[3]+a[5]+a[7]+a[9]+a[11])%2;
- a[2] = (a[3]+a[6]+a[7]+a[10]+a[11])%2;
- a[4] = (a[5]+a[6]+a[7])%2;
- a[8] = (a[9]+a[10]+a[11])%2;
- cout << "\nHamming code : ";
- for(int i = 11; i > 0; i--){
- cout << a[i] << " ";
- }
- cout << "\nDo you want to make any error (Y/N) : ";
- char c;
- cin >> c;
- if(c == 'Y'){
- cout << "\nEnter the position to make error : ";
- int pos;
- cin >> pos;
- a[pos] = ((a[pos] == 0)?1:0);
- }
- cout << "\nData Received : ";
- for(int i = 11; i > 0; i--){
- cout << a[i] << " ";
- }
- int r1,r2,r4,r8;
- r1 = (a[1]+a[3]+a[5]+a[7]+a[9]+a[11])%2;
- r2 = (a[2]+a[3]+a[6]+a[7]+a[10]+a[11])%2;
- r4 = (a[4]+a[5]+a[6]+a[7])%2;
- r8 = (a[8]+a[9]+a[10]+a[11])%2;
- int error_position = (r8*8) + (r4*4) + (r2*2) + (r1*1);
- if(error_position){
- cout << "\nError detected at position " << error_position;
- a[error_position] = ((a[error_position] == 0)?1:0);
- }
- else{
- cout << "\nNo error occured\n";
- }
- cout << "\nFinal Data : ";
- for(int i = 11; i > 0; i--){
- cout << a[i] << " ";
- }
- }
- /*
- OUTPUT
- Enter 7 data bits to be transmitted
- 1 0 0 1 1 0 1
- Hamming code : 1 0 0 1 1 1 0 0 1 0 1
- Do you want to make any error (Y/N) : Y
- Enter the position to make error : 7
- Data Received : 1 0 0 1 0 1 0 0 1 0 1
- Error detected at position 7
- Final Data : 1 0 0 1 1 1 0 0 1 0 1
- */
Add Comment
Please, Sign In to add comment