Cyclic Redundancy Code

Cyclic Redundancy Check | C++ Program to implement Cyclic Redundancy Check

Cyclic Redundancy Check is an algorithm that is utilized to discover errors in data that has been transmitted. Since each record or message can be thought of as a solitary example of bits it tends to be treated as a solitary expansive binary number. The CRC system makes utilization of a polynomial divisor of length n+1 to partition the source number – disposing of the quotient and utilizing the remainder as the check whole for that message.

C++ Program to Implement Cyclic Redundancy Code
#include<iostream>
using namespace std;
void division(int temp[],int gen[],int n,int r)
{
for(int i=0;i<n;i++)
{
    if (gen[0]==temp[i])
    {
        for(int j=0,k=i;j<r+1;j++,k++)
            if(!(temp[k]^gen[j]))
                temp[k]=0;
            else
                temp[k]=1;
    } }}
int main()
{int n,r,message[50],gen[50],temp[50];
cout<<"At Sender's End "<<endl;
cout<<"Enter the number of message bits : ";
cin>>n;
cout<<"Enter the number of generator bits : ";
cin>>r;
cout<<"Enter the message : ";
for(int i=0;i<n;i++)
    cin>>message[i];
cout<<"Enter the generator : ";
for(int i=0;i<r;i++)
    cin>>gen[i];
r--;
for(int i=0;i<r;i++)
    message[n+i] = 0;
for(int i=0;i<n+r;i++)
    temp[i] = message[i];
division(temp,gen,n,r);
cout<<"CRC : ";
for(int i=0;i<r;i++)
{
    cout<<temp[n+i]<<" ";
    message[n+i] = temp[n+i];
}
cout<<endl<<"Transmitted Message : ";
for(int i=0;i<n+r;i++)
    cout<<message[i]<<" ";
cout<<endl<<endl<<"At Receiver's End "<<endl;
cout<<"Enter the received message : ";
for(int i=0;i<n+r;i++)
    cin>>message[i];
for(int i=0;i<n+r;i++)
    temp[i] = message[i];
division(temp,gen,n,r);
for(int i=0;i<r;i++)
{
    if(temp[n+i])
    {
        cout<<"Error detected in received message.";
        return 0;
    } }
cout<<"No error in received Message.\nReceived Message : ";
for(int i=0;i<n;i++)
    cout<<message[i]<<" ";
return 0;
}
OUTPUT 1:
At Sender's End
Enter the number of message bits : 10
Enter the number of generator bits : 5
Enter the message : 1 1 0 1 0 1 1 1 1 1
Enter the generator : 1 0 0 1 1
CRC : 0 0 1 0
Transmitted Message : 1 1 0 1 0 1 1 1 1 1  0 0 1 0

At Receiver's End

Enter the received message : 1 1 0 1 0 1 1 1 1 1 0 0 1 0
No error in received Message.
Received Message : 1 1 0 1 0 1 1 1 1 1

OUTPUT 2:

At Sender's End
Enter the number of message bits : 10
Enter the number of generator bits : 5
Enter the message : 1 1 0 1 0 1 1 1 1 1
Enter the generator : 1 0 0 1 1
CRC : 0 0 1 0
Transmitted Message : 1 1 0 1 0 1 1 1 1 1  0 0 1 0

At Receiver's End

Enter the received message : 1 1 0 1 0 1 1 1 1 0 0 0 1 0
No error in received Message.
Error detected in received message.


tags : C++,C++ programs,programs of C++,C++ tutorials,Learn C++,CRC,C++ tutorialspoint.computer networks,computer networks programs, programming languages

1 Comments

Previous Post Next Post