Need help with a computeChange function

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Sep 2006
Posts: 67
Reputation: matrimforever is an unknown quantity at this point 
Solved Threads: 0
matrimforever matrimforever is offline Offline
Junior Poster in Training

Need help with a computeChange function

 
0
  #1
Oct 13th, 2006
We are discussing passing parameters by value and by reference in class and I need to come up with a function 'computeChange' from the following code:
  1. // The function prototype
  2. int computeChange(int& changeValue, int coinValue);
  3.  
  4. // some constants
  5. const int COINS[ ] = {50, 25, 10, 5, 1};
  6. const string COIN_NAMES[ ] = {"Halfs", "Quarters", "Dimes", "Nickels", "Pennies"};
  7. const int NUM_COINS = 5;
  8.  
  9.  
  10. int main ( )
  11. {
  12. int money; // the value we want to count change for
  13.  
  14. cout << "\nI will make change for you.";
  15. cout << "\nEnter in an amount between 1 and 99: ";
  16. cin >> money;
  17.  
  18. cout << "\nFor " << money << " you get:";
  19. for (int i = 0; i < NUM_COINS; i++)
  20. {
  21. cout << "\n" << computeChange(money, COINS[i]) << " " << COIN_NAMES[i];
  22. }
  23. cout << endl;
  24.  
  25. system("PAUSE");
  26. return 0;
  27. }
  28.  
  29. int computeChange(int& changeValue, int coinValue)
  30. {
  31. // write the code here for the computeChange function
  32. }


What I have so far:


  1. int computeChange(int& changeValue, int coinValue)
  2. {
  3.  
  4. // 1. Compute the number of coins of the given denomination it can take out of n/money
  5. // 2. Compute the new value of n, after taking out those coins.
  6. // For example, money = 57 cents. We can get 1 half dollar, then 7 cents left. No quarters or
  7. // dimes, but we can take out 1 nickel, which leaves 2 cents or two pennies total.
  8.  
  9. int halfs; // Number of half dollars in change
  10. int quarters; // " " quarters " "
  11. int dimes; // " " dimes " "
  12. int nickels; // " " nickels " "
  13. int pennies; // " " pennies " "
  14.  
  15.  
  16.  
  17.  
  18. //
  19. halfs = changeValue / 50;
  20.  
  21. //
  22. // Compute the remaining amount of change by subtracting the amount
  23. // of halfs from the total change.
  24. //
  25. changeValue = changeValue - (halfs * 50);
  26.  
  27. //
  28. // Proceed in the same way for quarters, dimes,
  29. // nickels, and pennies.
  30. //
  31. quarters = changeValue / 25;
  32. changeValue = changeValue - (quarters * 25);
  33. dimes = changeValue / 10;
  34. change = changeValue - (dimes * 10);
  35. nickels = changeValue / 5;
  36. pennies = changeValue - (nickels * 5);
  37.  
  38.  
  39. }

This doesn't work as I'm sure you can see. I am thinking I don't even need to declare the quarters, dimes, etc, since they are declared previously as a string and COINS value, and if that is the case, i'm not sure how to code that in. I guess I'm not really clear as to what I need to do to pass the value and reference from the first part of the code. Anyone have suggestions?
Last edited by matrimforever; Oct 13th, 2006 at 1:46 am.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,761
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 283
Lerner Lerner is offline Offline
Posting Virtuoso

Re: Need help with a computeChange function

 
0
  #2
Oct 13th, 2006
We can debate whether declaring the constants globally or local to main() until we're blue in the face, and we won't be right or wrong. Since this is an excercise in passing parameters, however, I would argue to make the constants local to main() and pass them as needed to computeChange(). I would also declare an int variable to represent the number of however many coins you will need of each type, and send it to computeChange() by reference. Then you would pass the amount to make change from, the value of the coin you are using, and how many coins of that type you will need, to computeChange(). Within computeChange() you could use a loop to do the math until the amount to make change from is less than the value of the current coin. Each time through the loop you increment the number of coins it's going to take, without all the other verbage you have in the function now. If you set it up right, the number of each coin and the amount left to make change from will be sent back to main() where they will be reused if needed.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,653
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 474
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: Need help with a computeChange function

 
0
  #3
Oct 13th, 2006
I think you have not explained the problem statement properly. For eg. if the user enters that he wants change for 2 dollars then the possibilities are:

4 halfs OR 8 quarters OR 20 dimes and so on....

Also why you need to pass the constant global arrays to the functions.

Why need a for loop in main (). Let the function take care of all the iterations and keep main() as simple as possible.

Also instead of declaring seperate variables declare an array of the size of NUM_COINS and keep filling it with the coins of each type and actually pass THAT array as reference to the function.

Hope it helped, bye.

PS: Dont use "system ("pause")" since using system functions is not that good a programming practice. Insted use "getchar()"
The romantic image of an über-programmer is someone who fires up Emacs, types like a machine gun, and delivers a flawless final product from scratch. A more accurate image would be someone who stares quietly into space for a few minutes and then says “Hmm. I think I’ve seen something like this before.” - John D
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 1985 | Replies: 2
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC