Compare commits
4 Commits
a27584fc48
...
a1e2a8cbdc
Author | SHA1 | Date | |
---|---|---|---|
a1e2a8cbdc | |||
404045e1f0 | |||
4a88dd9f0f | |||
197fc7f126 |
103
Codes/Code-A4 (Hamming Code).cpp
Normal file
103
Codes/Code-A4 (Hamming Code).cpp
Normal file
@ -0,0 +1,103 @@
|
||||
#include <iostream>
|
||||
#include <cmath>
|
||||
#include <vector>
|
||||
using namespace std;
|
||||
|
||||
// Function to calculate the number of parity bits needed
|
||||
int calculateParityBits(int dataBits) {
|
||||
int parityBits = 0;
|
||||
while (pow(2, parityBits) < dataBits + parityBits + 1) {
|
||||
parityBits++;
|
||||
}
|
||||
return parityBits;
|
||||
}
|
||||
|
||||
// Function to encode the data using Hamming code
|
||||
vector<int> encodeData(vector<int> data) {
|
||||
int dataBits = data.size();
|
||||
int parityBits = calculateParityBits(dataBits);
|
||||
vector<int> encoded(dataBits + parityBits, 0);
|
||||
|
||||
// Set the data bits
|
||||
int j = 0;
|
||||
for (int i = 0; i < encoded.size(); i++) {
|
||||
if (i + 1 == pow(2, j)) {
|
||||
j++;
|
||||
} else {
|
||||
encoded[i] = data[i - j];
|
||||
}
|
||||
}
|
||||
|
||||
// Calculate and set the parity bits
|
||||
for (int i = 0; i < parityBits; i++) {
|
||||
int parityBit = pow(2, i);
|
||||
int sum = 0;
|
||||
for (int j = parityBit - 1; j < encoded.size(); j += 2 * parityBit) {
|
||||
for (int k = 0; k < parityBit; k++) {
|
||||
if (j + k < encoded.size()) {
|
||||
sum += encoded[j + k];
|
||||
}
|
||||
}
|
||||
}
|
||||
encoded[parityBit - 1] = sum % 2;
|
||||
}
|
||||
|
||||
return encoded;
|
||||
}
|
||||
|
||||
// Function to check for errors in the encoded data
|
||||
int checkForErrors(vector<int> encoded) {
|
||||
int parityBits = calculateParityBits(encoded.size() - calculateParityBits(encoded.size()));
|
||||
int errorPosition = 0;
|
||||
|
||||
for (int i = 0; i < parityBits; i++) {
|
||||
int parityBit = pow(2, i);
|
||||
int sum = 0;
|
||||
for (int j = parityBit - 1; j < encoded.size(); j += 2 * parityBit) {
|
||||
for (int k = 0; k < parityBit; k++) {
|
||||
if (j + k < encoded.size()) {
|
||||
sum += encoded[j + k];
|
||||
}
|
||||
}
|
||||
}
|
||||
errorPosition += (sum % 2) * parityBit;
|
||||
}
|
||||
|
||||
return errorPosition;
|
||||
}
|
||||
|
||||
int main() {
|
||||
int dataBits;
|
||||
cout<<"Enter the number of data bits:\t";
|
||||
cin >> dataBits;
|
||||
|
||||
vector<int> data(dataBits);
|
||||
cout<<endl<<"NOTE: Make sure the bits are entered in binary format, separated by spaces.\nEg. 1 0 0 1 (for 4 data bits).";
|
||||
cout<<endl<<"Enter the data bits:\t";
|
||||
for (int i = 0; i < dataBits; i++) {
|
||||
cin >> data[i];
|
||||
}
|
||||
|
||||
vector<int> encoded = encodeData(data);
|
||||
cout<<endl<<"--------------------"<<endl;
|
||||
cout<<"Encoded bits are:\t";
|
||||
for (int bit : encoded) {
|
||||
cout << bit << " ";
|
||||
}
|
||||
cout<<endl<<"--------------------"<<endl;
|
||||
|
||||
cout<<endl<<"Enter the encoded bits:\t";
|
||||
vector<int> receivedEncoded(encoded.size());
|
||||
for (int i = 0; i < encoded.size(); i++) {
|
||||
cin >> receivedEncoded[i];
|
||||
}
|
||||
|
||||
int errorPosition = checkForErrors(receivedEncoded);
|
||||
if (errorPosition == 0) {
|
||||
cout<<"No errors detected."<<endl;
|
||||
} else {
|
||||
cout<<"Error detected at position: "<<errorPosition<<endl;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
BIN
Printable outputs/Output-A4.pdf
Normal file
BIN
Printable outputs/Output-A4.pdf
Normal file
Binary file not shown.
@ -25,10 +25,11 @@ This Git repository is a comprehensive resource for the Computer Networks and Se
|
||||
> No code or output for assignment A1.
|
||||
|
||||
##### A4 - Error detection and correction using Hamming code and CRC
|
||||
- [Code-A4](https://git.kska.io/sppu-te-comp-content/ComputerNetworksAndSecurity/src/branch/main/Codes/Code-A4.cpp)
|
||||
- [Code-A4 (CRC)](https://git.kska.io/sppu-te-comp-content/ComputerNetworksAndSecurity/src/branch/main/Codes/Code-A4%20%28CRC%29.cpp)
|
||||
- [Code-A4 (Hamming Code)](https://git.kska.io/sppu-te-comp-content/ComputerNetworksAndSecurity/src/branch/main/Codes/Code-A4%20%28Hamming%20Code%29.cpp)
|
||||
- [Handout-A4](https://git.kska.io/sppu-te-comp-content/ComputerNetworksAndSecurity/src/branch/main/Handouts/Handout-A4.pdf)
|
||||
- [Write-up - A4](https://git.kska.io/sppu-te-comp-content/ComputerNetworksAndSecurity/src/branch/main/Write-ups/Write-up%20-%20A4.pdf)
|
||||
- [Output-A4] // WORKING ON IT!
|
||||
- [Output-A4](https://git.kska.io/sppu-te-comp-content/ComputerNetworksAndSecurity/src/branch/main/Printable%20outputs/Output-A4.pdf)
|
||||
|
||||
##### A5 - Sliding Window Protocol
|
||||
- [Code-A5](https://git.kska.io/sppu-te-comp-content/ComputerNetworksAndSecurity/src/branch/main/Codes/Code-A5%20%28Sliding%20Window%29.cpp)
|
||||
|
Loading…
Reference in New Issue
Block a user