By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,655 Members | 1,005 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,655 IT Pros & Developers. It's quick & easy.

Coin Change Program

P: 1
Homework Assignment:

Write program to compute the coins necessary to return change made up of quarters, dimes, nickels, and pennies.
We are not allowed to use if/else or loops in this program.

I am a beginner who knows very little about C++. We are working from Deitel's fourth edition and using the Visual C++ 6.0 compiler. I have worked for days on this problem and cannot figure out the logic part of it.

I am using Windows 95 right now and don't have a compiler but have access to one later.

This is what I have so far:

Expand|Select|Wrap|Line Numbers
  1.  
  2. #include <iostream>
  3. using namespace std;
  4. void main ()
  5. {
  6.  
  7. float Qvalue=.25;//The value of a quarter
  8. float Dvalue=.10;//The value of a dime
  9. float Nvalue=.05;//The value of a nickel
  10. float Pvalue=.01;//The value of a penny
  11.  
  12. float change_amt;//Memory for user input
  13.  
  14. int Qcount=0;//Initialize quarter count to 0
  15. int Dcount=0;//Initialize dime count to 0
  16. int Ncount=0;//Initialize nickel count to 0
  17. int Pcount=0;//Initialize penny count to 0
  18.  
  19. Qcount+=1;//Increment Qcount by 1
  20. Dcount+=1;//Increment Dcount by 1
  21. Ncount+=1;//Increment Ncount by 1
  22. Pcount+=1;//Increment Pcount by 1
  23.  
  24. cout<<"Enter change amount in 2 digit decimal format" <<"\n";
  25. cin>>change_amt;//2 digit decimal format only
  26.  
  27. (change_amt !<=.00) || (change_amt !>.99);
  28.  
  29. cout<<"Change is comprised of:" 
  30.  
  31. <<Qcount "Quarter(s)" "\n"
  32. <<Dcount "Dime(s)" "\n"
  33. <<Ncount "Nickel(s)" "\n"
  34. <<Pcount "Penny/Pennies" "\n";
  35.  
  36. return 0;
  37. }
  38.  
The computer has to do the calculations but I don't know how to set it up. I was thinking:

Divide change by Qvalue and somehow using % to go from quarters to dimes, nickels and pennies using the leftovers?

The maximum count for Quarters is 3
The maximum count for Dimes is 2
The maximum count for Nickels is 1
The maximum count for Pennies is 4

The Coin counters have to be reset somehow for each input amount.

The assignment is due on February 20th.

Thanks for your help.
Feb 18 '07 #1
Share this Question
Share on Google+
2 Replies


Ganon11
Expert 2.5K+
P: 3,652
The computer has to do the calculations but I don't know how to set it up. I was thinking:

Divide change by Qvalue and somehow using % to go from quarters to dimes, nickels and pennies using the leftovers?
This is exactly how you would solve this problem. Think about this:

If I have x cents in change, then x / 25 will tell me how many quarters I need. Round down, because you can't have a fraction of a quarter, and if you rounded up, there would be more money than original. Now that I know how many quarters I have, I can either

A: Subtract the value of my quarters from the total amount
B: Use mod to do it for me.

How would you use % to make sure the new change amount is less than 25 cents?
Feb 18 '07 #2

Ganon11
Expert 2.5K+
P: 3,652
From a PM:

If I have (xcents/.25 % <.25)/.10 %<.10 and so on would this work???
I'm not sure if that sentence is syntactically correct, but if I'm correct, this is what you're trying to do:

Expand|Select|Wrap|Line Numbers
  1. xcents / 0.25;
  2. xcents = xcents % 0.25;
etc.

Is that correct?
Feb 18 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.