JoshuaForYou

Inheritance

Aug 17th, 2023
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.42 KB | None | 0 0
  1. // Simulate genetic inheritance of blood type
  2.  
  3. #include <stdbool.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <time.h>
  7.  
  8. // Each person has two parents and two alleles
  9. typedef struct person
  10. {
  11. struct person *parents[2];
  12. char alleles[2];
  13. }
  14. person;
  15.  
  16. const int GENERATIONS = 3;
  17. const int INDENT_LENGTH = 4;
  18.  
  19. person *create_family(int generations);
  20. void print_family(person *p, int generation);
  21. void free_family(person *p);
  22. char random_allele();
  23.  
  24. int main(void)
  25. {
  26. // Seed random number generator
  27. srand(time(0));
  28.  
  29. // Create a new family with three generations
  30. person *p = create_family(GENERATIONS);
  31.  
  32. // Print family tree of blood types
  33. print_family(p, 0);
  34.  
  35. // Free memory
  36. free_family(p);
  37. }
  38.  
  39. // Create a new individual with `generations`
  40. person *create_family(int generations)
  41. {
  42. //Allocate memory for new person
  43. person *p = malloc(sizeof(person));
  44.  
  45. // If there are still generations left to create
  46. if (generations > 1)
  47. {
  48. // Create two new parents for current person by recursively calling create_family
  49. person *parent0 = create_family(generations - 1);
  50. person *parent1 = create_family(generations - 1);
  51.  
  52. // TODO: Set parent pointers for current person
  53. p->parents[0] = parent0;
  54. p->parents[1] = parent1;
  55. // TODO: Randomly assign current person's alleles based on the alleles of their parents
  56. p->alleles[0] = parent0->alleles[rand() % 2];
  57. p->alleles[0] = parent1->alleles[rand() % 2];
  58. }
  59.  
  60. // If there are no generations left to create
  61. else
  62. {
  63. // TODO: Set parent pointers to NULL
  64. p->parents[0] = NULL;
  65. p->parents[1] = NULL;
  66.  
  67. // TODO: Randomly assign alleles
  68. p->alleles[0] = random_allele();
  69. p->alleles[1] = random_allele();
  70. }
  71. // TODO: Return newly created person
  72. return p;
  73. }
  74. // Free `p` and all ancestors of `p`.
  75. void free_family(person *p)
  76. {
  77. // TODO: Handle base case
  78. if(p == NULL)
  79. {
  80. return;
  81. }
  82. // TODO: Free parents recursively
  83. free_family(p->parents[0]);
  84. free_family(p->parents[1]);
  85. // TODO: Free child
  86. free(p);
  87. }
  88.  
  89. // Print each family member and their alleles.
  90. void print_family(person *p, int generation)
  91. {
  92. // Handle base case
  93. if (p == NULL)
  94. {
  95. return;
  96. }
  97.  
  98. // Print indentation
  99. for (int i = 0; i < generation * INDENT_LENGTH; i++)
  100. {
  101. printf(" ");
  102. }
  103.  
  104. // Print person
  105. if (generation == 0)
  106. {
  107. printf("Child (Generation %i): blood type %c%c\n", generation, p->alleles[0], p->alleles[1]);
  108. }
  109. else if (generation == 1)
  110. {
  111. printf("Parent (Generation %i): blood type %c%c\n", generation, p->alleles[0], p->alleles[1]);
  112. }
  113. else
  114. {
  115. for (int i = 0; i < generation - 2; i++)
  116. {
  117. printf("Great-");
  118. }
  119. printf("Grandparent (Generation %i): blood type %c%c\n", generation, p->alleles[0], p->alleles[1]);
  120. }
  121.  
  122. // Print parents of current generation
  123. print_family(p->parents[0], generation + 1);
  124. print_family(p->parents[1], generation + 1);
  125. }
  126.  
  127. // Randomly chooses a blood type allele.
  128. char random_allele()
  129. {
  130. int r = rand() % 3;
  131. if (r == 0)
  132. {
  133. return 'A';
  134. }
  135. else if (r == 1)
  136. {
  137. return 'B';
  138. }
  139. else
  140. {
  141. return 'O';
  142. }
  143. }
  144.  
Advertisement
Add Comment
Please, Sign In to add comment