Skip to content

Commit

Permalink
sol: lab5/inheritance
Browse files Browse the repository at this point in the history
  • Loading branch information
jfvillablanca committed Nov 4, 2023
1 parent 4faddc9 commit 6ec3583
Show file tree
Hide file tree
Showing 2 changed files with 138 additions and 0 deletions.
1 change: 1 addition & 0 deletions lab5/inheritance/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
https://cs50.harvard.edu/x/2023/labs/5/
137 changes: 137 additions & 0 deletions lab5/inheritance/inheritance.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
// Simulate genetic inheritance of blood type

#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

// Each person has two parents and two alleles
typedef struct person
{
struct person *parents[2];
char alleles[2];
} person;

const int GENERATIONS = 3;
const int INDENT_LENGTH = 4;

person *create_family(int generations);
void print_family(person *p, int generation);
void free_family(person *p);
char random_allele();

int main(void)
{
// Seed random number generator
srand(time(0));

// Create a new family with three generations
person *p = create_family(GENERATIONS);

// Print family tree of blood types
print_family(p, 0);

// Free memory
free_family(p);
}

// Create a new individual with `generations`
person *create_family(int generations)
{
person *new_person = malloc(sizeof(person));

// If there are still generations left to create
if (generations > 1)
{
// Create two new parents for current person by recursively calling create_family
person *parent0 = create_family(generations - 1);
person *parent1 = create_family(generations - 1);

new_person->parents[0] = parent0;
new_person->parents[1] = parent1;

new_person->alleles[0] = (rand() % 2 == 0) ? parent0->alleles[0] : parent0->alleles[1];
new_person->alleles[1] = (rand() % 2 == 0) ? parent1->alleles[0] : parent1->alleles[1];
}

// If there are no generations left to create
else
{
new_person->parents[0] = NULL;
new_person->parents[1] = NULL;
new_person->alleles[0] = random_allele();
new_person->alleles[1] = random_allele();
}

return new_person;
}

// Free `p` and all ancestors of `p`.
void free_family(person *p)
{
if (p == NULL)
{
return;
}

free_family(p->parents[0]);
free_family(p->parents[1]);

free(p);
}

// Print each family member and their alleles.
void print_family(person *p, int generation)
{
// Handle base case
if (p == NULL)
{
return;
}

// Print indentation
for (int i = 0; i < generation * INDENT_LENGTH; i++)
{
printf(" ");
}

// Print person
if (generation == 0)
{
printf("Child (Generation %i): blood type %c%c\n", generation, p->alleles[0], p->alleles[1]);
}
else if (generation == 1)
{
printf("Parent (Generation %i): blood type %c%c\n", generation, p->alleles[0], p->alleles[1]);
}
else
{
for (int i = 0; i < generation - 2; i++)
{
printf("Great-");
}
printf("Grandparent (Generation %i): blood type %c%c\n", generation, p->alleles[0], p->alleles[1]);
}

// Print parents of current generation
print_family(p->parents[0], generation + 1);
print_family(p->parents[1], generation + 1);
}

// Randomly chooses a blood type allele.
char random_allele()
{
int r = rand() % 3;
if (r == 0)
{
return 'A';
}
else if (r == 1)
{
return 'B';
}
else
{
return 'O';
}
}

0 comments on commit 6ec3583

Please sign in to comment.