- #1
sandy.bridge
- 798
- 1
Homework Statement
This question is rather easy. It is compiled online with CodeLab. For some reason my program executes perfectly except for one of the trials. Here is the question:
Given a type Money that is a structured type with two int fields, dollars and cents. Assume that an array named monthlySales with 12 elements, each of type Money has been declared and initialized.
Assume that a Money variable yearlySales has also been declared. Write the necessary code that traverses the monthlySales array and adds it all up and stores the resulting total in yearlySales. Be sure make sure that yearlySales ends up with a valid value, i.e. a value of cents that is less than 100.
The Attempt at a Solution
My solution:
PHP:
int j = 0;
yearlySales.cents = 0; yearlySales.dollars = 0;
while (j < 12)
{
yearlySales.cents += monthlySales[j].cents;
yearlySales.dollars += monthlySales[j].dollars;
if(yearlySales.cents >= 100){yearlySales.dollars++; yearlySales.cents -= 100;}
j++;
}
Can anyone see what is not correct about this code?