0

I am working on the print function for Runoff and I keep getting the response

"The winner is: (null)"

I guess at this point I can't tell if the tabulate function isn't working or if print isn't working. Does anyone see an obvious fault with this code?

// Print the winner of the election, if there is one
bool print_winner(void)
{
    int max=0; //holds the highest value found in candidates.vote

    for (int i=0;i<candidate_count; i++)
    {
        max=candidates[i].votes; //holds the highest value found in candidates.vote

        if(candidates[i].votes<candidates[i+1].votes)
        {
            max=candidates[i+1].votes;
        }
    }

    if(max>(voter_count*.5)).
        {
            printf("the winner is:%s\n", candidates[max].name);
            return true;
        }
    return false;
}

Thanks, Rob

EDIT. I have changed my function to accept a datastruct candidate for swaping usage and that appears to be going well. I am able to sort the candidates by vote count (verified by printf). The tie is still confounding me. If I have a clear winner, either unanimous or split but more then half, I print the winner is. If I have a tie, the program enters a loop that it doesn't come out of. printf tells me its reading the tie, but the if statement isn't in a loop. any thoughts here to help?

bool print_winner(void)
{
    candidate swap={0,0,false};
    int cc = candidate_count;

    for (int i=0;i<cc; i++)
    {
        for(int j=0; j<cc-1; j++)
        {
        if(candidates[j].votes>candidates[j+1].votes)
            {
            swap=candidates[j];
            candidates[j]=candidates[j+1];
            candidates[j+1]=swap;
            }
        }

    printf("%s ", candidates[i].name);
    printf("%i", candidates[i].votes);
    printf("\n");

    }

    if(candidates[cc-1].votes>(voter_count*.5))
    {
        printf("The winner is: %s\n",candidates[cc-1].name);
        printf("number of votes: %i\n,", candidates[cc-1].votes);
        return true;
    }
    if(candidates[cc-1].votes==candidates[cc-2].votes)
    {
        printf("tie");
        return false;
    }

    return false;

}

Thanks in advance for any help. Rob

0

Results of this code would be unpredictable. It looks like max holds the maximum number of votes, but then the code uses that maximum number of votes to select the candidate. Unfortunately, max doesn't contain the array index of the candidate that had the most votes.

In other words, say that candidate smith is in candidates[3] and was the highest vote getter with 35 votes. The code stores 35 in max and then tries to print candidates[35].name. Do you see the problem there?

BTW, what if there was a tie?

Also, if a library function call, like printf, doesn't appear to be working, you should assume that you've written something wrong. Library functions are well tested and have worked millions of times over. Finding a software bug in a standard library function is about as rare as finding a Unicorn or a mermaid! ;-)

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

| improve this answer | |
  • Makes total sense, now all i have to do is change it so it works. perhaps easier said then done. Thank you so much for the help! – Rob Watt Apr 28 at 22:59
  • Shouldn't be too difficult. You just need to store the array position in another variable.Of course, that doesn't deal with a tie. ;-) – Cliff B Apr 28 at 23:06
  • So you were right, tie now has me tripped up. I executed an insertion sort and that appear to be working. I am using print f and can see that everything is being ordered when there is a clear winner. when i enter in a tie scenario the program never stops running. if(candidates[cc-1].votes==candidates[cc-2].votes) { printf("tie"); return false; } I find it weird because this is not in a loop so i don't know why it is looping. Especially since my bubble sort (uses loops) is returning two of the same number just fine. if it isn't the winning number. – Rob Watt Apr 30 at 2:08
  • ill edit my above post with the new function at the bottom. – Rob Watt Apr 30 at 2:11

You must log in to answer this question.

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