/********************************************************************* lexicographic4.c Read in a 4-tuple a[0], a[1], a[2], a[3] from the file "ordered4tuples". So now a[0] <= a[1] <= a[2] <= a[3]. Put into lexicographic order, eliminating repetitions. **********************************************************************/ #include #include main() { int a[4]; int b[4]; int current[4]; int lastwritten[4]; int nextbigger[4]; int biggest[4]; int smallest[4]; int end; int i, j, d, m; float r1, r2; int maxa0, maxa1, maxa2, maxa3; int count, swap, SaveB; int isbigger4(int x[4], int y[4]){ int bigger; /* returns 0 if x < or equal to y; returns 1 if x > y */ bigger = 0; /* assume that x is smaller or equal to y */ if(x[0] > y[0]) bigger = 1; if(x[0] == y[0] && x[1]>y[1]) bigger = 1; if(x[0] == y[0] && x[1]==y[1] && x[2]>y[2]) bigger = 1; if(x[0] == y[0] && x[1]==y[1] && x[2]==y[2] && x[3]>y[3]) bigger = 1; return bigger; } FILE *fp; FILE *nfp; /* read in the first 4tuple from the file */ fp = fopen("ordered4tuples", "r"); nfp = fopen("final4tuples", "w"); count = 0; maxa0 = 1; maxa1 = 1; maxa2 = 1; maxa3 = 1; end=fscanf( fp, "%d %d %d %d", &a[0],&a[1],&a[2],&a[3]); while(end > 0){ count ++; if(a[0] > maxa0) maxa0 = a[0]; if(a[1] > maxa1) maxa1 = a[1]; if(a[2] > maxa2) maxa2 = a[2]; if(a[3] > maxa3) maxa3 = a[3]; end=fscanf( fp, "%d %d %d %d", &a[0],&a[1],&a[2],&a[3]); } printf("The largest value of a[0] that occurs is %d.\n", maxa0); printf("The largest value of a[1] that occurs is %d.\n", maxa1); printf("The largest value of a[2] that occurs is %d.\n", maxa2); printf("The largest value of a[3] that occurs is %d.\n", maxa3); printf("There are %d 4tuples on the list, possibly with repetitions.\n", count); /*********************************************************** read through the file and find the smallest 4-tuple. Write it to the file "final4tuples" using nfp Remember it as the 4tuple lastwritten. *************************************************************/ fclose(fp); fp = fopen("ordered4tuples", "r"); end = fscanf( fp, "%d %d %d %d", &smallest[0],&smallest[1],&smallest[2],&smallest[3]); for(i=0; i<4; i++) biggest[i] = smallest[i]; /* loop to find the first and the last 4tuple */ while(end > 0){ end = fscanf( fp, "%d %d %d %d", &b[0],&b[1],&b[2],&b[3]); /* Compare 'smallest' and b. If 'smallest' is smaller, keep it. If b is smaller then replace 'smallest' by b */ if( isbigger4(smallest,b) == 1) for(i=0; i<4; i++) smallest[i] = b[i]; /* Compare 'biggest' and b. If 'biggest' is bigger, keep it. If b is bigger then replace 'biggest' by b */ if( isbigger4(biggest,b) == 0 ) for(i=0;i<4;i++) biggest[i] = b[i]; }/* end of while loop */ for(i=0; i<4; i++){ fprintf(nfp, "%5d ", smallest[i]); lastwritten[i] = smallest[i]; } fprintf(nfp, "\n"); for(j=0; j 0){ end = fscanf( fp, "%d %d %d %d", &b[0],&b[1],&b[2],&b[3]); /* Check if b is smaller than lastwritten. If no, then SaveB = 0. If yes, more testing is needed and SaveB = 1 */ SaveB = isbigger4(b,lastwritten); /* if SaveB == 1, that is, if lastwritten > b, then we need to compare nextbigger to b. If nextbigger < b then SaveB = 0. If nextbigger > or equal to b then SaveB = 1. */ if(SaveB==1) SaveB = isbigger4(nextbigger,b); if(SaveB==1) for(i=0;i<4;i++) nextbigger[i] = b[i]; }/* end of while loop */ for(i=0; i<4; i++){ fprintf(nfp, "%5d ", nextbigger[i]); lastwritten[i] = nextbigger[i]; } fprintf(nfp, "\n"); if(nextbigger[0] == biggest[0]){ if(nextbigger[1]==biggest[1]){ if(nextbigger[2] == biggest[2]){ if(nextbigger[3] == biggest[3]){ break; } } } } } fclose( fp ); fclose( nfp ); } /* end of main */