0

please help.

Errors:

:( find_min returns minimum when all candidates are tied find_min did not identify correct minimum

:( find_min ignores eliminated candidates find_min did not identify correct minimum

I've tried debug50 and using printf to print min and in my tests my code does find the minimum vote value for remaining candidates but check50 returns the errors above.

My code:

int find_min(void)
{
    for (int i = 0; i < candidate_count; i++)
    {
        for (int j = 0; j < candidate_count; j++)
        {
            if (candidates[j].votes == i)
            {
                if (!candidates[j].eliminated)
                {
                    return i;
                }
            }
        }
    }
    return 0;
}

Thanks for you help!

1 Answer 1

0

It's entirely possible that you are using a small test data set that produces success, but doesn't have scenarios where your code would fail. The lesson here is to develop test data that fully exercises code, including all the corner cases.

Frankly, I don't understand the logic behind that code at all. It compares the number of votes to the index of the candidate??? Why????

Also, nested for loops??? Not needed and doesn't work. But, if you insist on it, the comparison needs to be between candidate[i] and candidate[j], not i and candidate[j].

This can be done in one pass through the array to find the lowest vote count(s), followed by another for loop to eliminate the losers. The pseudocode is simple.

  1. Create a var to hold the minimum number of votes
  2. Store the first candidate's vote count in that var.
  3. compare the stored value to the next candidate. If the next candidate has lower votes, update the var from step 1.
  4. repeat for the rest of the candidates.
  5. Now, go back through the list with a second loop (not nested) and mark anyone with that vote count as eliminated.

Nested loops are not always the answer.

Finally, be careful about the placement of a return statement. In this case, as soon as a candidate is eliminated, the return will execute. What if there's a candidate with a lower count later on? Or, what happens when there are so many voters that every candidate gets more votes than the total number of candidates (greater than the max values of i and j)?

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

6
  • Thanks for your help. Your pseudocode worked. I worked out the error in my code by replacing the candidate_count in the first loop to voter_count and now everything works fine. Just to explain the logic in my code: variable i starts at 0 and if any candidate[j] (that hasn't been eliminated) has the variable i then they will have the minimum else i++ and the loops continue. The function only requires the minimum vote so it doesn't matter if another candidate has the same minimum vote as that will be dealt with later in the is_tie(min) and eliminate(min) functions.
    – colliedawg
    Commented Oct 13, 2021 at 1:49
  • Can you please explain why you don't advise using nested for loops? We learn't them in week 1 and I couldn't solve the tabulate() function without them (could you?). Thanks.
    – colliedawg
    Commented Oct 13, 2021 at 1:53
  • NESTED for loops are a useful tool, but would you use a hammer to loosen a screw? In this case, it's not the right tool. Also, there's a saying in programming that I tell myself on every project - "KISS - Keep It Simple, Stupid!" It means using code that's more complicated than necessary leads to bugs, so use the simplest code necessary to accomplish the goal. In this case, it's two NON-NESTED for loops! The first loop to find the highest number of votes and the second to identify who has that many votes.
    – Cliff B
    Commented Oct 13, 2021 at 2:02
  • Next, consider the efficiency. Nested loops will have an O(n) efficiency of n squared. Two consecutive, non-nested for loops will have an efficiency of 2n. The latter is far more efficient. It's all about using the best tool or code for the job. Here, nesting loops can work, but there's a better, simpler solution.
    – Cliff B
    Commented Oct 13, 2021 at 2:04
  • Thanks for explaining. It makes sense. I’ll try and keep this in mind for future code.
    – colliedawg
    Commented Oct 14, 2021 at 6:26

You must log in to answer this question.

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