//===================================================== file = as3_2sol.c ===== //= Program to generate simulated disk access counts for Web hits = //============================================================================= //= Notes: This is assignment #2 for CIS 4930 (Simulation) for Spring 1999 = //=---------------------------------------------------------------------------= //= Build: bcc32 as3_2sol.c = //=---------------------------------------------------------------------------= //= Execute: as3_2sol = //=---------------------------------------------------------------------------= //= History: KJC (12/16/98) - Shell created = //= KJC (01/13/98) - Solution written = //= KJC (01/19/98) - Solution fixed (removed non-ANSI itoa()) = //============================================================================= //----- Include files --------------------------------------------------------- #include // Needed for printf(), fopen(), and fclose() #include // Needed for assert() macro #include // Needed for exit(), rand(), and RAND_MAX //----- Constants ------------------------------------------------------------- #define OUT_FILE "out.txt" // Output file name #define TOLERANCE 1.0e-8 // Tolerance for numerical slop #define NUM_MEAS 7 // Number of measurements in "bins" #define NUM_SAMPLES 10000 // Number of of samples to simulate //----- Globals --------------------------------------------------------------- struct web_hit // Structure for web hit samples { int disk_access; // ** Number of disk accesses double ratio_observed; // ** Observed ratio (0 to 1) for this sample }; // Measured disk accesses struct web_hit size[NUM_MEAS] = { {0, (double) 1289 / 74888}, {1, (double) 10123 / 74888}, {2, (double) 45126 / 74888}, {3, (double) 12133 / 74888}, {4, (double) 5771 / 74888}, {5, (double) 344 / 74888}, {6, (double) 102 / 74888}}; //----- Function prototypes --------------------------------------------------- double rand_val(void); // Generate random value between 0.0 and 1.0 //===== Main program ========================================================== void main(void) { FILE *fp; // File pointer double dist_func[NUM_MEAS]; // The CDF for the print job samples double z; // Uniform random number from 0 to 1 int bin_num; // Measurement "bin" number int disk_count; // Final disk count for this sample char outstring[80]; // Temporary string variable int i; // Loop counter // Open the output file clobbering any existing file fp = fopen(OUT_FILE, "w"); if (fp == NULL) { printf(" >>> ERROR in creating output file (%s) \n", OUT_FILE); exit(1); } // Build and output the CDF for the observed measurements dist_func[0] = size[0].ratio_observed; printf("dist_func[0] = %f \n", dist_func[0]); for (i=1; i (1.0 - TOLERANCE)); assert(dist_func[NUM_MEAS - 1] < (1.0 + TOLERANCE)); // Generate NUM_SAMPLES web hits and output to outfile for (i=0; i