-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscan.c
More file actions
81 lines (65 loc) · 2.04 KB
/
Copy pathscan.c
File metadata and controls
81 lines (65 loc) · 2.04 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
78
79
80
81
#include <stdio.h>
#include <stdlib.h>
int main() {
int totalRequest, tracks[100], direction, start, end, headpos, seektime = 0;
printf("Enter the total requests : ");
scanf("%d", &totalRequest);
printf("Enter the request sequence\n");
for(int i = 1; i <= totalRequest; i++) {
scanf("%d", &tracks[i]);
}
printf("Enter the head position : ");
scanf("%d", &start);
printf("Enter the right end of track : ");
scanf("%d", &end);
printf("Enter the direction(0 -> left and 1 -> right) : ");
scanf("%d", &direction);
tracks[0] = 0;
tracks[totalRequest + 1] = start;
tracks[totalRequest + 2] = end;
int size = totalRequest + 3;
for(int i = 0; i < size; i++) {
for(int j = 0; j < size - (i + 1); j++) {
if (tracks[j] > tracks[j + 1]) {
int temp = tracks[j];
tracks[j] = tracks[j + 1];
tracks[j + 1] = temp;
}
}
}
//find the head point after sorting
for(int i = 0; i < size; i++) {
if(tracks[i] == start) {
headpos = i;
break;
}
}
printf("Elavator movement : ");
if(direction == 1) {
for(int i = headpos; i < size; i++) {
printf("%d -> ", tracks[i]);
seektime += abs(start - tracks[i]);
start = tracks[i];
}
for(int i = headpos - 1; i >= 0; i--) {
printf("%d -> ", tracks[i]);
seektime += abs(start - tracks[i]);
start = tracks[i];
}
} else if(direction == 0) {
for(int i = headpos; i >= 0; i--) {
printf("%d -> ", tracks[i]);
seektime += abs(start - tracks[i]);
start = tracks[i];
}
for(int i = headpos + 1; i < size; i++) {
printf("%d -> ", tracks[i]);
seektime += abs(start - tracks[i]);
start = tracks[i];
}
} else {
printf("Wrong input\n");
}
printf("Total seek time : %d\n", seektime);
return 0;
}