I have to make a program which contains the function print_winner, which is supposed to find the candidate(s) with the most votes in an array of candidates and print their name(s). The candidate is a composite data type consisting of a string of the candidate's name as well as an int of their votes. Every time a vote goes toward a candidate, that candidate's number of votes increases by 1.
// Update vote totals given a new vote
bool vote(string name)
{
for(int i=0; i<candidate_count; i++)
{
if(strcmp(name,candidates[i].name)==0)
{
candidates[i].votes ++;
return true;
}
}
return false;
}
I have no problem making a program with print_winner() where there can only be one candidate. The problem comes when I have to code for the possibility that two or more winning candidates have the same amount of votes, in which case I have to print all of their names
I start by creating an array of variable x which is made up of a string and an int, same as a candidate
typedef struct
{
int votes;
string name;
}
winner;
winner x[candidate_count];
Then, I set all x variables in the array to have 0 votes (Writing this I think this step is unnecessary since the x variables don't have to have a value, let me know if I'm wrong).
for(int i=0; i<candidate_count; i++)
{
x[i].votes=candidates[0].votes=0;
x[i].name=candidates[0].name="None";
}
After that, I iterate along the candidates, comparing each candidate to the first x in the array. If that candidate has more votes than x, then I plug the name and number of votes of that candidate into the first x in the array. If there is a candidate that has the same number of votes as the first x in the array, I use iteration along x to plug the values of that candidate into the next x. If there is a third candidate with the same number of votes, the same happens, but the valuesare plugged into the third x in the array. The way I've coded this I hope the first x in the array never has fewer votes than any other x in the array.
for(int i=0; i<candidate_count; i++)
{
{
if(x[0].votes<candidates[i].votes)
{
x[0].votes=candidates[i].votes;
x[0].name=candidates[i].name;
}
else if(x[0].votes==candidates[i].votes)
{
for(int j=0; j<candidate_count-1; j++)
{
if(x[j].votes>x[j+1].votes)
{
x[j+1].votes=candidates[i].votes;
x[j+1].name=candidates[i].name;
}
}
}
}
}
Finally comes the printing part. I used another iteration along the x array, where I compare each x value(x[i]) the next x value(x[i+1]) in the array, and if they have the same number of votes and a different name, that means that I have different candidates who are both winners. Therefore, I print the name of x[i] and continue with the iteration. As soon as I run across an x[i] whose x[i+1] either has fewer votes or the same name(no two candidates can have the same name in this election), I print x[i] and break the loop.
for(int i=0; i<candidate_count; i++)
{
if (x[i].votes==x[i+1].votes && !(strcmp(x[i].name,x[i+1].name)==0))
{
printf("%s\n",x[i].name);
}
printf("%s\n",x[i].name);
break;
}
return;
But when I run the program with a tied election, it doesn't give the correct results:
$ ./plurality a b c Number of voters: 6 Vote: a Vote: a Vote: a Vote: b Vote: b Vote: b Winner: b
What do I do?
Supposing everything else is good with your code at check50...
As specs of plurality, you shouldn't modify the code save the
voteandprint_winnerfunctionsso my suggestion is to erase the following and its consequences:
One working approach to the
print_winnerfunction is to make sure to (all inside of this function):Look among candidates for the one with the most votes, saving the definite winner inside a variable. It really doesn't matter if there's a tie.
Print candidate with the most votes by calling the winner variable.
Look for among the OTHER candidates for those who have equal votes, if so print them.
Make sure not to reprint winners.
Note that my print winner function is like 20 lines long, so my suggestion is that you avoid complex or very long functions.