KEMBAR78
Click To Download Hamming Code Final Program | PDF | Computer Programming | Software Engineering
0% found this document useful (0 votes)
8 views3 pages

Click To Download Hamming Code Final Program

The document contains a C++ program that implements Hamming code for error detection and correction. It allows users to input data, generate a Hamming code, introduce errors, and then detect and correct those errors. The program supports both fixed 11-bit data and user-defined 4-bit data with even parity only.

Uploaded by

ekanshk7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views3 pages

Click To Download Hamming Code Final Program

The document contains a C++ program that implements Hamming code for error detection and correction. It allows users to input data, generate a Hamming code, introduce errors, and then detect and correct those errors. The program supports both fixed 11-bit data and user-defined 4-bit data with even parity only.

Uploaded by

ekanshk7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

#include <iostream>

#include <vector>
#include <cmath>
#include <string>
#include <cstdlib>
using namespace std;

int calculateParity(vector<int>& bits, int pos) {


int parity = 0;
for (int i = 1; i < bits.size(); ++i) {
if (i & pos) parity ^= bits[i];
}
return parity;
}

vector<int> generateHammingCode(string data) {


int m = data.size();
int r = 0;
while ((1 << r) < m + r + 1) r++;
int n = m + r;

vector<int> code(n + 1, 0); // 1-based indexing


int j = 0;
for (int i = 1; i <= n; ++i) {
if ((i & (i - 1)) != 0) {
code[i] = data[j++] - '0';
}
}
for (int i = 0; i < r; ++i) {
int pos = 1 << i;
code[pos] = calculateParity(code, pos);
}

return code;
}

void introduceError(vector<int>& code, int pos) {


if (pos > 0 && pos < code.size()) {
code[pos] ^= 1;
}
}

pair<vector<int>, int> detectAndCorrect(vector<int>& code) {


int r = log2(code.size());
int errorPos = 0;
for (int i = 0; (1 << i) < code.size(); ++i) {
int pos = 1 << i;
int parity = calculateParity(code, pos);
if (parity != 0) errorPos += pos;
}
if (errorPos > 0 && errorPos < code.size()) {
cout << "Error detected at position: " << errorPos << endl;
code[errorPos] ^= 1;
} else {
cout << "No error detected." << endl;
}
return {code, errorPos};
}

string extractData(vector<int>& code) {


string data = "";
for (int i = 1; i < code.size(); ++i) {
if ((i & (i - 1)) != 0) {
data += to_string(code[i]);
}
}
return data;
}

void printCodeword(vector<int>& code) {


for (int i = 1; i < code.size(); ++i) cout << code[i];
cout << endl;
}

int main() {
int choice;
cout << "Choose Mode:\n1. 11-bit Data (Q1)\n2. 4-bit Data (Q2)\nEnter: ";
cin >> choice;

string dataword;
if (choice == 1) {
dataword = "00101101011";
cout << "Q1: Given 11-bit Dataword = " << dataword << endl;
} else if (choice == 2) {
cout << "Input 4-bit data word (e.g., 1101): ";
cin >> dataword;
} else {
cout << "Invalid choice.\n";
return 1;
}

int parityType;
cout << "Input parity (Odd-1 / Even-0): ";
cin >> parityType;
if (parityType != 0) {
cout << "Only Even Parity is supported.\n";
return 0;
}

vector<int> code = generateHammingCode(dataword);


cout << "\nSender Side:\nCodeword: ";
printCodeword(code);

int introduce;
cout << "Introduce error? (1-Yes / 0-No): ";
cin >> introduce;

if (introduce == 1) {
int errorPos;
if (choice == 1) {
errorPos = 7; // For Q1, flip at 7th position from LSB
cout << "Flipping bit at position: 7 (from LSB side)\n";
} else {
string mode;
cout << "Introduce Error: Manual / Random? ";
cin >> mode;
if (mode == "Manual") {
cout << "Enter bit position to flip (1-based from right/LSB): ";
cin >> errorPos;
} else {
errorPos = rand() % (code.size() - 1) + 1;
cout << "Random bit flipped at position: " << errorPos << endl;
}
}
introduceError(code, errorPos);
cout << "Codeword after error: ";
printCodeword(code);
}

cout << "\nReceiver Side:\nReceived Codeword: ";


printCodeword(code);
auto [corrected, errPos] = detectAndCorrect(code);

cout << "Corrected Codeword: ";


printCodeword(corrected);

string original = extractData(corrected);


cout << "Recovered Dataword: " << original << endl;

return 0;
}

You might also like