//================================================= file = sortdouble.c ===== //= Program to sort a series X double values = //=========================================================================== //= Notes: = //= 1) Input from input file "in.dat" to stdin (see example below) = //= - No comments allowed = //= 2) Output is to stdout = //= 3) Uses qsort() = //=-------------------------------------------------------------------------= //= Example "in.dat" file: = //= = //= 10.1 = //= 3.5 = //= 6.0 = //= 4.0 = //= 1.1 = //= 2.1 = //= 2.2 = //= 7.0 = //= 6.0 = //=-------------------------------------------------------------------------= //= Example output (for above "in.dat") = //= = //= 1.100000 = //= 2.100000 = //= 2.200000 = //= 3.500000 = //= 4.000000 = //= 6.000000 = //= 6.000000 = //= 7.000000 = //= 10.100000 = //=-------------------------------------------------------------------------= //= Build: gcc sortdouble.c, bcc32 sortdouble.c, cl sortdouble.c = //=-------------------------------------------------------------------------= //= Execute: sortdouble < in.dat = //=-------------------------------------------------------------------------= //= Author: Ken Christensen = //= University of South Florida = //= WWW: http://www.csee.usf.edu/~christen = //= Email: christen@csee.usf.edu = //=-------------------------------------------------------------------------= //= History: KJC (03/15/12) - Genesis (from sort.c) = //= KJC (05/23/18) - Minor clean up = //=========================================================================== //----- Include files ------------------------------------------------------- #include // Needed for printf() and feof() #include // Needed for exit() and atof() //----- Constants ----------------------------------------------------------- #define M_SIZE 1048576 // Memory block for input //----- Function prototypes ------------------------------------------------- int comp(const void *p, const void *q); // Compare p and q for qsort() //=========================================================================== //= Main program = //=========================================================================== int main(void) { double *X; // Integer data from input file int N; // Number of values in "in.dat" int i; // Loop counte char inputString[32]; // Input string double input; // Input vaue // Read input into block of size M_SIZE N = 0; X = (double *) malloc(M_SIZE*sizeof(double)); while(1) { scanf("%s", inputString); input = atof(inputString); if (feof(stdin)) break; N++; if ((N % M_SIZE) == 0) X = (double *) realloc(X, (N + M_SIZE)*sizeof(double)); if (X == NULL) { fprintf(stderr,"*** ERROR 001 -- realloc() failed with N = %d", N); exit(-1); } X[N-1] = input; } X = (double *) realloc(X, N*sizeof(double)); if (X == NULL) { fprintf(stderr,"*** ERROR 002 -- realloc() failed with N = %d", N); exit(-1); } // Sort the series X of size N using qsort() qsort(X, N, sizeof(double), comp); // Output the sorted series to stdout for (i=0; i