-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnew-scan.c
More file actions
66 lines (52 loc) · 1.3 KB
/
Copy pathnew-scan.c
File metadata and controls
66 lines (52 loc) · 1.3 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
#include <stdio.h>
#include <stdlib.h>
int main() {
int n, req[100], head, size, seek = 0;
printf("Enter number of requests: ");
scanf("%d",&n);
printf("Enter request sequence:\n");
for(int i=0;i<n;i++)
scanf("%d",&req[i]);
printf("Enter initial head position: ");
scanf("%d",&head);
printf("Enter disk size: ");
scanf("%d",&size);
// Add boundary points
req[n] = 0;
req[n+1] = size-1;
int total = n + 2;
// Sort
for(int i=0;i<total-1;i++)
for(int j=0;j<total-i-1;j++)
if(req[j] > req[j+1]) {
int t = req[j];
req[j] = req[j+1];
req[j+1] = t;
}
int pos;
for(int i=0;i<total;i++) {
if(req[i] >= head) {
pos = i;
break;
}
}
printf("Head Movement:\n%d -> ", head);
// Move right
for(int i=pos;i<total;i++) {
seek += abs(head - req[i]);
head = req[i];
printf("%d -> ", head);
}
// Jump to start (IMPORTANT FIX)
seek += abs(head - 0);
head = 0;
printf("0 -> ");
// Continue
for(int i=0;i<pos;i++) {
seek += abs(head - req[i]);
head = req[i];
printf("%d -> ", head);
}
printf("\nTotal seek time = %d\n", seek);
return 0;
}