Uploaded by widak72570

Cyclic redandant check -sender reciver

advertisement
Experiment -3
Program-1
Implementation of CRC Error Detection Mechanism
#include<iostream>
using namespace std;
int Binary_divison(int dividend[],int g_data[],int divided_size,int divisor_size)
{
int rem[100],current_divisor[100];
int z;
for(int i=0; i<divided_size; i ++)
{
if(dividend[i]==1)
{
for( z=0; z<divisor_size; z++)
{
current_divisor[z]=g_data[z];
}
}
else
{
for( z=0; z<divisor_size; z++)
{
current_divisor[z]=0;
}
}
int a=0;
for( int z=i,j=0; z<divisor_size+i; z++)
{
if(dividend[z]==current_divisor[j])
{
rem[a]=0;
j++;
}
else
{
rem[a]=1;
j++;
}
a++;
}
for( int z=0; z<divisor_size-1; z++)
{
rem[z]=rem[z+1];
dividend[z+i+1]=rem[z];
}
}
}
int CRC_sender(int data[],int g_data[], int appended_data[],int m,int n,int p )
{
int codeword[100];
Binary_divison(appended_data,g_data,m,n);
cout<<"\n\nThe CRC is : ";
for( int i=m; i<m+p; i++)
{
cout<<" "<<appended_data[i];
codeword[i]=appended_data[i];
}
cout<<"\n\nThe Code Word is : ";
for( int i=0; i<m; i++)
{
codeword[i]=data[i];
}
for( int i=0; i<m+p; i++)
{
cout<<" "<<codeword[i];
}
}
int CRC_reciever(int data[],int g_data[], int appended_data[],int m,int n,int p )
{
Binary_divison(appended_data,g_data,m,n);
bool error;
for(int i=m; i<m+p; i++)
{
if(appended_data[i]==0)
{
error=false;
}
else
{
error=true;
break;
}
}
cout<<"\n\nThe remainder is : ";
for( int i=m; i<m+p; i++)
{
cout<<" "<<appended_data[i];
}
if(!error)
{
cout<<"\n\nNo Error detected ";
cout<<"\n\nThe Actual data is : ";
for( int i=0; i<m-p; i++)
{
cout<<" "<<data[i];
}
}
else
{
cout<<"\n\nError detected ";
cout<<"\n\nPlease re-transmit the data ";
}
}
int Display(int data[],int appended_data[],int g_data[],int m,int n,int p)
{
cout<<"\n\nThe Actual Data is : ";
for( int i=0; i<m; i++)
{
cout<<" "<<data[i];
}
cout<<"\n\nThe Appended data is : ";
for( int i=0; i<m+p; i++)
{
cout<<" "<<appended_data[i];
}
cout<<"\n\nThe G(x) is : ";
for( int i=0; i<n; i++)
{
cout<<" "<<g_data[i];
}
}
int main()
{
int data[100],g_data[100],appended_data[100],m,n,p;
int ch;
do
{
cout<<"\n CRC ALGORITHM";
cout<<"\n 1- Sender Side Algorithm";
cout<<"\n 2- Receiver Side Algorithm";
cout<<"\n 3- Exit";
cout<<"\n Enter Choice:";
cin>>ch;
switch(ch)
{
case 1:
cout<<"\nEnter the size of data : ";
cin>>m;
cout<<"Enter the data : ";
for( int i=0; i<m; i++)
{
cin>>data[i];
appended_data[i]=data[i];
}
cout<<"\nEnter the size of g(x) : ";
cin>>n;
cout<<"Enter the data of g(x): ";
for( int i=0; i<n; i++)
{
cin>>g_data[i];
}
p=n-1;
for(int i=m; i<m+p; i++)
{
appended_data[i]=0;
}
Display(data,appended_data,g_data,m,n,p);
CRC_sender(data,g_data,appended_data,m,n,p);
break;
case 2:
cout<<"\nEnter the size of bit string received : ";
cin>>m;
cout<<"Enter the data of bit string : ";
for( int i=0; i<m; i++)
{
cin>>data[i];
appended_data[i]=data[i];
}
cout<<"\nEnter the size of g(x) : ";
cin>>n;
cout<<"Enter the data of g(x): ";
for( int i=0; i<n; i++)
{
cin>>g_data[i];
}
p=n-1;
for(int i=m; i<m+p; i++)
{
appended_data[i]=0;
}
Display(data,appended_data,g_data,m,n,p);
CRC_reciever(data,g_data,appended_data,m,n,p);
break;
case 3:
break;
default:
cout<<"Invalid Input";
}
}
while(true);
}
Output
Sender Side Algorithm
Receiver Side Algorithm
With Error
Without Error
Download