/* cse2304 prac 2 */ #include int main(int argc, char *argv[]) { FILE *inputfile = NULL; long int m; /* the integer read in from user */ long int n = 0; long int movek; /* move brings k across integer list */ long int i; /* represents each number in the integer list */ long int j; /* j and k start off as 0 and make their way through list, inc= remented */ long int k; /* k represents end position, j represents start position */ int finished = 0; long int lower, upper; /* lower and upper are the lower and upper bounds of the list of integers.= They represent min and max */ long int positionJ = 0; long int positionK = 0; /* the start and end position of longest interva= l */ long int nextnum; /* offset for fseek. Tells us where to set file position= . Each time through while loop, nextnum should move one to the right */ if (argc != 3) /* There must be 3 arguments: program name, m, and filena= me */ { printf("There needs to be 3 command line arguments.\n"); exit(1); } m = atol(argv[1]); /*argv[1] was read in as a string. atol converts argv[1] into a long integ= er, m */ if ((inputfile = fopen(argv[2],"r")) == NULL) { printf("Oooop, I cannot find data.txt.\n"); exit(1); } else { finished = 0; n = 0; } /* data.txt is now open, and ready for reading. */ while(1) { /* first number scenario, unique case */ fscanf(inputfile,"%d",&i); /* scanf in, from the file, the first number i= */ movek = n + 1; j = n; k = n; lower = i; /* 5 is the first number. It's the max AND min */ upper = i; nextnum = ftell(inputfile); /* ftell is a new function. It obtains the = current value of the file position, indicated by inputfile */ while(1) /* in this while loop, keep on scanning in ints from file */ { if (fscanf(inputfile,"%d",&i) == EOF) { finished = 1; break; } if (i > upper) /* if so, i becomes upper */ { if (i - lower <= m) /* tests if mx-mn <= m */ { upper = i; /* the longest interval increases by 1 */ /* we can continue this loop */ } else { break; /* the interval and "longest running total" is broken */ } } else if (i < lower) /* if so, i becomes lower */ { if (upper - i <= m) /* tests if mx-mn <= m */ { lower = i; /* the longest interval increases by 1. i is = now the lowest in the list of integers */ } else { break; } } k = movek; /* increment the interval end position */ movek++; } /* end 2nd while */ if ((positionK - positionJ + 1) < (k - j + 1)) /* if length of old interv= al is less than length of new interval, let new interval be current longest= interval */ { positionK = k; positionJ = j; } if (finished == 1) { break; } /* need to rewind back to start, to read list of integers again */ fseek(inputfile,nextnum,SEEK_SET); /* fseek is a new function. It sets the file position indicator for input= file. The offset, nextnum, is set to the start of the file (SEEK_SET) */ /* If there were 9 integers in file and we were in this while loop the fi= rst time, we would start the loop again, looking at the 2nd integer in the = list, as we have already looked at the first. So the offset would be one (a= mount to shift) */ n++; /* brings j and k upwards, across the list */ } /*close 1st while */ printf("%d %d %d\n",positionJ,positionK,((positionK) - (positionJ) + 1)); /* prints out the start position, end position, and length of longest */ fclose(inputfile); return 0; }