/* A couple decides to have children. They decide to continue to have children until they have at least one boy and one girl. Then they decide to stop having children. This program simulates family size for 1000 couples. An asserion requires that the likelihood of boys is between 1/3 and 2/3. */ #include /* libraries for the pseudo-random number generator */ #include #include /* Within the time.h library, * time returns a value based upon the time of day * Within the stdlib.h library * rand returns a pseudo-random integer between 0 and RAND_MAX */ /* library for the assert function to check assertions/pre-conditions */ #include #define numberOfCouples 1000 /* procedure to simulate the number of children for one couple parameter fraction_boys: the percentage of boys born, expressed as a decimal fraction pre-condition: 0.33 <= fraction_boys <= 0.66 */ void simulate_couple (double fraction_boys, int * num_girls, int * max_girls, int * num_boys, int * max_boys ) { /* actively enforce the pre-condition */ assert ((0.33 <= fraction_boys) && (fraction_boys <= 0.66)); /* couple starts with no children */ int boys = 0; int girls = 0; /* couple has children */ while ((boys == 0) || (girls == 0)) { if ((((double) rand()) / ((double) RAND_MAX)) < fraction_boys) boys++; else girls++; } /* update records for girls and boys */ if (* num_girls == 0) { // this is first couple, so set all variables explicitly *num_girls = * max_girls = girls; *num_boys = * max_boys = boys; } else { // update variables, as appropriate *num_girls += girls; if (girls > *max_girls) *max_girls = girls; *num_boys += boys; if (boys > *max_boys) *max_boys = boys; } } /* procedure to conduct simulation for several couples parameter numCouples: the number of couples to be simulated parameter fraction_boys: the percentage of boys born, expressed as a decimal fraction */ void simulate_several_couples (int numCouples, double fraction_boys) { int couple; int total_girls = 0; int total_boys = 0; int max_girls, max_boys; for (couple = 1; couple < numCouples; couple++) { simulate_couple (fraction_boys, &total_girls, &max_girls, &total_boys, &max_boys); } double avg_children = ((double) (total_girls+total_boys)) / numCouples; printf (" %6.3lf %6.1lf %6d %6.1lf %6d %6.1lf\n", fraction_boys, (double) total_girls/numCouples, max_girls, (double) total_boys /numCouples, max_boys, avg_children ); } int main () { /* initialize pseudo-random number generator */ /* change the seed to the pseudo-random number generator, based on the time of day */ srand (time ((time_t *) 0) ); /* print initial title for program */ printf ("Simulation of family size with %d couples\n", numberOfCouples); /* print table heading */ printf ("fraction avg. max. avg max. avg. \n"); printf (" boys girls girls boys boys children\n"); /* run simulation for several couples */ double boy_fraction; for (boy_fraction = 0.35; boy_fraction <= 0.65; boy_fraction += 0.05) { simulate_several_couples (numberOfCouples, boy_fraction); } return 0; }