Advertisement
Guest User

priority.cpp

a guest
Jul 18th, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.68 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. bool cmp(pair<int,pair<int,int> > a, pair<int,pair<int,int> >b)
  5. {
  6.     return a.second.second>b.second.second;
  7. }
  8. int main()
  9. {
  10.     int no_of_proc;
  11.     cout<<"Enter number of process: ";
  12.     cin>>no_of_proc;
  13.     vector< pair<int,pair<int,int> >  > proc;
  14.  
  15.     cout<<"Enter process ID and burst time of process as follows: "<<endl;
  16.     for(int i=0; i<no_of_proc; i++)
  17.     {
  18.         int bur,prio,id;
  19.         cout<<"Process ID : " ;
  20.         cin>>id;
  21.         cout<<"Burst Time : " ;
  22.         cin>>bur;
  23.         cout<<"Priority : " ;
  24.         cin>>prio;
  25.         cout<<endl;
  26.         proc.push_back({id,{bur,prio}});
  27.     }
  28.  
  29.     sort(proc.begin(),proc.end(),cmp);
  30.  
  31.     // for(int i=0; i<proc.size(); i++)cout<<proc[i].first<<" "<<proc[i].second.first<<" "<<proc[i].second.second<<endl;
  32.  
  33.     int tat[100];
  34.     tat[0]=proc[0].second.first;
  35.     double sum1=(double)tat[0];
  36.     for(int i=1; i<proc.size(); i++)
  37.     {
  38.         tat[i]=(tat[i-1]+proc[i].second.first);
  39.         sum1+=(double)tat[i];
  40.     }
  41.  
  42.     int wait[100];
  43.  
  44.     double sum2=0;
  45.     for(int i=0; i<proc.size(); i++)
  46.     {
  47.         wait[i]=tat[i]-proc[i].second.first;
  48.         sum2+=(double)wait[i];
  49.     }
  50.  
  51.     cout<<"Gantt Chart:: " <<endl;
  52.  
  53.     cout<<"Process ID"<<"     "<<"Turn around Time    "<<"Waiting Time"<<endl;
  54.     for(int i=0; i<proc.size(); i++)
  55.     {
  56.         cout<<proc[i].first<<"                 "<<tat[i]<<"                     "<<wait[i]<<endl;
  57.     }
  58.  
  59.     double avgwait=sum2/no_of_proc;
  60.     double avgtat=sum1/no_of_proc;
  61.  
  62.  
  63.     cout<<"Average waiting time: "<<avgwait<<endl;
  64.     cout<<"Average turn around time: "<<avgtat<<endl;
  65.  
  66.  
  67.  
  68.     return 0;
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement