Networks : Program for Checksum

Home  >>  Networks  >>  Networks : Program for Checksum

Networks : Program for Checksum

4
May,2016

0

Program for Checksum.

#include<iostream>
#include<conio.h>
using namespace std;

int main(){
int n,er3,deci,deci2;
cout<<“————————–Sender side—————————“;
cout<<“\nenter the number of values: “;
cin>>n;
int sum=0,arr[n],temp[n],wrapped[4],checksum[4];
int sum2=0,temp2[n],wrapped2[4],checksum2[4];

for(int i=0;i<n;i++)
{
cin>>arr[i];
}

for(int i =0;i<n;i++)
{
sum=sum+arr[i];
}

cout<<“\nsum is : “<<sum;

for(int i=0;i<=n;i++)
{
temp[i]=sum%2;
sum=sum/2;
}
cout<<“\nBinary equilent: “;
for(int i=n;i>=0;i–)
{
cout<<temp[i];
}
cout<<“\n”<<temp[3]<<temp[2]<<temp[1]<<temp[0]<<” + “<<temp[5]<<temp[4]<<” = “;
wrapped[0]=temp[3];
wrapped[1]=temp[2];
wrapped[2]=temp[1]+temp[5];
wrapped[3]=temp[0]+temp[4];

for(int i=0;i<4;i++)
{
cout<<wrapped[i]; //cout<<temp[3]<<temp[2]<<temp[1]+temp[5]<<temp[0]+temp[4];
}
for(int i=0;i<4;i++)
{

if(wrapped[i]!=0)
checksum[i]=0;

else
checksum[i]=1;

}

cout<<“\nChecksum value is: “;

for(int i=0;i<4;i++)
{
cout<<checksum[i];
}
for(int i=0;i<4;i++)
{
deci=(checksum[0]*1)+(checksum[1]*2)+(checksum[2]*4)+(checksum[3]*8);
}

cout<<“\nThe checksum decimal equilent is: “<<deci;

cout<<“\n\n\n\n———————–Receiver side—————————“;

cout<<“\nThe values send by sender: “<<deci;
for(int i=0;i<n;i++)
{
cout<<“,”<<arr[i];
}

sum2=deci;

for(int i=0;i<n;i++)
{
sum2=sum2+arr[i];
}

cout<<“\nThe sum at reciever side: “<<sum2;
for(int i=0;i<=n;i++)
{
temp2[i]=sum2%2;
sum2=sum2/2;
}
cout<<“\nBinary equilent: “;
for(int i=n;i>=0;i–)
{
cout<<temp2[i];
}
cout<<“\n”<<temp2[3]<<temp2[2]<<temp2[1]<<temp2[0]<<” + “<<temp2[5]<<temp2[4]<<” = “;
wrapped2[0]=temp2[3];
wrapped2[1]=temp2[2];
wrapped2[2]=temp2[1]+temp2[5];
wrapped2[3]=temp2[0]+temp2[4];

for(int i=0;i<4;i++)
{
cout<<wrapped2[i]; //cout<<temp[3]<<temp[2]<<temp[1]+temp[5]<<temp[0]+temp[4];
}
for(int i=0;i<4;i++)
{

if(wrapped2[i]!=0)
checksum2[i]=0;

else
checksum2[i]=1;

}

cout<<“\nChecksum value is: “;

for(int i=0;i<4;i++)
{
cout<<checksum2[i];
}
for(int i=0;i<4;i++)
{
deci2=(checksum2[0]*1)+(checksum2[1]*2)+(checksum2[2]*4)+(checksum2[3]*8);
}

cout<<“\nThe checksum decimal equilent is: “<<deci2;

cout<<“\n———————Try fot Error———————–“;

cout<<“\nThe values send by sender: “;
for(int i=0;i<n;i++)
{
cout<<arr[i]<<“,”;
}
cout<<“\nEnter the checksum value: “;
cin>>er3;
sum2=er3;

for(int i=0;i<n;i++)
{
sum2=sum2+arr[i];
}

cout<<“\nThe sum at reciever side: “<<sum2;
for(int i=0;i<=n;i++)
{
temp2[i]=sum2%2;
sum2=sum2/2;
}
cout<<“\nBinary equilent: “;
for(int i=n;i>=0;i–)
{
cout<<temp2[i];
}
cout<<“\n”<<temp2[3]<<temp2[2]<<temp2[1]<<temp2[0]<<” + “<<temp2[5]<<temp2[4]<<” = “;
wrapped2[0]=temp2[3];
wrapped2[1]=temp2[2];
wrapped2[2]=temp2[1]+temp2[5];
wrapped2[3]=temp2[0]+temp2[4];

for(int i=0;i<4;i++)
{
cout<<wrapped2[i]; //cout<<temp[3]<<temp[2]<<temp[1]+temp[5]<<temp[0]+temp[4];
}
for(int i=0;i<4;i++)
{

if(wrapped2[i]!=0)
checksum2[i]=0;

else
checksum2[i]=1;

}

cout<<“\nChecksum value is: “;

for(int i=0;i<4;i++)
{
cout<<checksum2[i];
}
for(int i=0;i<4;i++)
{
deci2=(checksum2[0]*1)+(checksum2[1]*2)+(checksum2[2]*4)+(checksum2[3]*8);
}

cout<<“\nThe checksum decimal equilent is: “<<deci2;
getch();

}

 

Leave a Reply

Your email address will not be published. Required fields are marked *