I am having trouble with this code. In particular I can't seem to figure out why it is that the voteFractions() function doesn't work for me. It gets called appropriately, and all the correct parameters seem to reach the function, but I cannot get anything from "candidates[i].votes_fraction = candidates[i].votes/total;". All I ever get for candidates[i].votes_fraction is 0.00.
我在使用此代码时遇到问题。特别是我似乎无法弄清楚为什么voteFractions()函数对我不起作用。它被恰当地调用,并且所有正确的参数似乎都达到了函数,但我无法从“candidate [i] .votes_fraction = candidates [i] .votes / total;”获得任何结果。我得到的所有候选人[i] .votes_fraction是0.00。
I tried running the program with NUMBER_OF_CANDIDATES = 1, and everything runs OK when that is the case, so I feel like I may be doing something silly, but I just can't seem to see it...
我尝试用NUMBER_OF_CANDIDATES = 1运行程序,一切都运行正常,所以我觉得我可能做的事情很傻,但我似乎无法看到它......
#define NUMBER_OF_CANDIDATES 10
typedef struct candidate{
char name[20];
int votes;
float votes_fraction;
} candidate;
int totalVotes(candidate *candidates)
{
int total = 0;
for (int i = 0; i<NUMBER_OF_CANDIDATES; i++)
total += candidates[i].votes;
return total;
}
void voteFractions(candidate *candidates, int total, char *winner)
{
float most_votes = 0, test;
for (int i = 0; i<NUMBER_OF_CANDIDATES; i++)
{
candidates[i].votes_fraction = candidates[i].votes/total;
if (candidates[i].votes_fraction > most_votes)
{
most_votes = candidates[i].votes_fraction;
strcpy(winner, candidates[i].name);
}
}
}
int main()
{
candidate candidates[NUMBER_OF_CANDIDATES];
int total;
char winner[20];
for (int i = 0; i<NUMBER_OF_CANDIDATES; i++)
{
printf("Enter candidate's name and the number of votes received: ");
scanf("%s %d", candidates[i].name, &candidates[i].votes);
}
total = totalVotes(candidates);
voteFractions(candidates, total, winner);
return 0;
}
1 个解决方案
#1
2
The problem is that in this statement
问题是在这个声明中
candidates[i].votes_fraction = candidates[i].votes/total;
expression
表达
candidates[i].votes/total
uses integer arithmetic because the both operands have type int. As total is always greater than or equal to candidates[i].votes then the result of the division is equal to 0. You have to write
使用整数运算,因为两个操作数的类型都是int。由于总数总是大于或等于候选[i] .votes,因此除法的结果等于0.你必须写
( float )candidates[i].votes/total
Take into account that variable test
is declared but not used in function voteFractions
. You may remove it.
考虑到变量测试已声明但未在函数voteFractions中使用。你可以删除它。
void voteFractions(candidate *candidates, int total, char *winner)
{
float most_votes = 0, test;
//...
#1
2
The problem is that in this statement
问题是在这个声明中
candidates[i].votes_fraction = candidates[i].votes/total;
expression
表达
candidates[i].votes/total
uses integer arithmetic because the both operands have type int. As total is always greater than or equal to candidates[i].votes then the result of the division is equal to 0. You have to write
使用整数运算,因为两个操作数的类型都是int。由于总数总是大于或等于候选[i] .votes,因此除法的结果等于0.你必须写
( float )candidates[i].votes/total
Take into account that variable test
is declared but not used in function voteFractions
. You may remove it.
考虑到变量测试已声明但未在函数voteFractions中使用。你可以删除它。
void voteFractions(candidate *candidates, int total, char *winner)
{
float most_votes = 0, test;
//...