I know it is not correct implementation for print_winner function (printf should be inside the if statment). However, I cannot figure out why printf prints names differently when placed outside if-statement?
If I place printf outside if statement, here is the code and output
Code:
// Print the winner (or winners) of the election
void print_winner(void)
{
int maximum = candidates[0].votes;
string winner = candidates[0].name;
for (int h = 0; h < candidate_count; h++)
{
if (candidates[h].votes > maximum)
{
maximum = candidates[h].votes;
}
}
for (int k = 0; k < candidate_count; k++)
{
if (candidates[k].votes == maximum)
{
winner = candidates[k].name;
}
printf("%s\n", winner);
Output:
~/pset3/plurality/ $ ./test sonya criss ben anbu
Number of voters: 4
Vote: ben
Vote: ben
Vote: anbu
Vote: anbu
sonya
sonya
ben
anbu
If I put printf inside if statement, here is the code and output:
Code:
// Print the winner (or winners) of the election
void print_winner(void)
{
int maximum = candidates[0].votes;
string winner = candidates[0].name;
//Find maximum votes
for (int h = 0; h < candidate_count; h++)
{
if (candidates[h].votes > maximum)
{
maximum = candidates[h].votes;
winner = candidates[h].name;
}
}
//Find candidate with maximum votes
for (int k = 0; k < candidate_count; k++)
{
if (candidates[k].votes == maximum)
{
winner = candidates[k].name;
printf("%s\n", winner);
}
}
return;
}
Output:
~/pset3/plurality/ $ ./plurality sonya criss ben anbu
Number of voters: 4
Vote: ben
Vote: ben
Vote: anbu
Vote: anbu
ben
anbu
in your first case the winner name you print is candidates[0].name
which is sonia until if (candidates[k].votes == maximum)
become true and you reassign winner , and you print candidate_count times because unconditionally in the loop.
In the second case you only print the name of the person whose votes number equals the maximum ( if (candidates[k].votes == maximum)
is true), so only ben and anbu having both 2 votes being the maximum.
Because several persons can have the same number of votes it is useless to have the variable winner , you can do
// Print the winner (or winners) of the election
void print_winner(void)
{
int maximum = candidates[0].votes; /* suppose candidate_count > 0, else initialize with -1 */
//Find maximum votes
for (int h = 1; h < candidate_count; h++) /* useless to redo at index 0 */
{
if (candidates[h].votes > maximum)
maximum = candidates[h].votes;
}
//Find candidate(s) with maximum votes
for (int k = 0; k < candidate_count; k++)
{
if (candidates[k].votes == maximum)
puts(candidates[k].name);
}
}
From your remark
Could you elaborate why in the first case winner name is printed
candidates[0].name
untilif (candidates[k].votes
becomes true
because you initialize winner with candidates[0].name;
doing:
string winner = candidates[0].name;
and you modify winner only when if (candidates[k].votes == maximum)
is true:
if (candidates[k].votes == maximum) { winner = candidates[k].name;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.