-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFirstComeFirstServe.cpp
47 lines (44 loc) · 1.05 KB
/
FirstComeFirstServe.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <iostream>
#include<utility>
#include<algorithm>
using namespace std;
int main(){
int n;
cout<<"Enter the number of process"<<endl;
cin>>n;
pair<int,int>time[n];
int ct[n],tat[n];
cout<<"Enter the arrival time of the process"<<endl;
for(int i=0;i<n;i++){
cin>>time[i].first;
}
cout<<"Enter the bust time of the process"<<endl;
for(int i=0;i<n;i++){
cin>>time[i].second;
}
sort(time,time+n);
int curr=time[0].first;
for(int i=0;i<n;i++){
if(time[i].first<=curr){
ct[i]=curr+time[i].second;
curr+=time[i].second;
}else{
curr=time[i].first;
ct[i]=curr+time[i].second;
curr+=time[i].second;
}
}
for(int i=0;i<n;i++){
tat[i]=ct[i]-time[i].first;
}
float avgTat=0;
for(int i=0;i<n;i++){
avgTat+=tat[i];
}
cout<<"Turnaround Time of the process is as follows: "<<"\n";
for(int i=0;i<n;i++){
cout<<tat[i]<<" ";
}
avgTat/=n;
cout<<"\n"<<avgTat<<endl;
}