Advertisement
Guest User

Untitled

a guest
Jan 6th, 2011
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.32 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4.  
  5. #define STARTING_FAMILIES 4
  6. #define GENERATIONS 5000000
  7.  
  8. void
  9. doAGeneration(int dads, int moms, int *boys, int *girls);
  10.  
  11. int
  12. main()
  13. {
  14.         int             dads, moms, boys, girls;
  15.         int             generation;
  16.         int             total_boys, total_girls;
  17.         float           girl_frac, girl_prop;
  18.  
  19.         /* initial conditions */
  20.         dads = moms = STARTING_FAMILIES;
  21.         boys = girls = total_boys = total_girls = 0;
  22.         srand(time(NULL));
  23.  
  24.         for (generation = 0; generation < GENERATIONS; generation ++) {
  25.                 doAGeneration(dads, moms, &boys, &girls);
  26.                 total_boys += boys;
  27.                 total_girls += girls;
  28.  
  29.                 /* commented out code - to test if follow-on generations affect results
  30.                         dads = boys;
  31.                         moms = girls;
  32.                         if ((moms == 0) || (dads == 0))
  33.                                 break;
  34.                 */
  35.  
  36.                 boys = girls = 0;
  37.         }
  38.  
  39.         /* calculate and print overall stats */
  40.         girl_frac =
  41.                 100 * (float) total_girls
  42.                 /
  43.                 ((float) total_girls + (float) total_boys);
  44.         girl_prop = (float) total_girls / (float) total_boys;
  45.  
  46.         printf("\npopulation %d\n", total_boys + total_girls);
  47.         printf("%d total boys,\n%d total girls,\n%f %%girls,\n%f girls/boy\n",
  48.                         total_boys, total_girls, girl_frac, girl_prop);
  49.  
  50.         return 0;
  51. }
  52.  
  53. void
  54. doAGeneration(int dads, int moms, int *boys, int *girls)
  55. {
  56.         int             families;
  57.         int             gender;
  58.         int             i;
  59.  
  60.         if (dads < moms)
  61.                 families = dads;
  62.         else
  63.                 families = moms;
  64.  
  65.         /* Generate children for each family */
  66.         for (i = 0; i < families; i++) {
  67.                 gender = 0;
  68.                 while (gender == 0) {
  69.                         gender = rand() % 2;
  70.                         if (gender == 0)
  71.                                 (*girls)++;
  72.                         else
  73.                                 (*boys)++;
  74.                 }
  75.         }
  76.         /* Debugging print
  77.                 printf("%d girls, %d boys generated\n", *girls, *boys);
  78.         */
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement