Hot answers tagged

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

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


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

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


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

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: ...


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