Can anyone help me with my printResults function?

In my program, if one of the candidates has more than 50% of the votes, the program prints that that person is the winner. If there isn't anyone with more than 50% of the votes, the program prints that a runoff is needed. In my function, it just keeps printing that they are all winners. Can anyone help me out please??!!

#include <stdio.h>
#include <stdlib.h>

//Prototypes
void totals(int votescast[][4], int candvotesums[], float votingpercentages[], int row, int col);
void printResults(int precinct[], int votescast[][4], int candvotesums[], float votingpercentages[]);

int main()
{
//Call statements and declarations
int castedvotes[5][4] = {{192, 48, 206, 37}, {147, 90, 312, 21}, {186, 12, 121, 38}, {114, 21, 408, 39}, {267, 13, 382, 29}};
int candidatevotesums[4];
float votepercentages[4];
int r = 0;
int c= 0;
int precinctarray[5] = {1,2,3,4,5};
totals(castedvotes, candidatevotesums, votepercentages, r, c);
printResults(precinctarray, castedvotes, candidatevotesums, votepercentages);
return 0;
}

//This function determines the sum of all the vote casts, the candidates' number of vote casts, and the percentage of the total votes cast each candidate received.
void totals(int votescast[][4], int candvotesums[], float votingpercentages[], int row, int col)
{
int alltotalvotes = 0;
for(row=0; row<5; row++)
{
for(col=0; col<4; col++)
{
alltotalvotes += votescast[row][col];
}
}

for(col=0; col<4; col++)
{
candvotesums[col] = 0;
for(row=0; row<5; row++)
{
candvotesums[col] += votescast[row][col];
}
}

for(col=0; col<4; col++)
{
votingpercentages[col] = 1;
for(row=0; row<4; row++)
{
votingpercentages[col] = ((float)candvotesums[col]/alltotalvotes)*100;
}
}
}

//This function prints the labeled tabel of votes cast in each precinct for each canidate, and prints the candidate totals and the percentage beneath the orginal table.
void printResults(int precinct[], int votescast[][4], int candvotesums[], float votingpercentages[])
{
int r;
int c;
char cand;
printf("Precincts Candidate A Candidate B Candidate C Candidate D\n");
for(r=0; r<5; r++)
{
printf("\n%d", precinct[r]);
for(c=0; c<4; c++)
{
printf(" \t%7d\t", votescast[r][c]);
}
}

printf("\n\nCandidate Sums: \n");
for(c=0; c<4; c++)
{
printf("\t%7d\t", candvotesums[c]);

}

printf("\nVoting Percentages:\n");
for(c=0; c<4; c++)
{
if(c==4)
{
printf("\n");
}
else if(c==0)
{
cand = 'A';
}
else if(c==1)
{
cand = 'B';
}
else if(c==2)
{
cand = 'C';
}
else if(c==3)
{
cand = 'D';
}

printf("Candidate %c has %3.2f percent of all votes.\n", cand, votingpercentages[c]);
if(votingpercentages[0] > 50)
{
printf("Candidate %c is the winner.\n", cand);
}
else if(votingpercentages[1] > 50)
{
printf("Candidate %c is the winner.\n", cand);
}
else if(votingpercentages[2] > 50)
{
printf("Candidate %c is the winner.\n", cand);
}
else if(votingpercentages[3] > 50)
{
printf("Candidate %c is the winner.\n", cand);
}
else
{
printf("There will be a runoff needed.\n");
}
}

}
The problem is that you're recomputing cand each time through the loop but using fixed indexes to compare the percentages each time.

Let's assume that candidate 3 is the winner with more than 50%.
First time thorugh the loop, the following code fires with cand = 'A'.
1
2
else if(votingpercentages[3] > 50)
{  printf("Candidate %c is the winner.\n", cand);

The second time through the loop the same code fires with cand = 'B'.
etc, etc.

Consider the following instead:
1
2
3
4
5
6
7
8
9
10
11
12
13
    int winner_found = 0;
...
    printf("\nVoting Percentages:\n");
    for(c=0; c<4; c++)
    {   cand = c + 'A';
        printf ("Candidate %c has %3.2f percent of all votes.\n", cand, votingpercentages[c]);
        if(votingpercentages[c] > 50)
        {   printf("Candidate %c is the winner.\n", cand);
            winner_found = 1;
        }		
    }
    if (! winner_found)
        printf ("There will be a runoff needed.\n");


PLEASE USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Last edited on
Topic archived. No new replies allowed.