Search This Blog

Sunday 28 February 2016

Checksum error detecction

Note : lets say if we have sum of number in array is 36 then we convert into binary then if the number is >4 then we have to divide it into 4 digits and do a binary summation of that two parts so this code is only for who's binary number is less or equal then 8. then we have to do a complement of that number thus in case of 36 ans will be 1001 now this is binary of 9. so again if you add 9  into again that data then it will be 45 now again will do the same procedure so the answer will be 0. it means there is no error else error in data!

#include<iostream.h>
#include<conio.h>
#include<math.h>
int main()
{
int n,a[10],i,sum=0;
clrscr();
cout<<"Enter size of digits:";
cin>>n;

for(i=0;i<n;i++)
{
cin>>a[i];
sum = sum + a[i];
}
cout<<"sum of digit is:"<<sum;

n=sum;
int bin[10];
int r;
int p=0,j;
while(n!=0)
{
r=n%2;
n=n/2;
bin[p]=r;
p++;
}
int c=p-1;
cout<<"\nBinary is:";
for(j=c;j>=0;j--)
{
cout<<bin[j];
}
//parting two division of max 8 digits of that sum of digit
int x1[15],x2[15],q;

if(c==7)
{
for(j=c-4,q=0;j>=0;j--,q++)
{
x1[q]=bin[j];
}
for(j=c,q=0;j>=4;j--,q++)
{

x2[q]=bin[j];
}
}
else if(c==6)
{
for(j=c-3,q=0;j>=0;j--,q++)
{
x1[q]=bin[j];
}
x2[0]=0;
for(j=c,q=1;j>=4;j--,q++)
{

x2[q]=bin[j];
}
}
else if(c==5)
{
for(j=c-2,q=0;j>=0;j--,q++)
{
x1[q]=bin[j];
}
x2[0]=0;
x2[1]=0;
for(j=c,q=2;j>=4;j--,q++)
{

x2[q]=bin[j];
}
}
else if(c==4)
{
for(j=c-1,q=0;j>=0;j--,q++)
{
x1[q]=bin[j];
}
x2[0]=0;
x2[1]=0;
x2[2]=0;
for(j=c,q=3;j>=4;j--,q++)
{

x2[q]=bin[j];
}
}
else
{
//4 bits binary
for(j=c,q=0;j>=0;j--,q++)
{
x1[q]=bin[j];
}
x2[0]=0;
x2[1]=0;
x2[2]=0;
x2[3]=0;

}
int s=0;
for(q=0;q<4;q++)
{
      s=10*s+x1[q];
}

int t=0;
int mul=1;
for(q=0;q<4;q++)
{
      t=10*t+x2[q];
}

int deci1=0,deci2=0,rem,z=0;
while(t!=0)
{
rem=t%10;
t/=10;
deci1+=rem*pow(2,z);
z++;
}
z=0;
while(s!=0)
{
rem=s%10;
s/=10;
deci2+=rem*pow(2,z);
z++;
}
int deci = deci1+deci2;
int fibin[10];
z=0;
p=0;
while(deci!=0)
{
rem=deci%2;
deci/=2;
fibin[z]=rem;
z++;
}


if(z<4)
{
fibin[z]=0;
for(int k=z;k>=0;k--)
{
if(fibin[k]==0)
{
fibin[k]=1;
}
else
{
fibin[k]=0;
}
}
cout<<"\nfinal sum with complement is:"<<endl;
for(k=z;k>=0;k--)
{
cout<<fibin[k];
}

}
else
{
for(int k=z-1;k>=0;k--)
{
if(fibin[k]==0)
{
fibin[k]=1;
}
else
{
fibin[k]=0;
}
}
cout<<"\nfinal sum with complement is:"<<endl;
for(k=z-1;k>=0;k--)
{
cout<<fibin[k];
}
}

int ans=0;
if(z==3)
{
for(q=z;q>=0;q--)
{
      ans=10*ans+fibin[q];
}
}
else
{
for(q=z-1;q>=0;q--)
{
ans=10*ans+fibin[q];
}
}
int check=0;
q=0;
while(ans!=0)
{
rem=ans%10;
ans/=10;
check+=rem*pow(2,q);
q++;
}
cout<<"\nans of complement is "<<check;

getch();
return 0;
}


Enter n
1
enter data members
36

checksum is 9

No comments:

Post a Comment