//====================================================== file = qsort.c ===== //= Program to sort a list of floating point (double) numbers = //=========================================================================== //= Notes: = //= 1) Input from input file "in.dat" to stdin (see example below) = //= * No comments are allowed = //= 2) Output is to stdout = //=-------------------------------------------------------------------------= //= Example "in.dat" file: = //= = //= 1.0 = //= 1.0 = //= 28.0 = //= 6.0 = //= 9.0 = //= 2.0 = //= 19.0 = //= 19.0 = //=-------------------------------------------------------------------------= //= Example output (for above "in.dat") = //= = //= 1.000000 = //= 1.000000 = //= 2.000000 = //= 6.000000 = //= 9.000000 = //= 19.000000 = //= 19.000000 = //= 28.000000 = //=-------------------------------------------------------------------------= //= Build: bcc32 qsort.c = //=-------------------------------------------------------------------------= //= Execute: qsort < in.dat = //=-------------------------------------------------------------------------= //= Author: Ken Christensen = //= University of South Florida = //= WWW: http://www.csee.usf.edu/~christen = //= Email: christen@csee.usf.edu = //=-------------------------------------------------------------------------= //= History: KJC (07/25/07) - Genesis = //=========================================================================== //----- Include files ------------------------------------------------------- #include // Needed for printf() and feof() #include // Needed for exit(), atof(), and qsort() //----- Defines ------------------------------------------------------------- #define MAX_SIZE 2000000 // Maximum size of time series data array //----- Globals ------------------------------------------------------------- double X[MAX_SIZE]; // Time series read from "in.dat" int N; // Number of values in "in.dat" //----- Function prototypes ------------------------------------------------- int comp(const void *p, const void *q); // Compare p and q for qsort() //=========================================================================== //= Main program = //=========================================================================== void main(void) { char tempString[1024]; // Temporary string variable int i; // Loop counter // Load the series X N = 0; while(1) { scanf("%s", tempString); if (feof(stdin)) break; // Put value in array and increment array index X[N] = atof(tempString); N++; // Check if MAX_SIZE data values exceeded if (N >= MAX_SIZE) { printf("*** ERROR - greater than %d data values \n", MAX_SIZE); exit(1); } } // Sort the series X using ANSI C qsort() function qsort(X, N, sizeof(double), comp); // Output the sorted list for (i=0; i