Advertisement
rajath_pai

Frame Sorting

Mar 1st, 2021
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.94 KB | None | 0 0
  1. //Write a program for frame sorting technique used in buffers.
  2.  
  3. #include<bits/stdc++.h>
  4. using namespace std;
  5.  
  6. struct packet{
  7.     string str;
  8.     int num;
  9. };
  10.  
  11. int main(){
  12.     struct packet s1[100],s2[100],s3[100];
  13.     int cnt = 0;
  14.     cout << "Enter The message to be Transmitted :\n";
  15.     string s;
  16.     getline(cin,s);
  17.     cout << "\nEnter the size of the frame\n";
  18.     int frame;
  19.     cin >> frame;
  20.     for(int i = 0 ; i < s.size(); i+=frame){
  21.         string x;
  22.         for(int j = i ; j < i+frame; j++){
  23.             x+=s[j];
  24.         }
  25.         s1[cnt].str = x;
  26.         s1[cnt].num = cnt;
  27.         cnt++;
  28.         x.clear();
  29.     }
  30.     cout << "\nBefore shuffle\n";
  31.     for(int i = 0 ; i < cnt; i++){
  32.         cout << s1[i].num << " " << s1[i].str << endl;
  33.     }
  34.     int data[cnt];
  35.    
  36.     for(int i = 0; i < cnt; i++){
  37.         data[i] = i;
  38.     }
  39.    
  40.     random_shuffle(data,data+cnt);
  41.    
  42.     //s2 struct to carry shuffled values
  43.    
  44.     for(int i = 0 ; i < cnt; i++){
  45.         s2[data[i]].num = s1[i].num;
  46.         s2[data[i]].str = s1[i].str;
  47.     }
  48.    
  49.     cout << "\nafter shuffle\n";
  50.     //after shuffle
  51.     for(int i = 0 ; i < cnt; i++){
  52.         cout << s2[i].num << " " << s2[i].str << endl;
  53.     }
  54.     for(int i = 0 ; i < cnt; i++){
  55.         int x = INT_MAX,j=0,index = 0;
  56.         for(j = 0; j < cnt; j++){
  57.             if(s2[j].num < x && s2[j].num<INT_MAX){
  58.                 x = s2[j].num;
  59.                 index = j;
  60.             }
  61.         }
  62.         s3[i].num = x;
  63.         s3[i].str = s2[index].str;
  64.         s2[index].num = INT_MAX;
  65.     }
  66.     cout << "\nMessage received is :\n";
  67.     for(int i = 0 ; i < cnt; i++){
  68.         cout << s3[i].str;
  69.     }
  70.     cout << endl;
  71. }
  72.  
  73. /*
  74. OUTPUT
  75.  
  76.  
  77. Enter The message to be Transmitted :
  78. nmamit nitte
  79.  
  80. Enter the size of the frame
  81. 3
  82.  
  83. Before shuffle
  84. 0 nma
  85. 1 mit
  86. 2  ni
  87. 3 tte
  88.  
  89. after shuffle
  90. 0 nma
  91. 2  ni
  92. 3 tte
  93. 1 mit
  94.  
  95. Message received is :
  96. nmamit nitte
  97.  
  98.  
  99.  
  100.  
  101. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement