-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathfirst_come_first_service.cpp
More file actions
124 lines (106 loc) · 2.34 KB
/
first_come_first_service.cpp
File metadata and controls
124 lines (106 loc) · 2.34 KB
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#include <bits/stdc++.h>
#include <graphics.h>
using namespace std;
#define ll long long
#define f(i, n) for (int i = 0; i < n; i++)
void DDA(float x1, float y1, float x2, float y2, int color)
{
float dx = x2 - x1;
float dy = y2 - y1;
// Finding slope
float m = dy / dx;
float steps;
if (abs(m) <= 1)
{
steps = abs(dx);
}
else
{
steps = abs(dy);
}
float Delx = dx / steps;
float Dely = dy / steps;
float x = x1, y = y1;
putpixel(round(x), round(y), color);
for (int k = 1; k <= steps; k++)
{
x = x + Delx;
y = y + Dely;
putpixel(round(x), round(y), color);
}
}
int wf = 10;
int h = 50;
void makeRectangle(int x, int y, int t, int color)
{
int ax = x, ay = y;
int bx = ax + (t * wf), by = ay;
int cx = bx, cy = by + h;
int dx = ax, dy = cy;
DDA(ax, ay, bx, by, color);
DDA(bx, by, cx, cy, color);
DDA(cx, cy, dx, dy, color);
DDA(dx, dy, ax, ay, color);
}
void print(int x, int y, string s)
{
int n = s.length();
char c[n + 1];
for (int i = 0; i < n; i++)
{
c[i] = s[i];
}
c[n] = '\0';
outtextxy(x, y, c);
}
int main()
{
initwindow(800, 800);
setcolor(WHITE);
cout << "Enter number of processes\n";
int n;
cin >> n;
int burstTimes[n];
cout << "Enter the burst times :\n";
int x = 100, y = 100;
f(i, n)
{
cout << "Process " << (i + 1) << "th :";
cin >> burstTimes[i];
makeRectangle(x, y, burstTimes[i], 15);
string name = "P" + to_string(i + 1);
print(x + 5, y + 10, name);
x += burstTimes[i] * wf;
}
int waitingTimes[n];
int turnaround = 0;
float avgWait = 0;
x = 100, y = 100;
f(i, n)
{
cout << "For process " << (i + 1) << "\n";
turnaround += burstTimes[i];
cout << "waiting time : ";
waitingTimes[i] = (turnaround - burstTimes[i]);
cout << waitingTimes[i];
avgWait += waitingTimes[i];
cout << endl
<< endl;
makeRectangle(x, y, burstTimes[i], 13);
delay(500);
print(x, y + h + 15, to_string(waitingTimes[i]));
x += burstTimes[i] * wf;
}
avgWait = avgWait / n;
cout << "Avg wait time: " << avgWait << endl;
getch();
return 0;
}
/*
Sample Input
4
15
7
20
8
*/