将计算值分配到浮点数组-c++中

Assign calculated value into a float array - c++

本文关键字:数组 -c++ 计算 分配      更新时间:2023-10-16

我一直在尝试将当天计算的总收入放入一个数组中。最后,我可以把数组中的所有值加在一起,得到一个总数。

到目前为止,我有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];

如有任何帮助,我们将不胜感激!:(

在C++(几乎所有编程语言(中,数组索引从0开始,而不是1查看基于零的编号以了解更多信息。

将其更改为

totalincome = total[0] + total[1] + ... + total[29];

或者简单地说,为了让你的生活更轻松,使用一个循环:

totalincome = 0;
for (int i = 0; i < sizeof(total)/sizeof(total[0]); ++i)
     totalincome += total[i];
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排除在循环之外。