我一直试图将为该特定日期计算的总收入计入阵列。因此,最后我可以将数组中的所有值总计为总计。
到目前为止,我有2个阵列需要馅饼和挑选的苹果数量。为了计算那天馅饼,苹果托盘和总收入的收入,我把它放到了for循环中。
到目前为止,我已经得到了这个:(这是为了输入数组中的计算值)
float total[30];
int i, incmPie, numPie, rApples, applesLeft, FTray, incmFTray, PFTray;
float totalincm, incmApples, incmRApples, incmPFTray, totalincome;
**float total[30];**
int pieDemand[30]={4, 4, 2, 7, 1, 6, 7, 8, 9, 12, 2,13,13, 5, 3, 9, 9, 9, 8, 7,
12, 1, 3, 3,10,12, 3, 6, 9, 3};
int applesPicked[30]={330,123,110,245,321,999,0,100,77,89,100,200,300,390,700,20,701,6,800,90,
600,45,690,700,719,790,800,1000,66,666};
int date[30] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30};
printf("\n==============================================================================");
printf("\n Date Income from Pie Income from Apples Total income");
printf("\n==============================================================================");
for (i = 0 ; i <30; i++)
{
if (applesPicked[i] == 0)
{
incmPie = 0;
incmApples = 0;
totalincm = 0;
**total[i] = totalincm;**
}
else if (applesPicked[i] < (pieDemand[i]*8))
{
numPie = applesPicked[i]/8;
incmPie = numPie * 14;
rApples = applesPicked[i]%8;
incmRApples = rApples * 0.5;
incmApples = incmRApples;
totalincm = incmPie + incmRApples;
**total[i] = totalincm;**
}
else
{
incmPie = pieDemand[i] * 14;
applesLeft = applesPicked[i] - (pieDemand[i]*8);
FTray = applesLeft/20;
incmFTray = FTray * 12;
PFTray = applesLeft%20;
incmPFTray = PFTray * 0.5;
incmApples = incmFTray + incmPFTray;
totalincm = incmApples + incmPie;
**total[i] = totalincm;**
}
**totalincome** = total[1] + total[2] + total[3] + total[4] + total[5] + total[6] + total[7] + total[8] + total[9] + total[10] + total[11] + total[12] + total[13] + total[14] + total[15] + total[16] + total[17] + total[18] + total[19] + total[20] + total[21] + total[22] + total[23] + total[24] + total[25] + total[26] + total[27] + total[28] + total[29] + total[30];
printf("\n"); //prints onto the next line.
printf("%d/04/2013",date[i]); // prints the date.
printf("%15d", incmPie); // prints the income from pies for each value in the arrays.
printf("%20g", incmApples); // prints the income from apples from both full trays and remaining apples for each value in the arrays.
printf("%28g", totalincm);
}
printf("\n==============================================================================");
**printf("\n Total income for the entire month: $%g", totalincome);**
printf("\n------------------------------------------------------------------------------");
_getch();
}
并且我使用此代码来汇总数组的总和:
totalincome = total[1] + total[2] + ... + total[30];
任何帮助将不胜感激! :)
答案 0 :(得分:2)
在C ++(几乎所有编程语言)中,数组索引从0开始,而不是1!查看Zero-based numbering以获取更多信息。
将其更改为
totalincome = total[0] + total[1] + ... + total[29];
或者简单地说,为了让您的生活更轻松,请使用循环:
totalincome = 0;
for (int i = 0; i < sizeof(total)/sizeof(total[0]); ++i)
totalincome += total[i];
答案 1 :(得分:1)
totalincome = 0;
for (int i = 0; i < sizeof(total)/sizeof(total[0]); ++i)
totalincome += total[i];
对于静态arrray,这将起作用。如果数组是动态分配或作为指针传递的,则必须跟踪元素的数量。
totalincome = 0;
for (int i = 0; i < numelements; ++i)
totalincome += total[i];
您需要将totalincome
放在循环之外。