Advertisement
Eternoseeker

CRC code Aditya

Mar 20th, 2023 (edited)
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.03 KB | Source Code | 0 0
  1. #include<iostream>
  2. using namespace std;
  3.  
  4. void show_array(int a[], int n){
  5.     for(int i = 0; i < n; i++){
  6.         cout << a[i] << " ";
  7.     }
  8.     cout << endl;
  9. }
  10.  
  11. void division(int temp[], int gen[], int n, int r){
  12.     for(int i = 0; i < n; i++){
  13.         if(gen[0] == temp[i]){
  14.             for(int j = 0, k = i; j < r + 1; j++, k++){
  15.                 if(!(temp[k] ^ gen[j]))
  16.                     temp[k] = 0;
  17.                 else
  18.                     temp[k] = 1;
  19.             }
  20.         }
  21.     }
  22. }
  23.  
  24. int main(){
  25.     int n;
  26.     cout << "Enter the size of data: " << endl;
  27.     cin >> n;
  28.     int dataword[20];
  29.     int temp[20];
  30.     cout << "Enter the data: " << endl;
  31.     for(int i = 0; i < n; i++){
  32.         cin >> dataword[i];
  33.         temp[i] = dataword[i];
  34.     }
  35.     int generator[20];
  36.     cout << "Enter the generator: " << endl;
  37.     for(int i = 0; i < n; i++){
  38.         cin >> generator[i];
  39.     }
  40.     cout << "Enter the size of codeword: " << endl;
  41.     int code_size;
  42.     cin >> code_size;
  43.     for(int i = n; i < code_size; i++){
  44.         temp[i] = 0;
  45.     }
  46.     cout << "Sender side: " << endl;
  47.     cout << "The given dataword is: " << endl;
  48.     show_array(dataword, n);
  49.     cout << "The codeword is: " << endl;
  50.     show_array(temp, code_size);
  51.     division(temp, generator, n, (code_size - n));
  52.     cout << "Syndrome is: " << endl;
  53.     for(int i = n; i < code_size; i++){
  54.         cout << temp[i] << " ";
  55.     }
  56.     cout << endl;
  57.     int codeword[code_size];
  58.     for(int i = 0; i < n; i++){
  59.         codeword[i] = dataword[i];
  60.     }
  61.     for(int i = n; i < code_size; i++){
  62.         codeword[i] = temp[i];
  63.     }
  64.     cout << "The codeword transmitted is: " << endl;
  65.     show_array(codeword, code_size);
  66.  
  67.     cout << "\nReciever side: " << endl;
  68.     cout << "Enter the codeword recieved: " << endl;
  69.     int message[code_size];
  70.     for(int i = 0; i < code_size; i++){
  71.         cin >> message[i];
  72.         temp[i] = message[i];
  73.     }
  74.     division(temp, generator, n, code_size - n);
  75.     for(int i = n; i < code_size; i++){
  76.         if(temp[i]){
  77.             cout << "Error detected in message" << endl;
  78.             return 0;
  79.         }
  80.     }
  81.     cout << "No error detected in message" << endl;
  82.     cout << "Recieved message is: " << endl;
  83.     for(int i = 0; i < n; i++){
  84.         cout << message[i] << " ";
  85.     }
  86.     cout << endl;
  87.     return 0;
  88. }
  89.  
Tags: CN
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement