1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Records and C++

  1. Mar 19, 2013 #1
    1. The problem statement, all variables and given/known data
    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.



    3. 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?
     
  2. jcsd
  3. Mar 19, 2013 #2

    phinds

    User Avatar
    Gold Member
    2016 Award

    yearlySales.cents -= 100
     
  4. Mar 19, 2013 #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: Mar 19, 2013
  5. Mar 19, 2013 #4

    phinds

    User Avatar
    Gold Member
    2016 Award

    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: Mar 19, 2013
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted



Similar Discussions: Records and C++
  1. C/C++ hw question! (Replies: 3)

  2. C\C++ help needed (Replies: 2)

  3. C++ . (Replies: 19)

Loading...