 # practice Recursive Inheritance.c

``````// Create a new individual with `generations`
person *create_family(int generations)
{
// TODO: Allocate memory for new person
person *p = malloc(sizeof(person));
// Generation with parent data
if (generations > 1)
{
// TODO: Recursively create blood type histories for parents
p->parents = create_family(generations - 1);
p->parents = create_family(generations - 1);
// TODO: Randomly assign child alleles based on parents
p->alleles = p->parents->alleles[rand() % 2];
p->alleles = p->parents->alleles[rand() % 2];
}

// Generation without parent data
else
{
// TODO: Set parent pointers to NULL
p->parents = NULL;
p->parents = NULL;
// TODO: Randomly assign alleles
p->alleles = random_allele();
p->alleles = random_allele();
}

// TODO: Return newly created person
return p;
}
``````

Firstly, we pass 3 as a parameter into this function.

``````person *p = malloc(sizeof(person));
if (3 >1)
{
p->parents = create_family(3 - 1);
p->parents = create_family(3 - 1);
}
``````

Now, we got two function to do, and this time the parameter is 2
E

``````person *p = malloc(sizeof(person));
if (2 > 1)
{
p->parents = create_family(2 - 1); //A
p->parents = create_family(2 - 1); //B
}
``````

F

``````person *p = malloc(sizeof(person));
if (2 > 1)
{
p->parents = create_family(2 - 1); //C
p->parents = create_family(2 - 1); //D
}
``````

This time, we got 4 function to do. A

``````person *p = malloc(sizeof(person));
else
{
p->parents = NULL;
p->parents = NULL;

p->alleles = random_allele();
p->alleles = random_allele();
}
``````

B

``````person *p = malloc(sizeof(person));
else
{
p->parents = NULL;
p->parents = NULL;

p->alleles = random_allele();
p->alleles = random_allele();
}
``````

C

``````person *p = malloc(sizeof(person));
else
{
p->parents = NULL;
p->parents = NULL;

p->alleles = random_allele();
p->alleles = random_allele();
}
``````

D

``````person *p = malloc(sizeof(person));
else
{
p->parents = NULL;
p->parents = NULL;

p->alleles = random_allele();
p->alleles = random_allele();
}
``````

Now, we will continue the part after We get out of the recursive function.
E

``````        p->alleles = p->parents->alleles[rand() % 2]; //E->alleles from A
p->alleles = p->parents->alleles[rand() % 2]; //E->alleles from B
``````

F

``````        p->alleles = p->parents->alleles[rand() % 2]; //E->alleles from C
p->alleles = p->parents->alleles[rand() % 2]; //E->alleles from D
``````

and the last p which parents are E and F
G

``````        p->alleles = p->parents->alleles[rand() % 2]; //E->alleles from E
p->alleles = p->parents->alleles[rand() % 2]; //E->alleles from F
``````

And now, we got A, B, C, D, E, F, G, 7 person. Their relation would be like this:

``````// A    B    C    D
//   E         F
//        G
``````

and every time we get into the bottom of function, we always create a person

``````*p = malloc(sizeof(person))
``````

and we will return every person, at the end of the functions as well (7 person, 7 times)

``````    // TODO: Return newly created person
return p;
``````
#C #Recursive