gpt4 book ai didi

c++ - 将计算值分配给 float 组 - C++

转载 作者:行者123 更新时间:2023-11-28 00:29:20 25 4
gpt4 key购买 nike

我一直在尝试将那天计算的总收入放入一个数组中。这样我就可以在最后将数组中的所有值合计为一个总计。

到目前为止,我有 2 个数组,它们具有对馅饼的需求和采摘的苹果数量。为了计算当天的馅饼收入、苹果托盘收入和总收入,我将其放入 for 循环中。

到目前为止我得到了这个:(这是为了在数组中输入计算值)

float 总数[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];

任何帮助将不胜感激! :)

最佳答案

在 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];

关于c++ - 将计算值分配给 float 组 - C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23533118/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com