I was writing a SSTF disk scheduling algorithm code in SCILAB and during running after displaying first 3 values (41,34,11) correctly its always showing head at -1. Can somebody tell whats the problem in code? I tried to give a different if condition too but it wasn't also giving correct output.
clear
clf;
seek = 0
x = 8
l = list(176,60,79,92,114,11,34,41)
head = 50
mi = 1
for j = 1:x
for i=1:x
if (l(i)~=(-1))then
if( abs(head - l(i)) < abs(head -l(mi))) then
mi = i
end
end
end
seek = seek + abs(head - l(mi))
head = l(mi)
h(j) = head
se(j) = seek
mprintf('Head is at %i and seek is %i\n',head,seek)
l(mi) = -1
end
plot(h,se)
scatter(h,se)
your problem is that after the first 3 iterations, head=11 is closer to -1 than 60. Replace -1 by%nan
(not a number) and a as a result this value won't never taken as the closest to current head. Moreover by using a vector instead of a list you can remove the inner loop my using max
clear
l = [176,60,79,92,114,11,34,41];
head = 50;
seek = 0;
for j = 1:8
[step,im] = min(abs(head - l));
seek = seek + step;
head = l(im);
mprintf('Head is at %i and seek is %i\n',head,seek);
l(im) = %nan;
h(j) = head
se(j) = seek
end
clf
handle = plot(h,se)
scatter(h,se)
You get the following output:
Head is at 41 and seek is 9
Head is at 34 and seek is 16
Head is at 11 and seek is 39
Head is at 60 and seek is 88
Head is at 79 and seek is 107
Head is at 92 and seek is 120
Head is at 114 and seek is 142
Head is at 176 and seek is 204