Write a program to implement the checksum method. The implementation has two par
ID: 3836320 • Letter: W
Question
Write a program to implement the checksum method. The implementation has two parts a. Calculating the checksum at the sender b. Verifying the checksum at the receiver Calculating the checksum at the sender Inputs: 4 data each consisting of 16-bit (binary) Output: Checksum of 16-bit (binary) Example http://mathforum.org/library/drmath/view/54379.html E or example, suppo se We have the to lowing data. separate the data into groups of 4 bits only for readability 1 000 011 0 0 1 01 11 10 1010 1100 011 0 0 000 01 11 0 0 0 1 0 0 1 0 1 010 1 000 0001 1011 0 1 0 1 First, we add the 16-bit values 2 at a time: First 16-bit value 1 0 0 0 0 1 10 0 1 0 1 1110 Second it valu 1 010 1 1 0 0 0 11 0 0 0 0 0 1 0011 0010 1011 1 110 Produced a carry-out, which gets added 1 back into 0 011 0 0 1 0 1 011 11 11 Third 16-bit value 01 11 0 0 0 1 0 0 1 0 1010 0 1 0 1 0 0 011 11 10 1001 No carry to swing around Fourth 16-bit value 1000 0001 1011 0101 Produced a carry-out, which gets added 1 0 0 1 0 0 1 0 1 1 0 0 1 1110 1 back into 0010 0101 1001 1111 Our "one's complement sumExplanation / Answer
#include<stdio.h>
#define max 10
int ans[4];
void checksum(int[max][max],int);
void main()
{
int a[max][max],i,j,m,bug=0;
clrscr();
printf("Enter no.of strings:");
scanf("%d",&m);
for(i=0;i<m;i++)
{
printf(" Enter string %d: ",i+1);
for(j=0;j<4;j++)
scanf("%d",&a[i][j]);
}
printf(" Given strings are: ");
for(i=0;i<m;i++)
{
for(j=0;j<4;j++)
printf("%d",a[i][j]);
printf(" ");
}
checksum(a,m);
for(i=0;i<4;i++)
{
if(ans[i]==0)
ans[i]=1;
else
ans[i]=0;
}
printf(" The computed checksum at sender side is:: ");
for(i=0;i<4;i++)
printf("%d",ans[i]);
printf(" Transmitting to reciever...");
printf(" Enter the recieved strings:: ");
for(i=0;i<=m;i++)
{
printf(" Enter string %d: ",i+1);
for(j=0;j<4;j++)
scanf("%d",&a[i][j]);
}
checksum(a,m);
for(j=0;j<4;j++)
if(a[m][j]==ans[j])
bug++;
bug==0 ? printf(" No transmission error") : printf(" Transmission error");
getch();
}
void checksum(int a[max][max],int m)
{
int i,j,count=0,carry=0;
for(j=3;j>=0;j--)
{
count=0;
for(i=0;i<m;i++)
{
if(a[i][j]==1)
count++;
}
count+=carry;
if(count==0)
{
ans[j]=0;
carry=0;
}
else if(count==1)
{
ans[j]=1;
carry=0;
}
else if(count%2==1)
{
ans[j]=1;
carry=1;
}
else
{
ans[j]=0;
carry=1;
}
}
while(carry==1)
{
for(j=3;j>=0;j--)
{
ans[j]+=carry;
if(ans[j]==2)
{
ans[j]=0;
carry=1;
}
else
carry=0;
}
}
}
OUTPUT:
Enter no.of strings:3
Enter string 1:
1
0
1
0
Enter string 2:
1
1
1
1
Enter string 3:
0
0
1
0
Given strings are:
1010
1111
0010
The computed checksum at sender side is:: 0011
Transmitting to reciever...
Enter the recieved strings::
Enter string 1:
1
0
1
0
Enter string 2:
1
1
1
1
Enter string 3:
0
0
1
0
Enter string 4:
0
0
1
1
No transmission error
OUTPUT 2:
Enter no.of strings:3
Enter string 1:
1
0
1
0
Enter string 2:
1
1
1
1
Enter string 3:
0
0
1
0
Given strings are:
1010
1111
0010
The computed checksum at sender side is:: 0011
Transmitting to reciever...
Enter the recieved strings::
Enter string 1:
1
0
1
0
Enter string 2:
1
1
1
1
Enter string 3:
0
0
1
0
Enter string 4:
1
1
0
0
Transmission error
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.