//======================================================== file = dup.c ===== //= Program to check for duplicate entries in a series X of size N = //=========================================================================== //= Notes: = //= 1) Input from input file "in.dat" to stdin (see example below) = //= * Comments are bounded by "&" characters at the beginning and = //= end of the comment block = //= 2) Output is to stdout = //= 3) For every duplication of a value, one message is printed (thus = //= for three identical values, there will be two messages printed) = //= 4) If there are no duplicate values, a "no duplicates" message is = //= printed. = //=-------------------------------------------------------------------------= //= Example "in.dat" file: = //= = //= & Sample series of data which can be integers or reals. = //= There are 9 values in this file. & = //= 0.0 = //= 2.9 = //= 8.9 = //= 1.5 = //= 2.9 = //= 3.1 = //= 1.5 = //= 4.0 = //= 1.5 = //=-------------------------------------------------------------------------= //= Example output (for above "in.dat") = //= = //= ------------------------------------------------- dup.c ----- = //= 1.500000 is duplicated = //= 1.500000 is duplicated = //= 2.900000 is duplicated = //= ------------------------------------------------------------- = //=-------------------------------------------------------------------------= //= Build: gcc dup.c, bcc32 dup.c, cl dup.c = //=-------------------------------------------------------------------------= //= Execute: dup < in.dat = //=-------------------------------------------------------------------------= //= Author: Kenneth J. Christensen = //= University of South Florida = //= WWW: http://www.csee.usf.edu/~christen = //= Email: christen@csee.usf.edu = //=-------------------------------------------------------------------------= //= History: KJC (05/12/00) - Genesis = //=========================================================================== //----- Include files ------------------------------------------------------- #include // Needed for printf() and feof() #include // Needed for exit(), atof(), and qsort() #include // Needed for strcmp() //----- Defines ------------------------------------------------------------- #define MAX_SIZE 1000000L // Maximum size of time series data array //----- Globals ------------------------------------------------------------- double X[MAX_SIZE]; // Time series read from "in.dat" long int N; // Number of values in "in.dat" //----- Function prototypes ------------------------------------------------- void load_X_array(void); // Load X array int comp(const void *p, const void *q); // Compare p and q for qsort() //=========================================================================== //= Main program = //=========================================================================== void main(void) { long int i; // Loop counters long int dup_flag; // Duplicate value flag // Load the series X printf("------------------------------------------------- dup.c -----\n"); load_X_array(); // Sort the series X using ANSI C qsort() function qsort(X, N, sizeof(double), comp); // Do for all the values in X dup_flag = 0; for (i=0; i<(N - 1); i++) if (X[i] == X[i+1]) { printf(" %f is duplicated \n", X[i]); dup_flag = 1; } // Output a message if no duplicated values are found if (dup_flag == 0) printf(" No duplicated values found \n"); printf("-------------------------------------------------------------\n"); } //=========================================================================== //= Function to load X array from stdin and determine N = //=========================================================================== void load_X_array(void) { char temp_string[1024]; // Temporary string variable // Read all values into X N = 0; while(1) { scanf("%s", temp_string); if (feof(stdin)) goto end; // This handles a comment bounded by "&" symbols while (strcmp(temp_string, "&") == 0) { do { scanf("%s", temp_string); if (feof(stdin)) goto end; } while (strcmp(temp_string, "&") != 0); scanf("%s", temp_string); if (feof(stdin)) goto end; } // Enter value in array and increment array index X[N] = atof(temp_string); N++; // Check if MAX_SIZE data values exceeded if (N >= MAX_SIZE) { printf("*** ERROR - greater than %ld data values \n", MAX_SIZE); exit(1); } } // End-of-file escape end: return; } //=========================================================================== //= Function to compare two aruguments in an array of double = //= - Needed by qsort() = //=========================================================================== int comp(const void *p, const void *q) { double *ptr1 = (double *)(p); double *ptr2 = (double *)(q); if (*ptr1 < *ptr2) return -1; else if (*ptr1 == *ptr2) return 0; else return 1; }