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[0] = create_family(generations - 1);
        p->parents[1] = create_family(generations - 1);
        // TODO: Randomly assign child alleles based on parents
        p->alleles[0] = p->parents[0]->alleles[rand() % 2];
        p->alleles[1] = p->parents[1]->alleles[rand() % 2];
    }

    // Generation without parent data
    else
    {
        // TODO: Set parent pointers to NULL
        p->parents[0] = NULL;
        p->parents[1] = NULL;
        // TODO: Randomly assign alleles
        p->alleles[0] = random_allele();
        p->alleles[1] = 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[0] = create_family(3 - 1); 
         p->parents[1] = 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[0] = create_family(2 - 1); //A
     p->parents[1] = create_family(2 - 1); //B
}

F

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

This time, we got 4 function to do. A

person *p = malloc(sizeof(person));
else
{
    p->parents[0] = NULL;
    p->parents[1] = NULL;

    p->alleles[0] = random_allele();
    p->alleles[1] = random_allele();
}

B

person *p = malloc(sizeof(person));
else
{
    p->parents[0] = NULL;
    p->parents[1] = NULL;

    p->alleles[0] = random_allele();
    p->alleles[1] = random_allele();
}

C

person *p = malloc(sizeof(person));
else
{
    p->parents[0] = NULL;
    p->parents[1] = NULL;

    p->alleles[0] = random_allele();
    p->alleles[1] = random_allele();
}

D

person *p = malloc(sizeof(person));
else
{
    p->parents[0] = NULL;
    p->parents[1] = NULL;

    p->alleles[0] = random_allele();
    p->alleles[1] = random_allele();
}

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

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

F

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

and the last p which parents are E and F
G

        p->alleles[0] = p->parents[0]->alleles[rand() % 2]; //E->alleles[0] from E
        p->alleles[1] = p->parents[1]->alleles[rand() % 2]; //E->alleles[1] 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






你可能感興趣的文章

Leetcode 刷題 pattern - Breadth-First Search

Leetcode 刷題 pattern - Breadth-First Search

Debug Windows Service

Debug Windows Service

CMD 常用指令

CMD 常用指令






Comments