1

I am having trouble with this code. In particular I can't seem to figure out why it is that the voteFractions() function doesn't work for me. It gets called appropriately, and all the correct parameters seem to reach the function, but I cannot get anything from "candidates[i].votes_fraction = candidates[i].votes/total;". All I ever get for candidates[i].votes_fraction is 0.00.

I tried running the program with NUMBER_OF_CANDIDATES = 1, and everything runs OK when that is the case, so I feel like I may be doing something silly, but I just can't seem to see it...

#define NUMBER_OF_CANDIDATES 10

typedef struct candidate{

    char name[20];
    int votes;
    float votes_fraction;

} candidate;


int totalVotes(candidate *candidates)
{
    int total = 0;
    for (int i = 0; i<NUMBER_OF_CANDIDATES; i++)
        total += candidates[i].votes;

    return total;
}


void voteFractions(candidate *candidates, int total, char *winner)
{
    float most_votes = 0, test;

    for (int i = 0; i<NUMBER_OF_CANDIDATES; i++)
    {
        candidates[i].votes_fraction = candidates[i].votes/total;

        if (candidates[i].votes_fraction > most_votes)
        {
            most_votes = candidates[i].votes_fraction;
            strcpy(winner, candidates[i].name);
        }
    }
}


int main()
{

    candidate candidates[NUMBER_OF_CANDIDATES];
    int total;
    char winner[20];

    for (int i = 0; i<NUMBER_OF_CANDIDATES; i++)
    {
        printf("Enter candidate's name and the number of votes received: ");
        scanf("%s %d", candidates[i].name, &candidates[i].votes);
    }

    total = totalVotes(candidates);
    voteFractions(candidates, total, winner);

    return 0;
}
1
  • this line: scanf("%s %d", candidates[i].name, &candidates[i].votes); may or may not actually input/convert/and set the two variables successfully. The proper way to write this line is to check the returned value (which this line is discarding) to assure that both fields are set. Most likely, the second (and later) passes through the loop fail because no allowance for newlines and other white space is included. suggest: if( 2 != scanf(" %s %d", candidates[i].name, &candidates[i].votes) ) { // handle error } Note the ' ' before the %s so white space is skipped Commented Nov 16, 2014 at 19:11

1 Answer 1

2

The problem is that in this statement

candidates[i].votes_fraction = candidates[i].votes/total;

expression

candidates[i].votes/total

uses integer arithmetic because the both operands have type int. As total is always greater than or equal to candidates[i].votes then the result of the division is equal to 0. You have to write

( float )candidates[i].votes/total

Take into account that variable test is declared but not used in function voteFractions. You may remove it.

void voteFractions(candidate *candidates, int total, char *winner)
{
    float most_votes = 0, test;
    //... 
Sign up to request clarification or add additional context in comments.

3 Comments

That makes perfect sense to me, except for the fact that I declared votes_fraction as a float already...wouldnt that have taken care of it?
@home_wrecker votes_fraction gets already a truncated value.
@home_wrecker: when the compiler is evaluating the division, it looks only at the two operands of the division; if they're both int, then the division will be done as int. It's only when the assignment is evaluated that it does the conversion from the RHS type (int) to the LHS type (float).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.