Debugging a Program to Add Up Monthly Sales

  • Context: Comp Sci 
  • Thread starter Thread starter sandy.bridge
  • Start date Start date
  • Tags Tags
    Debugging Program
Click For Summary

Discussion Overview

The discussion revolves around debugging a program designed to sum monthly sales represented by a structured type called Money, which contains fields for dollars and cents. Participants are examining the logic of the code that aggregates these values and ensures that the total is valid, particularly focusing on how to handle cases where cents exceed 100.

Discussion Character

  • Homework-related
  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant presents an initial code attempt for summing the monthly sales and asks for feedback on its correctness.
  • Another participant suggests a correction regarding how to handle cases where the total cents exceed 100, proposing to adjust both dollars and cents accordingly.
  • A later reply acknowledges the correction and reflects on the oversight regarding the handling of cents, indicating a misunderstanding of the original code's logic.
  • There is a discussion about the validity of having a single entry with over 100 cents, with some participants questioning the practicality of such a scenario.

Areas of Agreement / Disagreement

Participants do not reach a consensus on the original code's correctness, as there are differing interpretations of how to handle the cents accumulation. Some agree on the need for adjustments, while others reflect on potential misunderstandings.

Contextual Notes

There are unresolved assumptions regarding the input values for cents and the implications of having values that exceed 100 cents in a single entry.

sandy.bridge
Messages
797
Reaction score
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?
 
Physics news on Phys.org
yearlySales.cents -= 100
 
Hey phinds! I suppose that makes sense! I didn't really take into consideration if they gave, let's say, 200 cents. Thanks!

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; yearlySales.cents = yearlySales.cents%100;}

j++;
}
 
Last edited:
sandy.bridge said:
Hey phinds! I suppose that makes sense! I didn't really take into consideration if they gave, let's say, 200 cents. Thanks!

No, you are missing the point.

EDIT: OOPS ... *I* am the one making a mistake. I read "+=" as "=", and you are correct, the only problem I can see would be if a single entry had over 100 cents, which really wouldn't make sense (or wouldn't make proper cents :smile:)
 
Last edited:

Similar threads

  • · Replies 10 ·
Replies
10
Views
3K
  • · Replies 17 ·
Replies
17
Views
3K
  • · Replies 21 ·
Replies
21
Views
3K
Replies
1
Views
2K
Replies
5
Views
2K
  • · Replies 18 ·
Replies
18
Views
3K
  • · Replies 16 ·
Replies
16
Views
13K
  • · Replies 21 ·
Replies
21
Views
4K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K