0

Finally took a break for a few days to think about stuff and I am back at doing psets

Now I am trying to fix the tabulate function, here's my code -

void tabulate(void)
{
    for (int i = 0; i < candidate_count; i++)
    {
    for (int j = 1; j < candidate_count - 1; j++ )
    if (strcmp(candidates[preferences[i][0]].name, candidates[j].name) == 0)
    {
        candidates[i].votes++;
    }
    }
    string min;
    for (int i = 0; i < candidate_count; i++)
    {
        for (int j = 1; j < candidate_count -1; j++)
        {
            if(candidates[i].votes < candidates[j].votes)
            {
                min = candidates[i].name;
            }
        }
        candidates[i].eliminated = 1;
    }
    
    return;
}

I believe the first part of my code should behave correctly where I increment votes however the second part where I eliminate candidates doesn't seem very right.I am not sure why but like David mentioned it smells bad. A reason I can think of is because I am eliminating the candidate in every iteration of the outer for loop which is definitely incorrect however I can't think of anything else as once i find the min string and I want to choose it to be eliminated I need to do it inside the loop as then afterwards outside the loop I cannot call the variable i.

Any suggestions or hints?

1 Answer 1

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.

1

You must log in to answer this question.

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