You are on page 1of 20

CSE 1004: Network and

Communication
(L23+L24)
ASSESSMENT – 2

By: Ansh Sharma


19BCE0752

List of lab experiments:

1) CRC – 8 bit, CHECKSUM – 16 bit (04-03-


2021)

2) Hamming Code (18-03-2021)


CSE 1004: Network and
Communication (L23+L24)
CRC – 8 bit, CHECKSUM – 16 bit

NAME: ANSH SHARMA


REG NO:19BCE0752
04-03-2021

1) Cyclic Redundancy Check C++ program to detect


error from entered code.
Ans)

C++ program:

#include <iostream>
using namespace std;

void division(int temp_array[], int generator[], int num, int r)


{
for (int i = 0; i < num; i++) {
if (generator[0] == temp_array[i]) {
for (int j = 0, k = i; j < r + 1; j++, k++)
if (!(temp_array[k] ^ generator[j]))
temp_array[k] = 0;
else
temp_array[k] = 1;
}
}
}

int main()
{
int num, r, message_array[50], generator[50],
temp_array[50];

cout << "At Sender's End " << endl;

num=8;

cout << "Enter the number of generator bits : ";


cin >> r;
cout << "Enter the message_array : ";

for (int i = 0; i < num; i++)


cin >> message_array[i];

cout << "Enter the generator : ";

for (int i = 0; i < r; i++)


cin >> generator[i];

r--;

for (int i = 0; i < r; i++)


message_array[num + i] = 0;

for (int i = 0; i < num + r; i++)


temp_array[i] = message_array[i];

division(temp_array, generator, num, r);


cout << "CRC : ";

for (int i = 0; i < r; i++) {


cout << temp_array[num + i] << " ";
message_array[num + i] = temp_array[num + i];
}

cout <<"\nTransmitted message_array : ";


for (int i = 0; i < num + r; i++)
cout << message_array[i] << " ";

cout <<"\num\nAt Receiver's End " << endl;

cout << "Enter the received message_array : ";

for (int i = 0; i < num + r; i++)


cin >> message_array[i];

for (int i = 0; i < num + r; i++)


temp_array[i] = message_array[i];

division(temp_array, generator, num, r);

for (int i = 0; i < r; i++) {


if (temp_array[num + i]) {
cout << "Error detected in received message_array.";
return 0;
}
}

cout << "No error in received message_array.\nReceived


message_array : ";

for (int i = 0; i < num; i++)


cout << message_array[i] << " ";

return 0;
}
Output:
2) Checksum C++ program to print sum and
checksum of two binary strings.

Ans)
C++ program :

#include<iostream>
#include<cstring>

using namespace std;

int main()
{
int num=16;

char str1[num],str2[num];
char sum[num],complement[num];
int i;

cout<<"Enter first binary string: \n";


cin>>str1;

cout<<"Enter second binary string: \n";


cin>>str2;

if(strlen(str1)==strlen(str2))
{
char carry='0';
int length=strlen(str1);

for(i=length-1;i>=0;i--)
{
if(str1[i]=='0' && str2[i]=='0' && carry=='0')
{
sum[i]='0';
carry='0';
}
else if(str1[i]=='0' && str2[i]=='0' && carry=='1')
{
sum[i]='1';
carry='0';

}
else if(str1[i]=='0' && str2[i]=='1' && carry=='0')
{
sum[i]='1';
carry='0';

}
else if(str1[i]=='0' && str2[i]=='1' && carry=='1')
{
sum[i]='0';
carry='1';

}
else if(str1[i]=='1' && str2[i]=='0' && carry=='0')
{
sum[i]='1';
carry='0';
}
else if(str1[i]=='1' && str2[i]=='0' && carry=='1')
{
sum[i]='0';
carry='1';

}
else if(str1[i]=='1' && str2[i]=='1' && carry=='0')
{
sum[i]='0';
carry='1';

}
else if(str1[i]=='1' && str2[i]=='1' && carry=='1')
{
sum[i]='1';
carry='1';

}
else
break;
}

cout<<"\nSum = "<<carry<<sum;

for(i=0;i<length;i++)
{
if(sum[i]=='0')
complement[i]='1';
else
complement[i]='0';
}

if(carry=='1')
carry='0';
else
carry='1';

cout<<"\nChecksum = "<<carry<<complement;
}
else
cout<<"\nWrong input strings\n";

return 0;
}

Output:
CSE 1004: Network and
Communication (L23+L24)
Hamming Code

NAME: ANSH SHARMA


REG NO:19BCE0752
18-03-2021

1) Write a program to implement hamming code


using the following instructions:

Instructions
Input: Read the dataword at sender side, Read the
code word at receiver side.
Output: No. of redundant bits, codeword at
sender side. Error or no error, if it is error,
Corrupted bit position at receiver side.

Ans)

Program:

#include <iostream>
#include <cstdlib>
#include <math.h>
#include <string.h>
using namespace std;
int main(int argc, char** argv)
{
int data_bits[32], m, r = 0, parity; //m = no. of data
bits, r = no. of redundant bits

cout << "\nSender's side: \n";


cout << "Enter the size of data bits: ";
cin >> m;
//finding no. of redundant bits
while (pow(2, r) < m + r + 1) {
r++;
}
cout << "Enter the dataword : \n";
for (int i = 1; i <= m; i++)
cin >> data_bits[i];
int hamming[m + r], j = 0, k = 1;
//finding positions of redundant bits.
for (int i = 1; i <= m + r; i++) {
if (i == pow(2, j)) {
hamming[i] = -1; //-1 is initial value of redundant
bits
j++;
}
else {
hamming[i] = data_bits[k];
k++;
}
}
k = 0;
int x, min, max = 0;
//finding parity bit
for (int i = 1; i <= m + r; i = pow(2, k)) {
k++;
parity = 0;
j = i;
x = i;
min = 1;
max = i;
while (j <= m + r) {
for (x = j; max >= min && x <= m + r; min++, x++) {
if (hamming[x] == 1)
parity = parity + 1;
;
}
j = x + i;
min = 1;
}
//checking for even parity
if (parity % 2 == 0) {
hamming[i] = 0;
}
else {
hamming[i] = 1;
}
}
cout << "\nNo. of redundant bits : " << r << endl;
cout << "\nHamming code is: ";
for (int i = 1; i <= m + r; i++)
cout << hamming[i] << " ";
int len = sizeof(hamming) / sizeof(hamming[0]);
int rn = 0;

cout << "\nReciever's side: \n";


cout << "\n Enter the no of recieved bits: ";
cin >> rn;
int rec[rn], pos, flag = 1;

cout << "\n Enter the codeword: \n";


for (int i = 1; i <= rn; i++) {
cin >> rec[i];
}
for (int i = 1; i <= rn; i++) {
if (rec[i] != hamming[i]) {
flag = 0;
pos = i;
}
}
if (flag == 1) {
cout << "\n No error in transition\n";
}
else if (flag == 0) {
cout << "\n Error in transition \n";
cout << "\n The error is at position: " << pos <<
endl;
cout << "\n Corrupted bit position at reciever's side:
" << pos << endl;
}
return 0;
}
Output:

You might also like