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

validateLock() function issue in tideman.c of pset3 #5

Open
DeWill404 opened this issue Mar 18, 2021 · 0 comments
Open

validateLock() function issue in tideman.c of pset3 #5

DeWill404 opened this issue Mar 18, 2021 · 0 comments

Comments

@DeWill404
Copy link

DeWill404 commented Mar 18, 2021

Hello DeWill here

I was solving pset3 of CS50 and got stuck at tideman problem of pset3,
I browsed on internet for some help and came to your solution(which helped me a lot to understand problem)....

While going through your code, I thought validateLock() function of your code might be incorrect( also I might be wrong 😄 ),
you are going through locked array linearly(like 1 row at a time), instead you should search alternatively....
I mean, consider pair (1,2) (2,3) (3,1)
the way you search is is,
first comparing 1 to 1,2,3
then 2 to 1,2,3
then 3 to 1,2,3
instead you should search as
first 1 to 2, then 2 to 3, and lastly 3 to 1

here is the solution that i came up with,

void validateLock(int x, int winner, int loser)
{
    for (int i = 0; i < candidate_count; i++)
    {
        if (lock == false)
        {
            return;
        }
        if (locked[x][i] == true)
        {
            if (x == winner && i == loser)
            {
                lock = false;
                return;
            }
            validateLock(i, winner, loser);
        }
    }
}

void lock_pairs(void)
{
    for (int i = 0; i < pair_count; i++)
    {
        locked[pairs[i].winner][pairs[i].loser] = true;

        validateLock(pairs[i].loser, pairs[i].winner, pairs[i].loser);
        // if the validateLock function found a cycle we reverse the lock
        if (!lock)
        {
            locked[pairs[i].winner][pairs[i].loser] = false;
        }
        lock = true;
    }
}

here i keep referance of staring pair as winner & loser
and start searching from one step ahead....

As I previously said, I might be wrong 😅 ,
btw thanks for posting solution publicly, I helped me a lot....

with regards DeWill

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