forked from Akanksha1212/C_Programs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
crc code.cpp
66 lines (65 loc) · 1.52 KB
/
crc code.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#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<<"hii";
cout<<"Enter the message1 : ";
cout<<"Enter the message2: ";
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;
}