-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiskSCAN.c
More file actions
103 lines (91 loc) · 2.02 KB
/
Copy pathdiskSCAN.c
File metadata and controls
103 lines (91 loc) · 2.02 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include<stdio.h>
#include<stdlib.h>
#define MAX_REQ 100
int rnum , head , dir , MAX_TRACK;
int scan(int req[]){
int totalTime=0;
int index=0;
//SORT THE REQUESTS
for(int i=0;i<rnum;i++){
for(int j=0;j<rnum-i-1;j++){
if(req[j]>req[j+1]){
int temp = req[j];
req[j]=req[j+1];
req[j+1]=temp;
}
}
}
//FIND THE index
for(int i=0;i<rnum;i++){
if(req[i]>=head){
index=i;
break;
}
}
//going in right direction
if(dir == 1){
for(int i=index;i<rnum;i++){
totalTime+=abs(head-req[i]);
head=req[i];
}
if(head!=MAX_TRACK-1){
totalTime+=abs(MAX_TRACK-1-req[rnum-1]);
head = MAX_TRACK-1;
for(int i=index-1;i>=0;i--){
totalTime+=abs(head-req[i]);
head=req[i];
}
}
}
else{
for(int i=index-1;i>=0;i--){
totalTime+=abs(head-req[i]);
head=req[i];
}
if(head!=0){
totalTime+=abs(req[0]-0);
head=0;
for(int i=index;i<rnum;i++){
totalTime+=abs(head-req[i]);
head=req[i];
}
}
}
return totalTime;
}
int main(){
int requests[MAX_REQ];
printf("Enter the number of requests:\n");
scanf("%d",&rnum);
printf("Enter the requests:\n");
for(int i=0;i<rnum;i++){
scanf("%d",&requests[i]);
}
printf("Enter the head:\n");
scanf("%d",&head);
printf("Enter the direction:\n");
scanf("%d",&dir);
printf("Enter the MAX_TRACK\n");
scanf("%d",&MAX_TRACK);
int seekTime = scan(requests);
printf("The total seekTime using scan is:%d",seekTime);
}
/*SAMPLE OUTPUT
Enter the number of requests:
7
Enter the requests:
52
42
2
56
73
44
97
Enter the head:
47
Enter the direction:
1
Enter the MAX_TRACK
200
The total seekTime using scan is:349
*/