0% found this document useful (0 votes)
47 views

Computer Network LAB 2

The document describes implementing error detection and correction techniques in computer networks using a C program. It includes the code to generate a message word, add redundant bits to create a code word, and then detect and correct errors in the received code word using syndrome decoding.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
47 views

Computer Network LAB 2

The document describes implementing error detection and correction techniques in computer networks using a C program. It includes the code to generate a message word, add redundant bits to create a code word, and then detect and correct errors in the received code word using syndrome decoding.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Computer Network LAB

JAMIA HAMDARD
Computer Science and Engineering

Submitted To :

Ms. Gautami Tripathi


Assistant Professor
Dept. of SEST

Submitted By:

Md Zeeshan Ahmed
Enrolment no. - 2020-310-117
Section: B 6th Semester
Dept. of SEST, Jamia Hamdard
Lab 2
To implement and check the error detection/error correction techniques in networks using
a c program.

Code
#include<stdio.h>
#include<stdlib.h>
int main(){

freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
int n;
int redundentbits;
scanf("%d",&n);
scanf("%d",&redundentbits);
int *MessageWord = (int*)malloc(n*sizeof(int));
int *CodeWord = (int*)malloc((n+redundentbits)*sizeof(int));
printf("Message Word : ");
for(int i=0;i<n;i++){
scanf("%d",(MessageWord+i));
CodeWord[i] = MessageWord[i];
printf("%d\t",MessageWord[i]);
}

int *divisor = (int*)malloc((redundentbits+1)*sizeof(int));


printf("\n");
printf("Divisor : ");
for(int i=0;i<=redundentbits;i++){
scanf("%d",(divisor+i));
printf("%d\t",divisor[i]);
CodeWord[n+i] = 0;
}
int *remainder = (int*)malloc((redundentbits+1)*sizeof(int));
int *temp = (int*)malloc((redundentbits+1)*sizeof(int));
int *curr = (int*)malloc((redundentbits+1)*sizeof(int));
printf("\nCodeWord : ");
for(int i=0;i<n+redundentbits;i++){
printf("%d\t",CodeWord[i]);
}
int i = 0;
for(i=0;i<=redundentbits;i++){
curr[i] = CodeWord[i];
}
int val = 0;
int size = -1;
int previous_index = -1;
while(i<redundentbits+n){
previous_index = i;
int j;
// printf("\n");
for(j=0;j<=redundentbits;j++){
temp[j] = curr[j]^divisor[j];
// printf("%d\t",temp[j]);
}
for(int ind=0;ind<=redundentbits;ind++){
remainder[ind] = temp[ind];
}
int idx = 0;
while(idx <= redundentbits){
if(temp[idx] == 1)
break;
idx++;
}
if(idx > redundentbits){
for(j=0;j<=redundentbits;j++){
if(j+i < n+redundentbits)
curr[j] = CodeWord[j+i];
else{
size = j;
val = 1;
break;
}
}
i = j+i;
}
else{
j=0;
while(idx <= redundentbits){
curr[j++] = temp[idx++];
}
while(j <= redundentbits){
if(i+1 < redundentbits+n)
curr[j++] = CodeWord[i++];
else{
size = j;
val = 1;
break;
}
}
if(val == 1)
break;
}
}
int remaining_bits = n+redundentbits - previous_index;
for(i=remaining_bits;i<=redundentbits;i++){
temp[i-remaining_bits] = remainder[i];
}
i = redundentbits - remaining_bits + 1;
while(i<=redundentbits){
temp[i++] = 0;
}
for(i=n;i<redundentbits+n;i++){
CodeWord[i] = temp[i-n+1];
}
printf("\nCodeWord : ");
for(int i=0;i<n+redundentbits;i++){
printf("%d\t",CodeWord[i]);
}
// /////////////////////////////////////////
// CodeWord[1] = 1;
for(i=0;i<=redundentbits;i++){
curr[i] = CodeWord[i];
}
val = 0;
size = -1;
previous_index = -1;
while(i<redundentbits+n){
previous_index = i;
int j;
// printf("\n");
for(j=0;j<=redundentbits;j++){
temp[j] = curr[j]^divisor[j];
// printf("%d\t",temp[j]);
}
for(int ind=0;ind<=redundentbits;ind++){
remainder[ind] = temp[ind];
}
int idx = 0;
while(idx <= redundentbits){
if(temp[idx] == 1)
break;
idx++;
}
if(idx > redundentbits){
for(j=0;j<=redundentbits;j++){
if(j+i < n+redundentbits)
curr[j] = CodeWord[j+i];
else{
size = j;
val = 1;
break;
}
}
i = j+i;
}
else{
j=0;
while(idx <= redundentbits){
curr[j++] = temp[idx++];
}
while(j <= redundentbits){
if(i+1 < redundentbits+n)
curr[j++] = CodeWord[i++];
else{
size = j;
val = 1;
break;
}
}
if(val == 1)
break;
}
}
val = 1;
// CodeWord[1] = 1;
printf("\n");
for(int k=0;k<=redundentbits;k++){
if(remainder[k] == 1){
printf("\nThere's an Error in the recieved Code\n");
val = 0;
break;
}
}
if(val)
printf("\nThere's no Error in the reieved Code\n");
free(MessageWord);
free(CodeWord);
free(divisor);
free(remainder);
free(temp);
free(curr);
return 0;
}

Output

You might also like