// Print the winner of the election, if there is one
bool print_winner(void)
{
// TODO
float votes2win;
//Votes could be odd number so would need to be rounded
int rounded_votes;
votes2win = voter_count / 2;
rounded_votes = round(votes2win);
for (int i = 0; i < candidate_count; i++)
{
if (candidates[i].votes > rounded_votes)
{
printf("%s\n", candidates[i].name);
return true;
}
}
return false;
}
需要打印获胜者似乎无法找到此函数中的问题是函数还是我的代码中的其他内容?
在有 3 名选民的决选中,由于使用了
round
,此函数将仅打印拥有 3 票的候选人。利用 C 截断整数除法的事实。