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.
34 messages · Page 1 of 1 (latest)
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.
Looking...
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
☝️ what do you enter to give you these results?
https://submit.cs50.io/check50/dbbe3b6cf0fc5d77216c44dfbd70ab9bedade201
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.
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
this one statement I don't quite understand, but I know this is my mistake since that is one of my lines of code. Would you mind giving me some more insight?
also, my main issue is locked pairs.
I can't solve that function
The issue stems partly from the values being garbage
here is the context of the problem
so how should I go about it, given my circumstances
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.
Compile with every possible warning and see what the compiler says; compile with sanitizers enabled
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;
}
Sanitizers?
i changed this: if (preferences[i][j] > 0) to what you actually see there. Swapping j and i