Debugging a Program to Add Up Monthly Sales

In summary, the conversation discusses a coding problem involving a structured type called Money and an array named monthlySales. The goal is to add up the values in the array and store the total in a variable called yearlySales, making sure that the value of cents is always less than 100. A possible solution is provided, but a potential issue is pointed out and addressed.
  • #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?
 
Physics news on Phys.org
  • #2
yearlySales.cents -= 100
 
  • #3
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:
  • #4
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:
  • #5


I would first check the input data to make sure it is valid and matches the expected format. I would also check for any potential errors or bugs in the code itself, such as using the correct variable names and data types, ensuring proper looping and conditional statements, and checking for any potential overflow or underflow errors. Additionally, I would test the code with different inputs and see if the results are consistent. If there are still issues with the code, I would use debugging tools or techniques to pinpoint the specific error and make necessary adjustments.
 

1. Why is my program not adding up the monthly sales correctly?

There could be several reasons for this issue. One common reason is that there may be a coding error in your program, such as a missing or incorrect variable or function. Another possibility is that there may be a data input error, such as incorrect formatting or missing data. It's also possible that there may be a logic error in your program's algorithm, causing it to calculate the sales incorrectly.

2. How can I identify and fix errors in my code?

The first step is to carefully review your code line by line to check for any typos or mistakes. You can also use a debugger tool to step through your code and identify where the program is going wrong. Another helpful strategy is to use print statements to track the values of variables and see where they may be changing unexpectedly. Once you have identified the error, you can make the necessary changes to fix it.

3. Can data input errors affect the accuracy of my program's results?

Yes, data input errors can significantly impact the accuracy of your program's calculations. It's important to ensure that all data is entered correctly and in the expected format. You may also want to include validation checks in your code to catch any potential input errors and handle them appropriately.

4. What can I do to prevent logic errors in my program?

One way to prevent logic errors is to plan and test your code thoroughly before implementing it. This can include creating test cases and running them with different sets of data to ensure that your program produces accurate results. Additionally, you can use comments and documentation within your code to help you and others understand the logic behind your program.

5. How can I improve the efficiency of my program when adding up monthly sales?

There are several ways to improve the efficiency of your program. One approach is to use appropriate data structures and algorithms that are optimized for the type of data and calculations you are performing. You can also consider implementing caching or other optimization techniques to reduce the time and resources needed for your program to complete its task. Additionally, regularly reviewing and updating your code can help to identify and eliminate any redundant or unnecessary steps that may be slowing down your program.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
10
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
18
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
21
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
3K
  • Programming and Computer Science
Replies
25
Views
2K
  • Programming and Computer Science
Replies
32
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
5K
Back
Top