0

The way I have coded it, I believe my min should be set correctly, but when I try to run it, the check50 says it's wrong. I tested it by printing my min at various points in the code, and it is not givng the right min. Any advice why? Thanks!

// Return the minimum number of votes any remaining candidate has int find_min(void) { // TODO int min = voter_count; for (int i = 0; i < candidate_count; i++) { if (candidates[i].eliminated == false && candidates[i].votes < min) { min = candidates[i].votes; printf("min %s is %i\n", candidates[i].name, min); //just to see where going wrong } printf("final min is %i\n", min); //just to see where going wrong return min; } return 0; }

This is what I got when I ran the program, so clearly something is going wrong. ~/pset3/runoff/ $ ./runoff a b c Number of voters: 9 Rank 1: a Rank 2: b Rank 3: c

Rank 1: a Rank 2: c Rank 3: b

Rank 1: a Rank 2: b Rank 3: c

Rank 1: a Rank 2: b Rank 3: c

Rank 1: c Rank 2: b Rank 3: a

Rank 1: c Rank 2: a Rank 3: b

Rank 1: c Rank 2: a Rank 3: b

Rank 1: b Rank 2: c Rank 3: a

Rank 1: c Rank 2: b Rank 3: a

min a is 4 final min is 4 a b c

1 Answer 1

1

This routine always returns candidate 0's vote count (if not eleminated) or voter_count (the default value for min) if candidate 0 is eliminated because return min; is inside the for loop. return is the final line executed in any function.

1
  • Thanks! That was really helpful!!!
    – user29490
    Aug 17, 2020 at 22:01

You must log in to answer this question.

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