Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- #define STARTING_FAMILIES 4
- #define GENERATIONS 5000000
- void
- doAGeneration(int dads, int moms, int *boys, int *girls);
- int
- main()
- {
- int dads, moms, boys, girls;
- int generation;
- int total_boys, total_girls;
- float girl_frac, girl_prop;
- /* initial conditions */
- dads = moms = STARTING_FAMILIES;
- boys = girls = total_boys = total_girls = 0;
- srand(time(NULL));
- for (generation = 0; generation < GENERATIONS; generation ++) {
- doAGeneration(dads, moms, &boys, &girls);
- total_boys += boys;
- total_girls += girls;
- /* commented out code - to test if follow-on generations affect results
- dads = boys;
- moms = girls;
- if ((moms == 0) || (dads == 0))
- break;
- */
- boys = girls = 0;
- }
- /* calculate and print overall stats */
- girl_frac =
- 100 * (float) total_girls
- /
- ((float) total_girls + (float) total_boys);
- girl_prop = (float) total_girls / (float) total_boys;
- printf("\npopulation %d\n", total_boys + total_girls);
- printf("%d total boys,\n%d total girls,\n%f %%girls,\n%f girls/boy\n",
- total_boys, total_girls, girl_frac, girl_prop);
- return 0;
- }
- void
- doAGeneration(int dads, int moms, int *boys, int *girls)
- {
- int families;
- int gender;
- int i;
- if (dads < moms)
- families = dads;
- else
- families = moms;
- /* Generate children for each family */
- for (i = 0; i < families; i++) {
- gender = 0;
- while (gender == 0) {
- gender = rand() % 2;
- if (gender == 0)
- (*girls)++;
- else
- (*boys)++;
- }
- }
- /* Debugging print
- printf("%d girls, %d boys generated\n", *girls, *boys);
- */
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement