Hot answers tagged

3

int voter_count, candidate_count; candidate candidates[candidate_count]; In C, you cannot define an array with a variable size at a global scope (even if you could, candidate_count does not have a value at compile time so memory could not be allocated appropriately for the array). You need to define the array with a fixed size (let's say 5 for the sake of ...


2

Interesting that check50 doesn't pick that up! There is a problem in find_min. That function should have no side-effects, ie should not change any data, only report a result. This line candidates[j].votes = candidates[i].votes; has the potential to change a candidates vote count; that would be bad. There may be other problems in the code, but this jumps off ...


2

You return prematurely. Simply finding a vote count smaller than the max is not enough. You need to keep going until you've looked at all the candidates, and then return the smallest vote count. // Return the minimum number of votes any remaining candidate has int find_min(void) { int minVote = MAX_VOTERS; for (int x = 0; x < candidate_count; x++) ...


1

"Is it implicit in the above code that if vote returns true then the vote function runs as normal and updates the preferences array?" Answer: No. The vote function runs and then the result is evaluated. If vote returns true, the function has already updated the preferences array (assuming, of course, it's been written correctly :)


1

From the spec: The array preferences[i] will represent all of the preferences for voter number i, and the integer preferences[i][j] here will store the index of the candidate who is the jth preference for voter `i. So j represents the preference (rank). In this program the ith index (which is analagous to the jthe index from the spec) represents the ...


1

If the second, or any odd-indexed candidate, is the winner print_winner will not detect that. Why? The i for loop in vote only processes the even indexes because i is incremented twice: once in the loop itself and once in the else clause. Incrementing the for variable within the loop is very often a bug waiting to happen. If you run the Usage Election but ...


1

This for (int j =0; j < candidate_count; j++) only ever tests the 1st candidate, because it returns from the if/else block. (And it doesn't care whether candidate has been eliminated!). This election will produce an invalid result: 4 candidates (a b c d), 8 voters whose first choices are a, a, b, b, b, b, c, d respectively. The rest of the ballot doesn't ...


1

All you have to do is add \n after %s. (No space in between). Thank @rassenguy


1

You should try resetting j to 0 every time the outer loop restarts. And you should also check if any other candidates are also eliminated to make sure that the right candidate's votes are incremented. If this helps, please check the tickmark.


1

The logic is that the votes for the loser in the current round are removed in the next round, so yes, as a practical matter, the 2nd place vote for the odd man out voter is counted. More generally stated, after removing all votes for eliminated candidates, the highest place vote by each voter is counted.


1

Just a couple of thoughts, not necessarily a conclusive list, but: What is your find_min function accomplishing? What is it returning? Might checking if a candidate is eliminated be useful in some of your functions? I would have a good look at the logic of the tabulate function (keep in mind point 2, and also consider your indexing logic).


1

An idea that might help: if you want to eliminate candidate i, then rather than storing their name, you could think about storing i itself in another variable.


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