-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscan.cpp
More file actions
77 lines (75 loc) · 1.83 KB
/
scan.cpp
File metadata and controls
77 lines (75 loc) · 1.83 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
#include <bits/stdc++.h>
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
void scan(int arr[], int n, int head)
{
int count = 0;
vector<int> left, right;
vector<int> sequence;
for (int i = 0; i < n; i++)
{
if (arr[i] > head)
left.push_back(arr[i]);
else
right.push_back(arr[i]);
}
sequence.push_back(head);
sort(left.begin(), left.end());
sort(right.begin(), right.end());
string str;
cout << "Enter direction" << endl;
cin >> str;
int x = 2;
while (x--)
{
if (str == "left")
{
for (int i = left.size() - 1; i >= 0; i--)
{
sequence.push_back(left[i]);
count += abs(head - left[i]);
head = left[i];
}
str = "right";
}
else if (str == "right")
{
for (int i = 0; i < right.size(); i--)
{
sequence.push_back(right[i]);
count += abs(head - right[i]);
head = left[i];
}
str = "left";
}
}
cout << "Total number of seek operations = "
<< count << endl;
cout << "Seek Sequence is" << endl;
for (int i = 0; i < sequence.size(); i++)
{
cout << sequence[i] << endl;
}
}
int main()
{
int n;
cout << "ENTER NUMBER OF REQUEST" << endl;
cin >> n;
int arr[n];
cout << "A disk queue with requests for I/O to blocks on cylinders" << endl;
for (int i = 0; i < n; i++)
{
arr[i] = rand() % 200;
cout << arr[i] << " ";
}
cout << endl;
int h;
cout << "Intial head position" << endl;
cin >> h;
cout << "\n Head start at:" << h << endl;
// cout << "\n\nSuppose head start at:" << head << endl;
scan(arr, n, h);
}