//====================================================== file = delta.c ===== //= Program to output deltas between values for 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 = //=-------------------------------------------------------------------------= //= Example "in.dat" file: = //= = //= & Sample series of data which can be integers or reals. & = //= & There are 11 values in this file. & = //= 50 = //= 42 = //= 48 = //= 61 = //= 60 = //= 53 = //= 39 = //= 54 = //= 42 = //= 59 = //= 53 = //=-------------------------------------------------------------------------= //= Example output (for above "in.dat"): = //= = //= c:\work\delta < in.dat = //= ------------------------------------------------- delta.c ----- = //= -8.000000 = //= 6.000000 = //= 13.000000 = //= -1.000000 = //= -7.000000 = //= -14.000000 = //= 15.000000 = //= -12.000000 = //= 17.000000 = //= -6.000000 = //= --------------------------------------------------------------- = //=-------------------------------------------------------------------------= //= Build: bcc32 delta.c = //=-------------------------------------------------------------------------= //= Execute: summary1 < delta = //=-------------------------------------------------------------------------= //= Author: Ken Christensen = //= University of South Florida = //= WWW: http://www.csee.usf.edu/~christen = //= Email: christen@csee.usf.edu = //=-------------------------------------------------------------------------= //= History: KJC (05/26/18) - Genesis = //=========================================================================== //----- Include files ------------------------------------------------------- #include // Needed for printf() and feof() #include // Needed for exit() and atof() #include // Needed for strcmp() //----- Defines ------------------------------------------------------------- #define MAX_SIZE 10000000 // Maximum size of time series data array //----- Globals ------------------------------------------------------------- double *X; // Time series read from "in.dat" int N; // Number of values in "in.dat" //----- Function prototypes ------------------------------------------------- void load_X_array(void); // Load X array //=========================================================================== //= Main program = //=========================================================================== void main(void) { double delta; // Delta value int i; // Loop counter // Malloc space for X X = (double *) malloc(sizeof(double) * MAX_SIZE); if (X == NULL) { printf("*** ERROR - Could not malloc() enough space \n"); exit(1); } // Load the series X printf("------------------------------------------------- delta.c -----\n"); load_X_array(); // Loop to compute min, max, sum, first moment (mean), and second moment for (i=0; i<(N-1); i++) { delta = X[i+1] - X[i]; printf("%f \n", delta); } printf("---------------------------------------------------------------\n"); // Free the malloced memory free(X); } //=========================================================================== //= 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 (multiple comments are handled) if (strcmp(temp_string, "&") == 0) { while(1) { do { scanf("%s", temp_string); } while (strcmp(temp_string, "&") != 0); scanf("%s", temp_string); if (strcmp(temp_string, "&") != 0) break; } 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; }