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:

``````// 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:

``````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?

## All 2 Replies

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()"

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.