1

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)

1 Answer 1

1

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

enter image description here

Sign up to request clarification or add additional context in comments.

5 Comments

thank you this helped a lot and is there any command that lets you generate random integers in SCILAB
also i gave a condition for number not to be -1 why is that not working?
It is a pure algorithmic problem: at the first iteration of your for i loop, mi has the value of the previous iteration of the for j loop. Make your program run by hand you will easilly see that.
For uniform random integers for example Y = grand(1, 10, "uin", 1,100) generates a 1x10 vector of uniform random integers betwwen 1 and 10.
between 1 and 100, sorry.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.