Shortest Seek Time First Algorithm Problem

I have programmed the following program in C. My shortest seek time first (sst) is breaking the program. If I comment out line 68 in main (the call to sst), the program will run the other 2 algorithms fine. I know that my sst is not optimized and has a lot of repeated code, but I am trying to figure out what is breaking it. Right now it will run the first algorithm and just stay in an infinite loop or something. If you see where in the sst the program is breaking, help would be appreciated. Here is the program:

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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
//
//  File.c
//  Project3
//
//  Created by Chris on 5/29/14.
//
//
#include <sys/types.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <dirent.h>
#include <stdio.h>
#include <string.h>
#define MAX 100
#define BUFFER_LENGTH 4096

void fcfs(int input[MAX], int start_head, int counter);
void sst(int input[MAX], int start_head, int counter);
void scanlook(int input[MAX], int start_head, int counter);
int compare (const void * a, const void * b)
{
    return ( *(int*)a - *(int*)b );
}

int main(int argc, char *argv[]){
//start at track 15
    int start_head = 15;
//allow the user to input numbers manually until they enter negative number

    int input[MAX];
    int counter = 0;

    int temp = 0;
    char line[BUFFER_LENGTH];
    char *imp;
    //if they input anything else other than number, print error
    //save the input into an array of max size 100
    while(temp >=0)
    {
        imp = fgets (line, BUFFER_LENGTH, stdin);
        if (imp == NULL)
        {
            printf("%s", "Error in reading! Now exiting");
            exit(1);
        }
        else
        {
            if (sscanf(line, "%d", &temp) != 1)
            {
                printf("You didn't enter a number, now exiting!\n");
                exit(1);
            }
        }
        input[counter] = temp;
        counter++;
    }
    counter--;
    //after they finish the input:
    //call the first come first serve algorithm
    fcfs(input, start_head, counter);
    
    
    //now sort the array for the next 2 algorithms
    qsort (input, counter, sizeof(int), compare);


//call the shortest seek time algorithm
    sst(input, start_head, counter);
//call the Scan-Look Algorithm (elevator)
    scanlook(input, start_head, counter);
    return 0;
}

void fcfs(int input[MAX],int start_head, int counter)
{
    int i=0;
    int total_trav=0;
    int temp;
    printf("First Come First Served\n");
    printf("Head Movement\tTracks Traversed\n");
    
    while(counter>0)
    {
        temp = abs(start_head-input[i]);
        total_trav = total_trav+temp;
        printf("Tracks %d - %d\t%d\n", start_head, input[i], temp);
        start_head = input[i];
        ++i;
        counter--;
    }
    printf("\nTotal Tracks Traversed\t%d\n", total_trav);
}

void sst(int input[MAX], int start_head, int counter)//I think the issue is here
{
    int toprint[MAX],low_lim, high_lim, current=0, holder=0, to_visit=counter;
    //get to the index in input where your start head is in the middle
    while(start_head<input[current] && current < counter)
        ++current;
    high_lim=current;
    if(current>0)
        low_lim=current-1;
    else
    {
        int i;
        for(i=0; ++i; i<counter)
            toprint[i+1]=input[i];
    }
    printf("got to here #1");
    /*--------------------This is the first element only------------------------------------------*/
    if(abs(start_head-input[low_lim])<=abs(start_head-input[high_lim]))
    {
        toprint[holder] =input[low_lim];
        holder++;
        to_visit--;
        if(low_lim>=1)
        {
            current=low_lim;
            low_lim--;
        }
        else
        {
            toprint[holder]= input[0];
            holder++;
            to_visit--;
            while(to_visit>0)
            {
                toprint[holder]=input[high_lim];
                high_lim++;
                to_visit--;
            }
        }
        printf("got to here #2");
    }
    
    else
    {
        toprint[holder] =input[high_lim];
        holder++;
        to_visit--;
        if(high_lim<counter)
        {
            current=high_lim;
            high_lim++;
        }
        else
        {
            toprint[holder]= input[0];
            holder++;
            to_visit--;
            while(to_visit>0)
            {
                toprint[holder]=input[low_lim];
                low_lim--;
                to_visit--;
            }
        }
    }
    printf("got to here #3");
    /*--------------------This starts the loop------------------------------------------*/
    while(to_visit>0)
    {
        //if the lower is closer to this, if they are the same, always go lower.
        if(abs(input[current]-input[low_lim])<=abs(input[current]-input[high_lim]))
        {
            toprint[holder] =input[low_lim];
            holder++;
            to_visit--;
            if(low_lim>=1)
            {
                current=low_lim;
                low_lim--;
            }
            else
            {
                toprint[holder]= input[0];
                holder++;
                to_visit--;
                while(to_visit>0)
                {
                    toprint[holder]=input[high_lim];
                    high_lim++;
                    to_visit--;
                }
            }
            printf("got to here #4");
        }
        
        else
        {
            toprint[holder] = input[high_lim];
            holder++;
            to_visit--;
            if(high_lim<counter)
            {
                current=high_lim;
                high_lim++;
                to_visit--;
            }
            else
            {
                toprint[holder]= input[0];
                holder++;
                to_visit--;
                while(to_visit>0)
                {
                    toprint[holder]=input[low_lim];
                    low_lim--;
                    to_visit--;
                }
            }
        }
    }
    printf("got to here #5");
    fcfs(toprint, start_head, counter);
    return;
}

void scanlook(int input[MAX], int start_head, int counter)
{
    printf("Scan-Look (Elevator)\n");
    printf("Head Movement\tTracks Traversed\n");
    int starter = 0;
    while(start_head>input[starter])
        starter++;
    int second_starter=starter;
    int total_trav=0;
    int temp;
    while(starter<counter)
    {
        temp = abs(start_head-input[starter]);
        total_trav = total_trav+temp;
        printf("Tracks %d - %d\t%d\n", start_head, input[starter], temp);
        start_head = input[starter];
        starter++;
    }
    while(second_starter>0)
    {
        temp = abs(start_head-input[second_starter]);
        total_trav = total_trav+temp;
        printf("Tracks %d - %d\t%d\n", start_head, input[second_starter], temp);
        start_head = input[second_starter];
        second_starter--;
    }
    
    printf("\nTotal Tracks Traversed\t%d\n", total_trav);
}
Not sure what this has to do with Unix/Linux but . . .

line 106 for(i=0; ++i; i<counter) ?
This part is if the first element is at one end of the array, if it is then I just loop through copying the array over to be printed.
counter is how many elements in the array.

As for which forum to post to, I am required to program only in emacs or vim, so I wasn't sure where to post. Thanks for the reply though!
Last edited on
The syntax of a for loop is:
for (initialization; condition; increase) statement;
from here: http://www.cplusplus.com/doc/tutorial/control/

Try this: for(i=0; i<counter; ++i)
Wow can't believe I missed that, that fixed the problem and working great! Thank you so much!
Topic archived. No new replies allowed.