bool vote(string name)
{
// TODO
for (int i = 0; i < candidate_count; i++)
{
if (strcmp(candidates[i].name, name) == 0) //W's
{
candidates[i].votes++;
return true;
}
else if (strcmp(candidates[i + 1].name, name) == 0)
{
candidates[i].votes++;
return true;
}
else
{
return false;
}
}
return 0;
}
// Print the winner (or winners) of the election
void print_winner(void)
{
// TODO
for (int i = 0; i < candidate_count; i++)
{
if (candidates[0].votes > candidates[i].votes)
{
printf("%s", candidates[0].name); // 0 = bob/ first
break;
}
else
{
printf("%s\n", candidates[1].name);
}
}
}
I need to print the winner of the election but I cant seem to wrap my head around this, any help would be very appreciated, PLEASE DO NOT GIVE ME THE ANSWER I need the perspective so I can figure it out please don't just hand it to me, with that said I'll still need the answer dumbed down a little
Here are a few hints:
in the vote()
function, each candidate name should be tested until you find a match. The loop iterates on all candidates so it does not seem necessary to test more than one candidate at a time in the loop.
in the print_winner()
function, you should determine the best score then print all candidates with that score as there can be a tie. You can implement this with two separate loops.
To solve simple programming problems such as these, think of the steps you would take to solve the problem with pen and paper, decompose the steps into very basic tasks and translate these elementary steps into C code.