//====================================================== file = ztail.c ===== //= Program to compute positive (one-sided) tail values using Z score = //= - Hardcoded for 95%, 98%, and 99% values = //=========================================================================== //= 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) Minimum number of values should be 30 for a valid result = //= 4) Assumes population values, so variance is computed with N = //=-------------------------------------------------------------------------= //= Example "in.dat" file: = //= Input is values 1, 2, 3, ... 30 (one value per line) = //=-------------------------------------------------------------------------= //= Example output (for above "in.dat"): = //= ------------------------------------------------- ztail.c ----- = //= Total of 30 value = //= Mean = 15.500000 = //= Variance = 74.916667 = //= 95% tail value = 29.737336 = //= 98% tail value = 33.275680 = //= 99% tail value = 35.635153 = //= --------------------------------------------------------------- = //=-------------------------------------------------------------------------= //= Build: bcc32 ztail.c = //=-------------------------------------------------------------------------= //= Execute: ztail < in.dat = //=-------------------------------------------------------------------------= //= Author: Ken Christensen = //= University of South Florida = //= WWW: http://www.csee.usf.edu/~christen = //= Email: christen@csee.usf.edu = //=-------------------------------------------------------------------------= //= History: KJC (06/04/18) - Genesis (from ztest.c) = //=========================================================================== //----- Include files ------------------------------------------------------- #include // Needed for printf() #include // Needed for pow() and sqrt() #include // Needed for exit() and feof() #include // Needed for strcmp() //----- Defines ------------------------------------------------------------- #define MAX_VALUES 2000000 // Maximum number of values #define MIN_VALUES 30 // Minimum number of value (this is 30) #define Z_SCORE95 1.6449 // Z-score for 95% positive tail value #define Z_SCORE98 2.0537 // Z-score for 98% postive tail value #define Z_SCORE99 2.3263 // Z-score for 99% positive tail value //----- Globals ------------------------------------------------------------- double X[MAX_VALUES]; // Array of values read from "in.dat" int N; // Number of values //----- Function prototypes ------------------------------------------------- void load_X_array(void); // Load X array double compute_mean(void); // Compute mean double compute_var(double mean); // Compute variance //=========================================================================== //= Main program = //=========================================================================== void main(void) { double mean; // Mean of values double variance; // Variance of values double tail_value95; // Computed 95% positive tail value double tail_value98; // Computed 98% positive tail value double tail_value99; // Computed 99% positive tail value // Load the values into X and determine global variable N printf("------------------------------------------------- ztail.c -----\n"); load_X_array(); // Make sure than N is at least MIN_VALUES if (N < MIN_VALUES) { printf("*** ERROR - not enough value (minimum is %d) \n", MIN_VALUES); exit(1); } // Compute mean, variance, and tail values for X mean = compute_mean(); variance = compute_var(mean); tail_value95 = mean + (Z_SCORE95 * sqrt(variance)); tail_value98 = mean + (Z_SCORE98 * sqrt(variance)); tail_value99 = mean + (Z_SCORE99 * sqrt(variance)); // Output results printf(" Total of %d values \n", N); printf(" Mean = %f \n", mean); printf(" Variance = %f \n", variance); printf(" 95%% positive tail value = %f \n", tail_value95); printf(" 98%% positive tail value = %f \n", tail_value98); printf(" 99%% positive tail value = %f \n", tail_value99); 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_VALUES data values exceeded if (N > MAX_VALUES) { printf("*** ERROR - greater than %ld values \n", MAX_VALUES); exit(1); } } // End-of-file escape end: return; } //=========================================================================== //= Function to compute mean for a series X of N values = //=========================================================================== double compute_mean(void) { double mean; // Computed mean value int i; // Loop counter // Loop to compute mean mean = 0.0; for (i=0; i