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?