#Tideman ProblemSet CS50

34 messages · Page 1 of 1 (latest)

tropic coveBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question run !howto ask.

plain phoenix
#

Looking...

misty plume
#

what are you struggling with exactly

#

do you have the problem text?

#

perhaps you got i and j the wrong way around (or winner/loser)

#

im not a massive fan of the record_preferences function even if it does pass

#

that inner loop looks like its doing a lot more effort than it should be

#

e.g. we can assume that if we find a i first then j must be after it

#

we don't need to go on to find j

#

assuming that every person ranks every candidate

#

but anyway your pairs list

#

I think its duplicating entries, which they might not want

#

actually no it shouldn't be

#

would need to see the problem text anyway

plain phoenix
livid kindle
#

Uninitialized variables.

#

The ranks array, and x and y in one of the functions. At least.

#

Also, pay attention to the fact that the first candidate ID is 0. If you compare it to a zero-initialized rank it'll match.

#

But all in all, the main problem with your code is adherence to initialization rules. On one hand you leave e.g. int ranks[candidate_count]; uninitialized, which means it's filled with unpredictable garbage values, but then you explicitly initialize the bool locked[MAX][MAX]; array although it's already implicitly zero-initialized because it's in file scope.

#

If you have local int variables in function scope and access them uninitialized (for example if (x < y)), you don't know what values are being compared.

snow trail
#

So, to fix some of the misunderstanding. I am a total beginner to coding, and so I might do some stupid errors. Also, I am not allowed to modify anything other than the implementation of those 4 bottom functions. If I recall correctly: vote, record_preferences, add_pairs, sort pairs, lock_pairs, print_pairs

#

I'm not allowed to change anything other than those. So a lot of the stuff was note implemented by me

snow trail
#

also, my main issue is locked pairs.

#

I can't solve that function

livid kindle
#

The issue stems partly from the values being garbage

snow trail
#

here is the context of the problem

snow trail
#

also, I am currently on my phone but I can tell you that I've fixed one of the issues in the adding pairs, which was a logical mistake from me, also because I didn't quite understand the problem correctly.

livid kindle
#

Compile with every possible warning and see what the compiler says; compile with sanitizers enabled

snow trail
#

i changed this

// Record pairs of candidates where one is preferred over the other
void add_pairs(void)
{
    for (int i = 0; i < candidate_count; i++)
    {
        for (int j = 0; j < candidate_count; j++)
        {
            if (preferences[i][j] > preferences[j][i])
            {
                pairs[0+pair_count].winner = i;
                pairs[0+pair_count].loser = j;
                pair_count++;
            }
        }
    }
    return;
}
snow trail
#

i changed this: if (preferences[i][j] > 0) to what you actually see there. Swapping j and i