0

I've narrowed it down to line 57

x->alleles[i]=x->parents[i]->alleles[0];

I don't understand why it's not working, any help would be appreciated. Here's the full create_family function:

 // Create a new individual with `generations`
person *create_family(int generations)
{
    // TODO: Allocate memory for new person
    person *x =malloc(sizeof(person));

// Generation with parent data
if (generations > 1)
{
    // TODO: Recursively create blood type histories for parents
    person *y= create_family(generations-1);

    // TODO: Randomly assign child alleles based on parents
    for (int i=0; i<2; i++)
    {
        int r= rand() % 2;
        if (r==0)
        {
            x->alleles[i]=x->parents[i]->alleles[0];
        }
        else
        {
            x->alleles[i]=x->parents[i]->alleles[1];
        }
    }

}

// Generation without parent data
else
{
    // TODO: Set parent pointers to NULL
    x->parents[0]=NULL;
    x->parents[1]=NULL;

    // TODO: Randomly assign alleles
    x->alleles[0]=random_allele();
    x->alleles[1]=random_allele();

}

// TODO: Return newly created person
return x;

}

1 Answer 1

1

The problem is actually in this lines of code that need to be changed: // Generation with parent data if (generations > 1) { // TODO: Recursively create blood type histories for parents person *y= create_family(generations-1); Here you create a person y, a random person y with no background, and no pointers saved in any way to it. In the line 57, you reference the parents variable, even though you didn't set it. Instead of person *y, you should make the parents pointers, something like this: x->parents[0] = create_family(generation - 1); x->parents[1]= create_family(generation - 1);

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .