#include #include #define MAX_PRINT_THRESHOLD 20 typedef struct { int start; int end; int length; }Longest; /*find the longest interval where mx is max mn is min*/ int LongestInterval(FILE* input, int m, Longest* pos) { int mx = 0; //max value int mn = 0; //min value int value; int k; //to store end value int j; //to store start value long int filepos; //current file position if((fscanf(input, "%d", &value) == EOF)) { fprintf(stderr, "There is nothing in the file to process\n"); return 1; } //initialise variables mx = mn = value; j = 0; //start k = 0; //end pos->length = 1; pos->start = 0;=20 pos->end = 0; filepos = ftell(input); //first element while((fscanf(input, "%d", &value) != EOF)) { //test condition: value > mx = mx -- maxvalue value < mn = mn -- min= value if(value > mx){ mx = value; }=20=20 if(value < mn){ mn = value; } /*Condition : mx-mn <= m*/ if(mx - mn <= m) { /*When mx-mn is satisfied k++(end)*/ /*increment end counter as satisfy condition*/ k++;=20 // printf("%d ", value); } else { // printf(" %d -- start: %d end %d length %d\n", value, j, k, k-j+1); /* does not satisfy condition*/ /* When mx-mn fails (value of startpos= j, endpos = k, leng = (k-= j)) -compare to previous length:=20 if(current lenght > previous Length) let the previous Length = current Lenght NB: first occurrence reset k jump the pointer to j+1 increment j (the start value) */ if(((k-j)+1) > pos->length){ pos->start = j; pos->end = k; pos->length = (k-j)+1; } j++; //goes to next value in file. k = j; //jump the file pointer to j+1 -- from previously recorded position fseek(input, filepos, SEEK_SET); //so u know what value to initialise mx and mn if(fscanf(input, "%d", &value) == EOF) break; mx = mn = value; filepos = ftell(input); } } if(((k-j)+1) > pos->length) { pos->start = j; pos->end = k; pos->length = (k-j)+1; } return 0; } /*=20 longest 2 inp.txt argc = 2 argv[0] = longest argv[1] = 2 argv[2] = inp.txt=20 */ int main(int argc, char** argv) { int m,i,v; FILE* inp = NULL; Longest lpos ; if(argc != 3) { fprintf(stderr, "Usage: %s \n", argv[0]); return 1; } //Open file from command line, mode "r" to read text if((inp = fopen(argv[2], "r")) == NULL) { fprintf(stderr, "Could not open %s\n", argv[2]); return 1; } //to get value m and convert to integer m = atoi(argv[1]); if(m<0) { fprintf(stderr, "The value m has to be a non negative integer\n"); return 1; } //Find the result if((LongestInterval(inp, m,&lpos)) ) { fprintf(stderr, "Could not open file %s\n", argv[2]); fclose(inp); return 1; } //Output results /*if(lpos.length <= MAX_PRINT_THRESHOLD) { rewind(inp); printf("\n\n\n\nPrinting the Longest interval\n"); for(i=0;i<= lpos.end;i++) { if((fscanf(inp,"%d",&v)) == EOF) break; if(i == lpos.start) printf("%d",v); else if(i >= lpos.start) printf(" %d", v); } } else { printf("The result is over %d numbers long, no numbers printed.",MAX_PRIN= T_THRESHOLD); }*/ if(lpos.length) { printf("\nLongest: %d %d %d\n", lpos.start, lpos.end, lpos.length); }else { printf("\nNO longest interval that satisfy mx-mn <= m\n"); } fclose(inp); return 0; }