/* A program that takes 2 command-line parameters: non-negative integer m a= nd the name of a file * containing n integers in free format. The prgram finds the longest inter= val such that * mx-mn<=m, and prints the interval's start position, its end position, = and its length, * where mx is the maximum number in the interval, and mn is the minimum nu= mber in the interval. */ #include #include #include #define MAX 10000 int main(int argc, char * argv[]) { FILE * fp; int array[MAX]; int diff; int startSoFar, endSoFar, max, min, lengthSoFar; int startNew, endNew, lengthNew; int num1, num2, offset, i, j=0; /* Check for the correct number and order of arguments */ if (argc != 3) { fprintf(stderr, "Invalid number of arguments. Usage: %s difference filena= me.\n", argv[0]); exit(1); } else if (!isdigit(*argv[1])) { fprintf(stderr, "Invalid first argument. Non-negative integer required.\n= "); exit(2); } else if ((fp=fopen(argv[2], "r")) == NULL) { fprintf(stderr, "Unable to open file %s for reading.\n", argv[2]); exit(3); } else if (fscanf(fp, "%d", &num1) != 1) { fprintf(stderr, "Error reading file %s.\n", argv[2]); exit(4); } else diff=atoi(argv[1]); j++; /* First number has been read */ startSoFar = endSoFar = -1; lengthSoFar=0; startNew = endNew = -1; lengthNew=0; while (startSoFar == -1 && fscanf(fp, "%d", &num2) == 1) { j++; if (num1 == num2) { min=max=num1; startSoFar=j-2; } else { min = (num1num2) ? num1 : num2; if (max-min <= diff) startSoFar=j-2; else num1=num2; } } if (startSoFar == -1) { printf("No interval was found in the file that satisfied the condition.\n= "); printf(" %d %d %d\n", startSoFar, endSoFar, lengthSoFar); } else { offset=startSoFar; /* current difference between number's positions in = file and in array */ array[0]=num1; array[1]=num2; i=2; while (i=min && array[i]<=max) { i++; continue; } else if (array[i]>max) max=array[i]; else min=array[i]; /* Check if condition of difference is still satisfied */ if (max-min <= diff) { i++; continue; } else { /* Close the current interval */ if (lengthSoFar == 0) { /* no prior interval */ endSoFar=i-1+offset; lengthSoFar=endSoFar-startSoFar+1; } else { endNew=i-1+offset; lengthNew=endNew-startNew+1; /* Check the length of newly found interval * If new one is longer, reset the start and finish to that new one */ if (lengthNew>lengthSoFar) { startSoFar=startNew; endSoFar=endNew; lengthSoFar=lengthNew; } /* Move array elements to the start of the array */ for (j=0; j<=lengthNew; j++) array[j]=array[i-lengthNew+j]; offset=offset+i-lengthNew; i=lengthNew; } /* Not end of file. Find the start of new interval */ j=i-1; if (array[i] == max) { min=array[i]; while (array[i]-array[j]<=diff) { min = (array[j]max) ? array[j] : max; j--; } } startNew=j+1+offset; i++; } } printf("Last index value: i=%d\n", i); /* Check if the firstly found interval was ever closed of */ if (endSoFar == -1) { endSoFar=i-1+offset; lengthSoFar=endSoFar-startSoFar+1; } printf(" %d %d %d\n", startSoFar, endSoFar, lengthSoFar); } return 0; }