0

I have written the functions but I am not sure which one is wrong as I am not getting any output

Here is my code:

// Record preference if vote is valid
bool vote(int voter, int rank, string name)
{
    for (int i = 0; i < candidate_count; i++)
    {
    if (strcmp(name, candidates[i].name) == 0)
    {
        preferences[voter][rank] = i;
        return true;
    }
    }
    return false;
}

// Tabulate votes for non-eliminated candidates
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++;
    }
    }
    return;
}

// Print the winner of the election, if there is one
bool print_winner(void)
{
    for (int i = 0; i < candidate_count; i++)
    {
    if (candidates[i].votes >= voter_count/2)
    printf("%s\n", candidates[i].name);
    return 1;
    }
    return false;
}

// Return the minimum number of votes any remaining candidate has
int find_min(void)
{
    for (int i = 0; i < candidate_count; i++)
    {
        for (int j = 1; j < candidate_count -1; j++)
        {
            if(candidates[i].votes < candidates[j].votes)
            {
                int min = i;
            }
        }
    }
    return 0;
}

// Return true if the election is tied between all candidates, false otherwise
bool is_tie(int min)
{
    for (int i = 0 ; i < candidate_count ; i++)
    {
    if (candidates[i].votes != min)
    {
            return false;
    }
    }
    return true;

}

// Eliminate the candidate (or candidates) in last place
void eliminate(int min)
{
    for (int i = 0 ; i < candidate_count ; i++)
    {
        if (candidates[i].votes == min)
        {
            candidates[i].eliminated = 1;
        }
    }
    return;
}

In hindsight the min function is also wrong i feel as i need to return the minimum value so would changing the return 0 to return min do the task?

1

Just a couple of thoughts, not necessarily a conclusive list, but:

  1. What is your find_min function accomplishing? What is it returning?
  2. Might checking if a candidate is eliminated be useful in some of your functions?
  3. 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

You must log in to answer this question.

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