0

I've been trying to put the total income calculated for that particular day into an array. So that at the end so that I can later total all the values in the array for a grand total.

I've got 2 arrays so far that have the demand for pies and number of apples picked. To calculate the income from pies, apple trays and total income for that day I've put it into a for loop.

So far I've got this: (this is for inputting the calculated value in the array)

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();
}

and i'm using this code to sum the total of the array:

totalincome = total[1] + total[2] + ... + total[30];

Any help will be appreciated! :)

1
  • 1
    what is the question? Also you should be using a loop or a library function for the total Commented May 8, 2014 at 5:06

2 Answers 2

2

In C++ (almost all programming languages), array index starts at 0, not 1! Check out Zero-based numbering for more info.

Change it to

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

Or simply, to make your life much easier, use a loop:

totalincome = 0;
for (int i = 0; i < sizeof(total)/sizeof(total[0]); ++i)
     totalincome += total[i];
Sign up to request clarification or add additional context in comments.

Comments

1
totalincome = 0;
for (int i = 0; i < sizeof(total)/sizeof(total[0]); ++i)
     totalincome += total[i];

For static arrray, this will work. If the array is dynamically allocated or passed as a pointer, you have to keep track of the number of elements.

totalincome = 0;
for (int i = 0; i < numelements; ++i)
     totalincome += total[i];

You need to put totalincome out of the loop.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.