//====================================================== file = sample.c ===== //= A program to demonstrate the Central Limit Theorm (CLT) = //============================================================================ //= Notes: = //= 1) CLT says, "Let X1, X2, X3, ..., Xn be a sequence of n independent = //= and identically distributed random variables each having finite = //= values of expectation mu and variance sigma^2. The CLT states that = //= as the sample size n increases, the distribution of the sample = //= mean of these random variables approaches the normal distribution = //= with a mean mu and variance sigma^2 / n irrespective of the shape = //= of the original distribution." = //=--------------------------------------------------------------------------= //= Execution: sample = //=--------------------------------------------------------------------------= //= Build: Standard CSIM build = //=--------------------------------------------------------------------------= //= History: KJC (07/12/11) - Genesis = //============================================================================ //----- Includes ------------------------------------------------------------- #include "csim.h" // Needed for CSIM stuff #include // Needed for printf() #include // Needed for exit() //----- Constants ------------------------------------------------------------ #define MAX 1000000 // Number of samples #define SAMPLE 10 // Sample size #define DIST 2 // 1 = uniform and 2 = exponential (mean = 5.0) //============================================================================ //== Main program == //============================================================================ void sim(void) { TABLE tb1, tb2; // CSIM tables double z; // Random variable double *x1, *x2; // Arrays for random values and sampled values double sum; // Temporary sum variable for sampling int i, j, k; // Counter indexes // Malloc space from the heap for x1 and x2 arrays x1 = (double *) malloc(sizeof(double) * MAX); x2 = (double *) malloc(sizeof(double) * MAX); if ((x1 == NULL) || (x2 == NULL)) { printf("*** ERROR - malloc() failed! \n"); exit(1); } // Initialize CSIM tables (set-up for a mean of 5.0) tb1 = table("Table of original values"); table_histogram(tb1, 40, 0.0, 20.0); tb2 = table("Table of sampled values"); table_histogram(tb2, 40, 0.0, 20.0); // Generate values (with mean = 5.0) and put them into x1 for (i=0; i