-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFCFS.cpp
80 lines (71 loc) · 1.39 KB
/
FCFS.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#1)FCFS
#include<iostream>
#include<stdio.h>
using namespace std;
main()
{
int n;
cout<<"No. of patients-";
cin>>n;
int at[n],bt[n],t=0,wt[n],id[n],tat[n],sum1=0,sum2=0,swap1,swap2,swap3;
float avgtt,avgwt;
for(int i=1;i<=n;i++)
{
id[i]=i;
printf("Patient- %d \n",i);
printf(" ARRIVAL TIME: ");
scanf("%d",&at[i]);
printf(" CONSULTION TIME:");
scanf("%d",&bt[i]);
printf("\n");
}
for (int c = 1;c <n; c++)
{
for (int d = 1 ; d <(n - c); d++)
{
if (at[d] > at[d+1]) /* For decreasing order use < */
{
swap1=at[d];
at[d]=at[d+1];
at[d+1]=swap1;
swap2=bt[d];
bt[d]=bt[d+1];
bt[d+1]=swap2;
swap3=id[d];
id[d]=id[d+1];
id[d+1]=swap3;
}
}
}
cout<<"Gantt Chart---"<<endl;
cout<<0;
for(int i=1;i<=n;i++)
{
if(at[i]<=t)
{
wt[i]=t-at[i];
tat[i]=wt[i]+bt[i];
cout<<" P"<<id[i]<<" "<<t+bt[i];
t=t+bt[i];
}
else
{
cout<<" Idle "<<at[i];
t=at[i];
i--;
}
}
cout<<endl<<endl;
printf("Patients- \tWaiting Time- \tTurn Around Time-\n");
for(int i=1;i<=n;i++)
{
sum1=sum1+wt[i];
sum2=sum2+tat[i];
printf(" P%d \t\t %d \t\t %d\n",i,wt[i],tat[i]);
}
cout<<endl;
avgwt=(float)sum1/n;
avgtt=(float)sum2/n;
cout<<"Average Waiting Time--"<<avgwt<<endl;
cout<<"Average Turnaround Time--"<<avgtt<<endl;
}