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!

Computer Science Homework Question

  1. Sep 26, 2007 #1
    1. The problem statement, all variables and given/known data
    A customer brings purchases worth less than $20 to the till, and hands over a $20 bill. Write pseudocode for an algorithm to figure out what change the customer should receive -- i.e., how many $10 bills, how many $5 bills, how many toonies ($2), loonies ($1), quarters (0.25), dimes (0.10), nickels (0.05), and pennies (0.01). Begin by listing the variables you will need. Keep in mind that a proper solution will use as few coins as possible!

    3. The attempt at a solution
    I don't know how to start this. I named some variables, ie numberOfTens, numberOfFives, change, totalOfPurchases etc but I don't even know where to start. I just want a helping hand so I can get started. Thanks so much!
  2. jcsd
  3. Sep 26, 2007 #2
    Note, do not use integer variable types. What variable types would allow you to store xx.xx, where xx are real #s?

    Customer's money - price of goods = change.

    You could use a series of if/else statements to test for the conditions, but you have to becareful. Well, from the problem statement we know that he must buy something totalling less than 20 (even if the item he buy is at minimum $0.01 cent).

    AFTER you calculate the difference, you know the amount of change.

    You can say if the change is GREATER than 10, subtract 10 from the change and increase the variable that holds the # of tens bills to hand out. You are guaranteed $9.99 as your max value (after subtracting $10), even if the customer purchases something of $0.01 cent value. Note, if the change is $8.31 for ex, the if condition will hold false and continue throughout the program (through the other if-else statements)

    You can't give another ten, if you've subtracted one already and have $9.99. If you give him another $10 (in addition to the first one), you'll lose 1 cent. So now, check for fives. If you subtract 5 from 9.99, you'll receive 4.99, which means that numberofFives++... right? keep it going. 4.99 is your maximum after you've subtracted the appropriate number of fives.

    Note, this program would only be good for handing the customer the highest # of bills possible, then the remaining cents if any. In real life, a customer may ask for 3 fives instead of a five and a ten.
    Last edited: Sep 26, 2007
  4. Sep 27, 2007 #3
    Thank you so much for your help!!!!! I'll reply back if I have any more question, but this should get me on the right track.
  5. Sep 27, 2007 #4
    • if change is >= ten dollars
    o subtract ten dollars from change and add 1 to numberOfTens
    • if change is >= than five dollars
    o subtract five dollars from change and add 1 to numberOfFives
    • if change is >= four dollars
    o subtract four dollars from change and add 2 to numberOfToonies
    • if change is >= two dollars but less than four dollars
    o subtract two dollars from change and add 1 to numberOfToonies
    • if change is greater than one dollar but less than 2 dollars
    o subtract one dollar from change and add 1 to numberOfLoonies
    • if change is less than one dollars
    o if change is >= 75 cents
    • subtract 75 cents from change and add 3 to numberOfQuarters
    o if change is >= 50 cents but less than 75 cents
    • subtract 50 cents from change and add 2 to numberOfQuarters
    o if change is >= 25 cents but less than 50 cents
    • subtract 25 cents from change and add 1 to numberOfQuarters
    • if change is less than 25 cents
    o if change is >= 20 cents
    • subtract 20 cents from change and add 2 to numberOfDimes
    o if change is >= 10 cents but less than 20 cents
    • subtract 10 cents from change and add 1 to numberOfDimes
    • if change is less than 10 cents
    o if change is >= 5 cents
    • subtract 5 cents from change and add 1 to numberOfNickels
    • if change is less than 5 cents
    o if change is 4 cents
    • subtract four from change and add four to numberOfPennies
    o if change is 3 cents
    • subtract three from change and add three to numberOfPennies
    o if change is 2 cents
    • subtract two from change and add two to numberOfPennies
    o if change is 1 cents
    • subtract one from change and add one to numberOfPennies

    It didn't tab like i did on word but you should get the general idea. Is this right or is there a more efficient way. Also, now that this is done, how do i set up my variables and display the change?
  6. Sep 27, 2007 #5
    That is one way of doing it. Your method seems to be correct, I didnt read each line, but the first few are good. Now, if the change is $4.99, the way you could optimize that code a little bit is to have a while loop, basically saying while the change is less than 4, and greater than or equal to 1, it will continue to subtract $1 from it.

    Note, the method I suggested and what you did is very tedious. At least you have something to hand in for your HW.

    I would sit and brainstorm other possibilities. Write all the things you know about C++ and see how you can implement them into your algorithm design.
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?

Similar Discussions: Computer Science Homework Question