CSE 307: Checksum

Friends,

Here is the code for the checksum.

#include<conio.h>
#include<stdio.h>
int main()
{
int n,i;
printf(“enter the no of elements in multiple of 4\n”);
scanf(“%d”,&n);
int a[n];
for(i=0;i<n;i++)
scanf(“%d”,&a[i]);
int sum[4];
if(n==8)
{
sum[0]=a[0]+a[4];
sum[1]=a[1]+a[5];
sum[2]=a[2]+a[6];
sum[4]=a[3]+a[7];
int check[4];
if(sum[0]%2==0)
check[0]=1;
else
check[0]=0;


if(sum[1]%2==0)
check[1]=1;
else
check[1]=0;
if(sum[2]%2==0)
check[2]=1;
else
check[2]=0;
if(sum[3]%2==0)
check[3]=1;
else
check[3]=0;
printf(“so now the transmitted datais \n”);
for(i=0;i<4;i++)
printf(“%d”,check[i]);
for(i=0;i<8;i++)
printf(“%d”,a[i]);
int rec[12];
printf(“enter the recieved data\n”);
for(i=0;i<12;i++)
scanf(“%d”,&rec[i]);
int sum1[4];
sum1[0]=rec[0]+rec[4]+rec[8];
sum1[1]=rec[1]+rec[5]+rec[9];
sum1[2]=rec[2]+rec[6]+rec[10];
sum1[3]=rec[3]+rec[7]+rec[11];
int check1[4];
if(sum1[0]%2==0)
check1[0]=0;
else
check1[0]=1;
if(sum1[1]%2==0)
check1[1]=0;
else
check1[1]=1;
if(sum1[2]%2==0)
check1[2]=0;
else
check1[2]=1;
if(sum1[3]%2==0)
check1[3]=0;
else
check1[3]=1;
for(i=0;i<4;i++)
printf( “%d”,check1[i]);
if(check1[0]==1 && check1[1]==1 && check1[2]==1 && check1[3]==1 )
{
printf(“no error”);
}
else
printf(“error”);
}

if(n==12)
{
sum[0]=a[0]+a[4]+a[8];
sum[1]=a[1]+a[5]+a[9];
sum[2]=a[2]+a[6]+a[10];
sum[4]=a[3]+a[7]+a[11];
int check[4];
if(sum[0]%2==0)
check[0]=1;
else
check[0]=0;
if(sum[1]%2==0)
check[1]=1;
else
check[1]=0;
if(sum[2]%2==0)
check[2]=1;
else
check[2]=0;
if(sum[3]%2==0)
check[3]=1;
else
check[3]=0;
printf(“so now the transmitted datais \n”);
for(i=0;i<4;i++)
printf(“%d”,check[i]);
for(i=0;i<12;i++)
printf(“%d”,a[i]);
int rec[16];
printf(“enter the recieved data\n”);
for(i=0;i<16;i++)
scanf(“%d”,&rec[i]);
int sum1[4];
sum1[0]=rec[0]+rec[4]+rec[8]+rec[12];
sum1[1]=rec[1]+rec[5]+rec[9]+rec[13];
sum1[2]=rec[2]+rec[6]+rec[10]+rec[14];
sum1[3]=rec[3]+rec[7]+rec[11]+rec[15];
int check1[4];
if(sum1[0]%2==0)
check1[0]=0;
else
check1[0]=1;
if(sum1[1]%2==0)
check1[1]=0;
else
check1[1]=1;
if(sum1[2]%2==0)
check1[2]=0;
else
check1[2]=1;
if(sum1[3]%2==0)
check1[3]=0;
else
check1[3]=1;
for(i=0;i<4;i++)
printf( “%d”,check1[i]);
if(check1[0]==1 && check1[1]==1 && check1[2]==1 && check1[3]==1 )
{
printf(“no error”);
}
else
printf(“error”);
}
getch();
return 0;
}

Thank you

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s