# Tag Info

3

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 than array elements. Not sure how your lock_pairs is meant to work (maybe you could edit an explanation into your question?), again you are not using pair_count. ...

3

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 been set to anything. The difference between the debug50 result and the program is result is because the contents of the assigned memory addresses is different.

2

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 descending order.

2

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, 2. A->B. Here, first pair meets your condition for incrementing the value of count but there looks no harm in assigning true to this combination. Hint: ...

2

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 doing. The code is basically setting each cell of the 2 dimensional array to false. I believe it is doing this not to clear the array but rather to initialize it....

2

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, from my understanding, looping through pair_count results in out of bounds errors. For example, 5 candidates will generate more than 5 pairs (let's say 8 for ...

2

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 is a good way of solving this. But chaining two functions out of lock_pairs() is a bit redundant. Try and have just one makes_cycle(). What's the job of ...

1

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's answer that first. Cycles are created if there is at least one arrow pointed towards each candidate. Uhm, okay. It is easier said than done, right? How do we ...

1

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 languages (C, C++, Java, etc.), you know that you are dealing with an array. If you have an integer or integer variable (such as i) in the square brackets, then you ...

1

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]

1

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 in following the sorted order. So, for example, if candidate 2 beats 3, 3 beats 4, and 4 beats 2, you have a cycle (which lock_pairs should avoid), but ...

1

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); } } The issue is that when you encounter the first locked pair to test, you return the value of check_cycle on this pair, and your function is never ...

1

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 lock_pairs function, it is relying on the prototype to inform it as to how the check_winner function behaves (such as what sort of value it returns).

1

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 #define MAX 9 // preferences[i][j] is number of voters who prefer i over j int preferences[MAX][MAX]; // locked[i][j] means i is locked in over j bool locked[...

1

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 already locked.

1

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 a struct like this: typedef struct { string name; int votes; } candidate; Now, say that you have two structs, a and b, and they have been populated ...

1

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 multiple sources for the purposes of this assignment, possibly.

1

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 challenge in my view). What I found was that the scenarios of pairs and candidates that I had originally constructed were too simplistic. That is, my code was &...

1

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 adjacent in your algorithm. Try thinking really carefully about what two pairs you should be looking at an any one time.

1

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 means that the first parameter must be an int, and the var name rank is used inside the function. This should not be confused with vars anywhere else, such as in ...

1

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, there would have to already exist a path from B->A. So my code checks the nodes reachable from B, over any number of steps over already locked edges (avoiding ...

1

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 // pairs - struct -> int winner, loser // i & j -> based on candidates for (int i = 0; i < candidate_count; i++) { for (int j = i + ...

1

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. In the problem directions it says You should not modify anything else in tideman.c other than the implementations of the vote, record_preferences, ...

1

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't work like the CS50 people envisioned them. To solve this, you'll probably have to remove the extra strength property, and calculate that in the sort_pairs ...

1

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, the overall winner will be the winner of one of the first n pairs. Example: If there are 9 voters and 6 candidates 5 people vote A, B, C, D, E, F 4 people ...

1

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 print_winner functions (and the inclusion of additional header files, if you’d like). You are permitted to add additional functions to tideman.c, so long as you do ...

1

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.

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