Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Write a program for frame sorting technique used in buffers.
- #include<bits/stdc++.h>
- using namespace std;
- struct packet{
- string str;
- int num;
- };
- int main(){
- struct packet s1[100],s2[100],s3[100];
- int cnt = 0;
- cout << "Enter The message to be Transmitted :\n";
- string s;
- getline(cin,s);
- cout << "\nEnter the size of the frame\n";
- int frame;
- cin >> frame;
- for(int i = 0 ; i < s.size(); i+=frame){
- string x;
- for(int j = i ; j < i+frame; j++){
- x+=s[j];
- }
- s1[cnt].str = x;
- s1[cnt].num = cnt;
- cnt++;
- x.clear();
- }
- cout << "\nBefore shuffle\n";
- for(int i = 0 ; i < cnt; i++){
- cout << s1[i].num << " " << s1[i].str << endl;
- }
- int data[cnt];
- for(int i = 0; i < cnt; i++){
- data[i] = i;
- }
- random_shuffle(data,data+cnt);
- //s2 struct to carry shuffled values
- for(int i = 0 ; i < cnt; i++){
- s2[data[i]].num = s1[i].num;
- s2[data[i]].str = s1[i].str;
- }
- cout << "\nafter shuffle\n";
- //after shuffle
- for(int i = 0 ; i < cnt; i++){
- cout << s2[i].num << " " << s2[i].str << endl;
- }
- for(int i = 0 ; i < cnt; i++){
- int x = INT_MAX,j=0,index = 0;
- for(j = 0; j < cnt; j++){
- if(s2[j].num < x && s2[j].num<INT_MAX){
- x = s2[j].num;
- index = j;
- }
- }
- s3[i].num = x;
- s3[i].str = s2[index].str;
- s2[index].num = INT_MAX;
- }
- cout << "\nMessage received is :\n";
- for(int i = 0 ; i < cnt; i++){
- cout << s3[i].str;
- }
- cout << endl;
- }
- /*
- OUTPUT
- Enter The message to be Transmitted :
- nmamit nitte
- Enter the size of the frame
- 3
- Before shuffle
- 0 nma
- 1 mit
- 2 ni
- 3 tte
- after shuffle
- 0 nma
- 2 ni
- 3 tte
- 1 mit
- Message received is :
- nmamit nitte
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement