-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSJF.cpp
More file actions
74 lines (74 loc) · 1.5 KB
/
Copy pathSJF.cpp
File metadata and controls
74 lines (74 loc) · 1.5 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
#include<iostream>
#include<iomanip>
using namespace std;
class sjf_alg
{
int exe[10],id[10];
int n;
void sort(int *,int*);
public:
void getdata();
void display();
void cal_wt_tt();
};
void sjf_alg::getdata()
{
cout<<"How many processes to be entered : ";
cin>>n;
for(int i=0;i<n;i++)
{
cout<<"Enter execution time of "<<i+1<<" process : ";
cin>>exe[i];
id[i]=i+1;
}
}
void sjf_alg::display()
{
cout<<endl<<"Process ID\tExecution time\tArrival Time "<<endl;
for(int i=0;i<n;i++)
cout<<setw(5)<<i+1<<setw(15)<<exe[i]<<setw(15)<<"0"<<endl;
}
void sjf_alg::sort(int *f,int *l)
{
int temp;
for(int y=0;y<n-1;y++)
{
for(int z=0;z<n-1;z++)
if(f[z]>f[z+1])
{
temp=f[z];
f[z]=f[z+1];
f[z+1]=temp;
temp=l[z];
l[z]=l[z+1];
l[z+1]=temp;
}
}
}
void sjf_alg::cal_wt_tt()
{
int wt=0,tnt=0;
float avg=0,avtnt=0;
sort(exe,id);
cout<<"\nProcess ID \tWaiting time \tTurn Around time "<<endl;
for(int i=0;i<n;i++)
{
tnt+=exe[i];
avtnt+=tnt;
avg+=wt;
cout<<setw(5)<<id[i]<<setw(15)<<wt<<setw(15)<<tnt<<endl;
wt+=exe[i];
}
avg=avg/(float)n;
avtnt/=(float)n;
cout<<"\nAverage Waiting time : "<<avg;
cout<<"\nAverage turn Around time : "<<avtnt<<endl;
}
int main()
{
sjf_alg sjf;
sjf.getdata();
sjf.display();
sjf.cal_wt_tt();
return 0;
}