-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDISKSHED_SSTF.cpp
More file actions
62 lines (48 loc) · 1.48 KB
/
DISKSHED_SSTF.cpp
File metadata and controls
62 lines (48 loc) · 1.48 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
#include <iostream>
#include <cstdlib>
#include <vector>
#include <cmath>
using namespace std;
// Function to calculate the absolute difference between two numbers
int absDiff(int a, int b) {
return abs(a - b);
}
int main() {
int n, head;
cout << "Enter the number of requests: ";
cin >> n;
if (n <= 0) {
cout << "Invalid number of requests. Exiting the program." << endl;
return 0;
}
vector<int> arr(n);
cout << "Enter the request values:" << endl;
for (int i = 0; i < n; i++) {
cout << "Request " << i + 1 << ": ";
cin >> arr[i];
}
cout << "Enter the initial head position: ";
cin >> head;
int totalSeekTime = 0;
int current = head;
cout << "Seek Sequence: ";
for (int i = 0; i < n; i++) {
int shortestSeekTime = INT_MAX;
int shortestSeekTimeIndex = -1;
for (int j = 0; j < n; j++) {
int diff = absDiff(arr[j], current);
if (diff < shortestSeekTime) {
shortestSeekTime = diff;
shortestSeekTimeIndex = j;
}
}
cout << arr[shortestSeekTimeIndex] << " ";
totalSeekTime += shortestSeekTime;
current = arr[shortestSeekTimeIndex];
arr[shortestSeekTimeIndex] = INT_MAX;
}
cout << endl;
cout << "Total Seek Time: " << totalSeekTime << endl;
cout<<"Throughput:"<<(float)n/totalSeekTime;
return 0;
}