Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
119 views

Write A Program To Implement Data Link Layer Farming Method Checksum

program

Uploaded by

ksai.mb
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
119 views

Write A Program To Implement Data Link Layer Farming Method Checksum

program

Uploaded by

ksai.mb
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Write a Program to implement data link layer farming method checksum

Checksum Program in C: Checksum is an error detection mechanism


that detects the error in the data/message in a data communication from
sender to receiver. It is only an error detection mechanism and hence
cannot correct the errors in the message bits.
It uses Checksum generator on the sender side and Checksum
checker on the receiver side to check the errors.

Example:
Number of messages transmitted: 2
Elements to calculate Checksum:
1011
1110
****SENDER SIDE****
SUM IS: 2121
CHECKSUM IS: 2122

****RECEIVER SIDE****
SUM IS: 2121
CHECKSUM IS: 0

Problem Description
Write a C Program to to implement checksum.

Problem Solution
1. Ask the user for number of messages transmitted say N.
2. Ask for N messages from the user.
3. Calculate the SUM and CHECKSUM at Sender Side.
4. Calculate the SUM and CHECKSUM at Receiver Side.
5. Exit.

#include<stdio.h>
#include<math.h>

int sender(int arr[10],int n)


{
int checksum,sum=0,i;
printf("\n****SENDER SIDE****\n");
for(i=0;i<n;i++)
sum+=arr[i];
1
printf("SUM IS: %d",sum);
checksum=~sum; //1's complement of sum
printf("\nCHECKSUM IS:%d",checksum);
return checksum;
}

void receiver(int arr[10],int n,int sch)


{
int checksum,sum=0,i;
printf("\n\n****RECEIVER SIDE****\n");
for(i=0;i<n;i++)
sum+=arr[i];
printf("SUM IS:%d",sum);
sum=sum+sch;
checksum=~sum; //1's complement of sum
printf("\nCHECKSUM IS:%d",checksum);
}

void main()
{
int n,sch,rch;
printf("\nENTER SIZE OF THE STRING:");
scanf("%d",&n);
int arr[n];
printf("ENTER THE ELEMENTS OF THE ARRAY TO CALCULATE CHECKSUM:\n");
for(int i=0;i<n;i++)
{
scanf("%d",&arr[i]);
}
sch=sender(arr,n);
receiver(arr,n,sch);
}

OUTPUT
ENTER SIZE OF THE STRING:2
ENTER THE ELEMENTS OF THE ARRAY TO CALCULATE CHECKSUM:
1010
1011

****SENDER SIDE****
SUM IS: 2021

2
CHECKSUM IS:-2022

****RECEIVER SIDE****
SUM IS:2021
CHECKSUM IS:0
ENTER SIZE OF THE STRING:4
ENTER THE ELEMENTS OF THE ARRAY TO CALCULATE CHECKSUM:
1010
1011
1012
1013

****SENDER SIDE****
SUM IS: 4046
CHECKSUM IS:-4047

****RECEIVER SIDE****
SUM IS:4046
CHECKSUM IS:0

Program Explanation
1. Ask the user to enter size of the string n.
2. Initialize an array of size n arr[n].
3. Enter n elements into the array to calculate Checksum.
4. To calculate Checksum at sender side pass the array and number of elements to a
function sender() and store the value returned by the function into a variable sch.
5. Inside the sender function run a loop from i=0 to n and in each iteration calculate sum
as sum = sum + arr[i]. It will result in addition of all the elements in the array.
6. Now, calculate checksum as 1’s complement of the variable sum and print
both sum and checksum for the sender side.
7. Return the value of Checksum and store it in a variable sch.
8. To calculate Checksum at receiver side pass the array, number of elements and the
also the sch to a function receiver().
9. Inside the receiver function run a loop from i=0 to n and in each iteration calculate
sum as sum = sum + arr[i]. It will result in addition of all the elements in the array.
10. Now, add the value of sum with the value of sch and store it in the variable sum. It
is done in order to decode the checksum generated by the sender.
11. calculate checksum as 1’s complement of the variable sum and print
both sum and checksum for the receiver side.

Example:
 Consider, the size of string is n=2 and the two elements are 1011 and 0111.
These two values will be stored in arr[] and will be passed to sender function.

3
 Inside the sender function run a loop and calculate the sum of both the elements
i.e., sum = 1122. Take the 1’s of sum to calculate checksum i.e., checksum = -
1123.
 Now, print sum and checksum at the sender side and return the value of
checksum and store it in the variable sch.
 At the receiver function pass the arr[], n, sch. Inside the function run a loop and
calculate the sum of both the elements then come out of the loop and print sum
at sender side i.e., sum = 1122.
 Now add the value of sch to the value of sum and finally store it in the same
variable sum to decode the checksum generated at the sender side.
 Take the 1’s complement of sum and store it in the
variable checksum i.e., checksum = 0. Now, print the value of checksum for
the receiver side

You might also like