-
Notifications
You must be signed in to change notification settings - Fork 0
/
c-scan.c
69 lines (55 loc) · 1.57 KB
/
c-scan.c
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
#include <stdio.h>
void cscan(int arr[], int head, int size, int seek_sequence[]) {
int seek_count = 0;
int distance, cur_track;
// Appending end values to the end and start of the array
arr[size] = head;
arr[size+1] = 0;
// Sorting the array
for(int i = 0; i <= size; i++) {
for(int j = i+1; j <= size+1; j++) {
if(arr[i] > arr[j]) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}
int index;
for(int i = 0; i <= size+1; i++) {
if(head == arr[i]) {
index = i;
break;
}
}
// Moving the disk arm to the end
for(int i = index; i <= size+1; i++) {
cur_track = arr[i];
seek_sequence[seek_count++] = cur_track;
}
// Moving the disk arm back to the start
for(int i = 0; i < index; i++) {
cur_track = arr[i];
seek_sequence[seek_count++] = cur_track;
}
printf("Seek Sequence is: ");
for(int i = 0; i < seek_count; i++) {
printf("%d ", seek_sequence[i]);
}
printf("\nTotal seek time: %d\n", seek_count-1);
}
int main() {
int size, head;
printf("Enter the size of request queue: ");
scanf("%d", &size);
int arr[size];
int seek_sequence[size+2]; // To store seek sequence
printf("Enter the request queue: ");
for(int i = 0; i < size; i++) {
scanf("%d", &arr[i]);
}
printf("Enter the initial head position: ");
scanf("%d", &head);
cscan(arr, head, size, seek_sequence);
return 0;
}