| | |
Need help with a computeChange function
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Sep 2006
Posts: 67
Reputation:
Solved Threads: 0
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:
What I have so far:
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?
C++ Syntax (Toggle Plain Text)
// The function prototype int computeChange(int& changeValue, int coinValue); // some constants const int COINS[ ] = {50, 25, 10, 5, 1}; const string COIN_NAMES[ ] = {"Halfs", "Quarters", "Dimes", "Nickels", "Pennies"}; const int NUM_COINS = 5; int main ( ) { int money; // the value we want to count change for cout << "\nI will make change for you."; cout << "\nEnter in an amount between 1 and 99: "; cin >> money; cout << "\nFor " << money << " you get:"; for (int i = 0; i < NUM_COINS; i++) { cout << "\n" << computeChange(money, COINS[i]) << " " << COIN_NAMES[i]; } cout << endl; system("PAUSE"); return 0; } int computeChange(int& changeValue, int coinValue) { // write the code here for the computeChange function }
What I have so far:
C++ Syntax (Toggle Plain Text)
int computeChange(int& changeValue, int coinValue) { // 1. Compute the number of coins of the given denomination it can take out of n/money // 2. Compute the new value of n, after taking out those coins. // For example, money = 57 cents. We can get 1 half dollar, then 7 cents left. No quarters or // dimes, but we can take out 1 nickel, which leaves 2 cents or two pennies total. int halfs; // Number of half dollars in change int quarters; // " " quarters " " int dimes; // " " dimes " " int nickels; // " " nickels " " int pennies; // " " pennies " " // halfs = changeValue / 50; // // Compute the remaining amount of change by subtracting the amount // of halfs from the total change. // changeValue = changeValue - (halfs * 50); // // Proceed in the same way for quarters, dimes, // nickels, and pennies. // quarters = changeValue / 25; changeValue = changeValue - (quarters * 25); dimes = changeValue / 10; change = changeValue - (dimes * 10); nickels = changeValue / 5; pennies = changeValue - (nickels * 5); }
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.
•
•
Join Date: Jul 2005
Posts: 1,761
Reputation:
Solved Threads: 283
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.
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()"
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
![]() |
Similar Threads
- PHP Feezing with a function (PHP)
- ASP Replace Function.... (ASP)
- missing function header (C++)
- Function that returns void (C++)
Other Threads in the C++ Forum
- Previous Thread: string in c++,segmentation fault,thread
- Next Thread: play avi using C++ VS2005
Views: 1985 | Replies: 2
| Thread Tools | Search this Thread |
Tag cloud for C++
6 api application array arrays assignment beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete developer display dll email encryption error file format forms fstream function functions game generator getline givemetehcodez graph iamthwee ifstream image input int java lib loop looping loops map math matrix memory multidimensional multiple newbie news node number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sort sorting string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






