I've been working on plurality for hours now and I'm stuck with a "segmentaion error." So my approach to this problem was to sort the candidate objects in terms of votes and print the first object (which has the highest votes). If there were equal votes, I created another object array called equals[] and stored the two objects from candidates[] to that object. However, when I print this at the end, it returns a segmentation error after correctly printing out the results.

I know that the error lies somewhere in me printing too many things into equals[] but I don't understand how I can fix this.

Source code:



Looks like you might have fixed it already? Otherwise, can you tell me what inputs you used that generated a segfault.

I took your code and ran it in the IDE with debug50:

~/test/ $ debug50 plurality bob bill
Number of voters: 3
Vote: bob
Vote: bill
Vote: bob

Also tried a few other combinations which didn't segfault either.

  • Hello! The outputs that don't work are all those that are in ties. So, for example, if do bob and bill and put the number of voters as 4 with the following sequence: Bob, Bill, Bob, Bill, it will print out "Bob" then "Bill" then "segmentation error" May 11 '20 at 7:42

I found the problem. In equality (int), they are multiplying the number of candidate with the possible number of equality, for example: Joe Lewis Rob Bob Marie David: Possible Equality=5 and number of candidates=6. So int equality = 30. In your last for loop where you print the winners, you need to write it like this: for (int i = 0; i < (equals/candidate_count+1); i++) So that if you take the last example: equals=30 candidate_count=6 so when you divide them, you obtain 5, but you need plus 1 so that you can go to the last array.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .