Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CS50x Problem Set 5 - Inheritance allows inheriting both alleles from one parent #277

Open
BallyCode opened this issue Aug 21, 2024 · 1 comment

Comments

@BallyCode
Copy link

My initially incorrect code passed check50 with this:

        // TODO: Randomly assign current person's alleles based on the alleles of their parents
        aperson->alleles[0] = parent0->alleles[rand() % 2];
        aperson->alleles[1] = parent0->alleles[rand() % 2];

inheriting both alleles from parent0.

The second line should have been aperson->alleles[1] = parent1->alleles[rand() % 2];

I discovered the error when I noticed a person with an OO blood type had parents with OO and BB blood types in the printout of the family.

@BallyCode
Copy link
Author

I believe this code will correctly check that a person inherits from both parents but it should be carefully double-checked! It includes a few debugging printfs that are commented out.

Unfortunately, due to the random assignment of alleles, it requires multiple runs of inheritance in order to catch errors.

bool check_inheritance(person *p)
// Returns true if all persons are inheriting from both their parents. Returns false if error.
{
    if (p == NULL)
    {
        return true;
    }
    if (p->parents[0] != NULL) // no need to check for parents[1], since a person can't have only one parent
    {
        bool match = (p->alleles[0] == p->parents[0]->alleles[0] || p->alleles[0] == p->parents[0]->alleles[1]) &&
            (p->alleles[1] == p->parents[1]->alleles[0] || p->alleles[1] == p->parents[1]->alleles[1]);
        if (! match)
        {
            // printf("ERROR FOUND in person%p\n", &p);
            return false;
        }
        else
        {
            return true;
        }
    }
    if (check_inheritance(p->parents[0]) == true)
    {
        return check_inheritance(p->parents[1]);
    }
    else
    {
        return false;
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant