# Tag Info

2

for (int i = 0; i < candidate_count; i++) { if (candidates[i].votes >= maxvote) { maxvote = candidates[i].votes; winner = candidates[i].name; printf("The winner is %s\n", winner); } } the logic of your code to get the max number of votes is correct, but your logic to get the winner is a ...

2

The error means a path that does not take the if branch or the else branch will not return a boolean, since the returns are embedded in those blocks. Think about this comment //if input name does not match with any name . What the program is doing is //if input name does not match this name . If program returns on the first non-matching name, it will not ...

1

Try an election with 3 candidates, A B C, and 6 voters, with the result A 3, B 1, C 2. The self-evident winner is A with 3 votes. The first print_winner function will return C. On the first iteration of i, winner_votes will be 3. However, on the second iteration, where i equals 1, it will only compare B to C, and winner_votes will be 2. Since the inner (j) ...

1

Try this election: 3 candidates: a, b, c and 2 voters who vote for c. It reports a and b as (tied) winners. That's because c's votes are not considered in the first for (int i = 0; i < voter_count; i++), because voter_count is 2 and candidate c is index 2. (Neither is candidate c considered in the second for (int i = 0; i < voter_count; i++), for the ...

1

https://cs50.stackexchange.com/a/38819/25714 Check this post on seg fault (plurality) that I have answered. If found to help be sure to tick the answer to keep other smiling too 😃

1

Your code is incrementing maxvote every time your if statement is true.. The goal is to get the max number of votes and see then check if who are the candidates that have the same number of votes to the max votes you got. int maxvote = 0; for (int i = 0; i < candidate_count; i++) { if (candidates[i].votes > 0) { ...

1

There is no data that will show you what check50 sees. That is because program violates this instruction from the spec: You should not modify anything else in plurality.c other than the implementations of the vote and print_winner functions (and the inclusion of additional header files, if you’d like). check50 uses the distro main function for testing.

1

strcpy will copy the characters from one string to another location. You specify winner.name as the target, but that one currently is "". The compiler would translate "" to a string (in this case consisting of just the null terminator) that is stored in some memory that is loaded before main runs, and is read-only (so you don't accidentally overwrite other ...

1

If there are 3 candidates, as in the example, what are the indexes of the candidates array? What is the maximum i that this for (int i = 0; i < candidate_count+1; i++) will try to process (remember, starting at 0)?

1

The return false; should be the last line of your function, after the loop, there is nothing to do in that else. Rationale: If you have a name match, you know the vote is valid. But if you have a mismatch, it might still be valid, matching another name. So only after the loop, if you haven't had a name match with any candidate, do you know the vote is ...

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