-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsjf.cpp
More file actions
73 lines (73 loc) · 1.63 KB
/
sjf.cpp
File metadata and controls
73 lines (73 loc) · 1.63 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
# include<iostream>
# include<vector>
using namespace std;
int mat[10][6];
int main()
{
int n,arr,bur,tempa,tempb;
cout<<"Enter the no of processes:";
cin>>n;
for(int i=0;i<n;i++)
{
cout<<"Enter arrival time :";
cin>>arr;
mat[i][0]=arr;
cout<<"Enter burst time :";
cin>>bur;
mat[i][1]=bur;
}
for(int i=0;i<n;i++)
{
if(mat[i][0]==0)
{
tempa=mat[0][0];
tempb=mat[0][1];
mat[0][0]=mat[i][0];
mat[0][1]=mat[i][1];
mat[i][0]=tempa;
mat[i][1]=tempb;
break;
}
}
int j;
for(int i=2;i<n;i++)
{
int key=mat[i][1];
int keyarr=mat[i][0];
j=i-1;
while(j>=1 && mat[j][1]>key)
{
mat[j+1][1]=mat[j][1];
mat[j+1][0]=mat[j][0];
j=j-1;
}
mat[j+1][1]=key;
mat[j+1][0]=keyarr;
}
mat[0][2]=0;
mat[0][3]=mat[0][1];
for(int i=1;i<n;i++)
{
mat[i][2]=mat[i-1][2]+mat[i-1][1];
mat[i][3]=mat[i][1]+mat[i-1][3];
}
for(int i=0;i<n;i++)
{
mat[i][4]=mat[i][3]-mat[i][0];
mat[i][5]=mat[i][2]-mat[i][0];
}
float avg_tat=0,avg_wt=0;
for(int i=0;i<n;i++)
{
avg_tat=avg_tat+mat[i][4];
avg_wt=avg_wt+mat[i][5];
}
cout<<endl;
cout<<"Average turn around time"<<avg_tat*1.0/n<<endl;
cout<<"Average waiting time"<<avg_wt*1.0/n<<endl;
for(int i=0;i<n;i++)
{
cout<<mat[i][0]<<" "<<mat[i][1]<<" "<<mat[i][2]<<" "<<mat[i][3]<<" "<<mat[i][4]<<" "<<mat[i][5]<<endl;
}
return 0;
}