Description of the Problem:
In my program, i need to write a program that will determine how many coins of each denomination (1¢, 5¢, 10¢, 25¢, and 50¢) are required to make up a given amount of change. For example, to make 57¢ we could use 1 half dollar, 1 nickel, and 2 pennies.
Given an amount of change to make, it should be obvious that many combinations of coins are possible. In the example above, we could make up 57¢ with: 57 pennies; 5 dimes, a nickel, and 2 pennies; 2 quarters and 7 pennies; and so on. In this program I need to use an algorithm that figures out how many of teh largest denomination coin i need first, and works down through each succeeding coin demonination.
Alright, I have been looking over this program for awhile now and I cant seem to get my function right. I have ran over what I had so far with my professor and he said that it is all correct except for my function that i built, so he deleted it and told me to start fresh. I was wondering if someone could point me into the right direction to compute my function for this code to run properly.
HERE IS MY CODE:
// This program will make change
#include <iostream>
#include <cassert>
using namespace std;
// The function prototype
int computeChange(int& changeValue, int coinValue);
// some constants
const int HALVES = 50;
const int QUARTERS = 25;
const int DIMES = 10;
const int NICKELS = 5;
const int PENNIES = 1;
const string HALF = "halves";
const string QUARTER = "quarters";
const string DIME = "dimes";
const string NICKEL = "nickels";
const string PENNY = "pennies";
int main ( )
{
int money; // the value i 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:";
cout << "\n" << computeChange(money, HALVES) << " " << HALF;
cout << "\n" << computeChange(money, QUARTERS) << " " << QUARTER;
cout << "\n" << computeChange(money, DIMES) << " " << DIME;
cout << "\n" << computeChange(money, NICKELS) << " " << NICKEL;
cout << "\n" << computeChange(money, PENNIES) << " " << PENNY;
cout << endl;
system("PAUSE");
return 0;
}
int computeChange(int& changeValue, int coinValue)
{
// THIS IS WHERE I NEED HELP???? CAN ANYONE HELP ME?? THANKS
}
You will notice that the code is suppose to do the same thing over and over again, but with different values for n and for the coin denomination. In this case, i want a function that takes as its inputs n, the amount of change left, and a coin denomination. The function should do as follows
1. Compute the number of coins of the given denomination it can take out of n, and
2. Compute the new value of n, after taking out those coins.
Im completely stuck. I hope it looks like i have made some effort to solve this problem. I just simply have ran out of ideas. Can anyone help me?? If you would like anything else please just ask and i can simply answer the questions.