我在checkTotal()
功能遇到问题。
在第一次迭代中,它可以正确地对所有内容进行求和。
然而,在我的第二次迭代中,所累加的值并不准确,而且我不确定这些数字的来源。
int checkTotal(int numSchemes, int numComponents, int weight[numComponents][numSchemes], char **list){
int cCounter = 0;
int total = 0;
while (cCounter < numComponents){
total = total + weight[numSchemes][cCounter];
printf("%d\n", total);
cCounter++;
}
return total;
}
这是调用checkTotal
的函数void weightInput(int numSchemes, int numComponents, int weight[numComponents][numSchemes], char **list){
int sCounter = 0;
int cCounter = 0;
// iterating through the number of schemes
while (sCounter < numSchemes){
printf("\nMarking scheme #%d: \n", (sCounter+1));
while (cCounter < numComponents){
printf("\tenter %s's weight: ", list[cCounter]);
int theWeight;
scanf("%d", &theWeight);
weight[sCounter][cCounter] = theWeight;
cCounter++;
}
//printf("%d\n", weight[sCounter][cCounter]);
printf("THE TOTAL IS %d\n", checkTotal(sCounter, numComponents, weight, list));
sCounter++;
cCounter = 0;
}
}
该函数采用2d数组,遍历第二维,然后将所有int加起来以获得总数。
它成功地总结并在第一次迭代中返回总和,但对于我的第二次迭代,它现在就完成了。
如果我在第一次迭代时输入33,25,5,5,它将返回65。
对于我的第二次迭代,我会(10,5,10,25)但是当它应该是50时它会返回47.
如果有人可以提供帮助,我们将不胜感激。提前谢谢你,
答案 0 :(得分:1)
在我看来,逻辑可能会回到前面?
int weight[numComponents][numSchemes]
在签名行中,但随后使用。
访问它weight[numSchemes][cCounter]
cCounter和numSchemes应该是相反的吗?
答案 1 :(得分:0)
total = total + weight[numSchemes][cCounter];
应该是:
total = total + weight[numComponents][cCounter];
谢谢@ user3629249