Untitled
#include <bits/stdc++.h> using namespace std; struct Prs { int id; int at; int bt; int ct; int tat; int wt; }; int main(){ int n; cout<<"enter the number of processes: "; cin>>n; cout<<"\n"; vector<Prs> pr(n); for(int i=0;i<n;i++) { pr[i].id=i+1; cout<<"enter arrival time for process "<<i+1<<" "; cin>>pr[i].at; cout<<"\n"; cout<<"enter burst time for process "<<i+1<<" "; cin>>pr[i].bt; cout<<"\n"; } sort(pr.begin(),pr.end(),[](Prs a, Prs b) { return a.at < b.at;}); int curt=0; float totat=0, towt=0; for(int i=0;i<n;i++){ if(curt<pr[i].at){ curt = pr[i].at; } pr[i].ct = curt + pr[i].bt; pr[i].tat = pr[i].ct - pr[i].at; pr[i].wt = pr[i].tat - pr[i].bt; curt = pr[i].ct; totat += pr[i].tat; towt += pr[i].wt; } for(auto &p: pr){ cout<< "P" << p.id << "\t" <<p.at<<"\t"<<p.bt<<"\t"<<p.ct<<"\t"<<p.tat<<"\t"<<p.wt<<endl;} cout<<"Gantt chart:\n"; //cout<<"\t"; for(auto &p:pr){ cout<< " P"<<p.id<< "\t|"; } cout<<"\n"; cout<<pr[0].at; for(auto &p:pr){ cout<<"\t"<<p.ct; } cout<<"\nAverage turn around time is: "<<totat/n<<endl; cout<<"Average waiting time is: "<<towt/n<<endl; return 0; }
Leave a Comment