Hot answers tagged

3 votes
Accepted

PSET3 Tideman: Lock_Pairs Help

This is one of those times when I would recommend starting over. Try and break it down in terms of what you need your function to do, and build some pseudocode first. Some pointers: First, recursion ...
user avatar
  • 940
3 votes

pset3, tideman, cs50 2020

add_pairs should increment pair_count. sort_pairs would access an element beyond the array length. If you have pair_count elements, you have pair_count-1 neighbour comparisons per round, one less ...
user avatar
  • 20.7k
3 votes
Accepted

Program and debug50 different results

Declaring the array here int matrix[candidate_count]; allocates the memory for it. But it does not initialize the contents. At the end of the for loop, there is a good chance that matrix[m] has not ...
user avatar
3 votes
Accepted

Pset3: Tideman sort_pairs doesn't work?

preferences[pairs[i].winner][pairs[i].loser] > preferences[pairs[i+1].winner][pairs[i+1].loser] This condition swaps the elements in ascending order while check50 checks for the pairs array for ...
user avatar
  • 66
2 votes

How do I implement lock_pair function correctly in Tideman?

pair[i].winner may be equal to pair[j].loser in many cases where pair[i].winner is not the end element in the chain. Consider voting of three candidates A, B, C where sorted pairs as follows: 1. B->C, ...
user avatar
  • 66
2 votes
Accepted

p_set3 why does the int main clear the graph of locked in pairs?

Interesting. I am working on the Tideman problem now and didn't notice this block until I read your question. Let's ignore the comment that states "clear graph..." and focus only on what the code is ...
user avatar
2 votes
Accepted

Failing add_pairs function check50

I solved it after few hours. Damn.. Silly me.. I just had to remove the two break;s void add_pairs(void) { // TODO // Preferences integers of stringed candidates // Candidates string /...
user avatar
  • 131
2 votes

lock_pairs skips final pair if it creates cycle lock_pairs did not correctly lock all non-cyclical pairs

Instead of doing if(cycle[i]) do if(cycle(pairs[i].winner)). It should fix your problem because you are just calling a random iterator instead of the index of your respective candidate node. Also, ...
user avatar
  • 21
2 votes

pset3, vote(); direct comparison failing but strcmp() working?

Review the week 3 lecture starting at the 40 minute mark. From the transcript And it turns out, for reasons we'll explore in more detail next week, it is not sufficient to do what we did before and ...
user avatar
2 votes
Accepted

PSET3 Tideman - What to do with the lock_pairs function?

Specification says that, The function should create the locked graph, adding all edges in decreasing order of victory strength so long as the edge would not create a cycle. How are cycles created? Let'...
user avatar
2 votes

pset3 tideman lock_pairs: why does "lock_pairs locks all pairs when no cycles" fail?

Being the loser of an already locked pair is not necessarily sufficient to create a cycle. If A beats B, and B beats C, there is no cycle, but your algorithm will not lock in B -> C if A -> B is ...
user avatar
  • 940
1 vote

Tideman - "lock_pairs skips final pair if it creates cycle" - Almost there!

Ok, I got the solution. I changed this code: return check_cycle(loser, i, winCycle, loseCycle); to bool check = check_cycle(loser, i, winCycle, loseCycle); if (check) return true;
user avatar
1 vote
Accepted

How to swap elements in "struct" data type?

It's not really clear what you mean by "swapping elements". I can't see why you'd change one element in a struct, but I can see how you might want to swap entire structs. Say that you have ...
user avatar
  • 64.2k
1 vote

Passing arrays into function in tideman

ranks[] means an array of values that has been named ranks; from the declaration, we can tell that the values will all be integers. Any time you see a name followed by square brackets in C-based ...
user avatar
1 vote
Accepted

Can't determine the problem with my tideman program - check50 fails at lock_pairs function, says I didn't correctly lock all non-cyclical pairs

Thanks to u/yeahIProgram on reddit for this answer: If you look at the second if statement in my cycleCheck function, it first asks whether the current loser passed to the function is the winner of ...
user avatar
1 vote
Accepted

pset 3 tideman print winner does not exist despite being declared

Have you compiled the program locally? The compiler should complain about this strlen(champions) because champions is declared an int array here int champions[pair_count]
user avatar
1 vote
Accepted

PSET 3 - Tideman: Lock Pairs Function

The logic of find_source won't really achieve the objective. For each candidate, if no one beats that candidate, it will return false without checking subsequent candidates. However, pairs are locked ...
user avatar
  • 940
1 vote
Accepted

Tideman - :( lock_pairs skips final pair if it creates cycle

The problem is subtle and just a small error in execution logic: for (int i = 0; i < candidate_count; i++) { if(locked[thisloser][i] == true) { return check_cycle(i, original_winner)...
user avatar
  • 940
1 vote
Accepted

Tideman: invalid operands to binary expression ('void' > and 'int')

Have a look at your function prototype declared at the top of the file, and compare it to the function declaration in the body of the code. Remember that when the compiler reaches that line in the ...
user avatar
  • 940
1 vote
Accepted

Segmentation fault tideman swap function

If anyone else find this thread and have the same problem, I'll post the code after I solved it: #include <cs50.h> #include <stdio.h> #include <string.h> // Max number of candidates ...
user avatar
1 vote

pset3 Tideman: checking for cycles

I found it unnecessary to check multiple branches to the same node the way I executed my lock cycles function. I was very surprised by this. Whether this is related to the fact the graph does not have ...
user avatar
1 vote
Accepted

Logical flaw in Problem Set 3 - Tideman?

I have had this exact same problem, but managed to resolve it tonight. If this is the only error you are getting, your code is fundamentally correct (as not locking middle pairs would be the greatest ...
user avatar
1 vote
Accepted

sort_pairs function not working as per check50

I think you're trying to use bubble sort, since you're swapping pairs[i] and pairs[j] if pairs[j] is stronger than pairs[i]. However, bubble sort always swaps adjacent elements, and i and j aren't ...
user avatar
  • 709
1 vote
Accepted

What's in the bool function parenthesis?

No such thing as a silly question. It just means that you haven't learned it yet. That's all. ;-) This is a function "signature". It defines what is expected to be passed to the function. int rank ...
user avatar
  • 64.2k
1 vote

PSET3 -Tideman - Locking Pairs

I don't exactly understand your logic, I found something that feels somewhat simpler in an answer on the new cs50 ed platform. The approach is: For a cycle to appear when you lock a new edge A->B, ...
user avatar
  • 20.7k
1 vote
Accepted

Pset3: Tideman - Sort_pairs function

check50 doesn't care about how you implemented add_pairs when it's testing sort_pairs, so it doesn't initialize the winsize property. You'll need to remove that property, and access it in sort_pairs. ...
user avatar
  • 709
1 vote
Accepted

Tideman sort_pairs function

I don't know for a fact because I don't know how the check50 testing works, but I'd bet it's because of that extra strength property. Your program works all together, but the individual functions don'...
user avatar
  • 709
1 vote
Accepted

Tideman, my tests work but check50 says there is an error

Your old code was going through the first n pairs, where n is the number of candidates, then checking if one of the winners of those pairs is the total winner. You can't assume that with n candidates, ...
user avatar
  • 709
1 vote
Accepted

pset3 tideman failed to compile

Can't see your code, but the spec says: You should not modify anything else in tideman.c other than the implementations of the vote, record_preferences, add_pairs, sort_pairs, lock_pairs, and ...
user avatar
  • 18.4k
1 vote
Accepted

tideman pset3 sort pairs - code doesn't work

For the selection sort, you have to reset biggest (and depending on your choice for the initial value, also loc) at the start of the outer loop.
user avatar
  • 20.7k

Only top scored, non community-wiki answers of a minimum length are eligible