-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathC-LOOK.cpp
148 lines (111 loc) · 2.17 KB
/
C-LOOK.cpp
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#include<bits/stdc++.h>
int find(int d[],int n,int disk)
{
for(int i=0;i<n-1;i++)
{
if(d[i]>disk)
{
return i;
break;
}
}
}
int main()
{
int i,j,n;
int disk; //loc of head
int temp,max;
int dloc; //loc of disk in array
int no_t;
printf("enter total number of tracks\n");
scanf("%d",&no_t);
printf("enter number of location\n");
scanf("%d",&n);
int d[n+1]; //disk queue
printf("enter position of head\n");
scanf("%d",&disk);
printf("enter elements of disk queue\n");
for(i=0;i<n;i++)
{
scanf("%d",&d[i]);
}
for(i=0;i<n-1;i++) // sorting disk locations
{
for(j=i+1;j<n;j++)
{
if(d[i]>d[j])
{
temp=d[i];
d[i]=d[j];
d[j]=temp;
}
}
}
printf("elements after sorting\n");
for(i=0;i<n;i++)
{
printf("%d ",d[i]);
}
int prev_head;
printf("\nenter previous head position\n");
scanf("%d",&prev_head);
int c;
int sum=0;
if(prev_head>disk)
c=1;
else
c=2;
//go towards left
if(c==1)
{
int previous = find(d,n,disk);
previous = previous-1;
//printf("previous=%d\n",previous);
sum+=abs(disk-d[previous]);
//printf("sum=%d\n",sum);
printf("%d->",disk);
for(i=previous;i>=0;i--)
{
printf("%d ->",d[i]);
if(i!=0)
{
int dr=abs(d[i]-d[i-1]);
sum+=dr;
//printf("sum=%d\n",sum);
}
}
for(i=n-1;i>=previous+1;i--)
{
printf("%d ->",d[i]);
if(i!=previous+1)
{
int dr=abs(d[i]-d[i-1]);
sum+=dr;
// printf("sum=%d\n",sum);
}
}
printf("\nmovement of total cylinders %d\n",sum);
}
//go towards right
else if(c==2)
{
int previous = find(d,n,disk);
printf("%d\n",previous);
sum+=abs(d[previous]-disk);
printf("%d->",disk);
for(i=previous;i<n;i++)
{
printf("%d ->",d[i]);
if(i!=n-1)
sum+=abs(d[i+1]-d[i]);
}
for(i=0;i<=previous-1;i++)
{
printf("%d ->",d[i]);
if(i!=previous-1)
sum+=abs(d[i+1]-d[i]);
}
printf("\nmovement of total cylinders %d\n",sum);
}
return 0;
}