I’m trying to write a C++ program that uses a recursive function to find the number of possible combinations of pennies, nickels, dimes and quarters to make $1.00 (or any amount of change for that matter)
When i started the program, it seemed deceptively easy… I thought that if you went through subtracting 25 cents from the total and adding the number of possible ways to make a quarter (13) to a counter, and so forth adding the ways to make a dime (4), nickel (2) or anything less than a nickel (1) until there was no change left your answer.
But I found out rather quickly that you can’t add the number of ways, if there’s 13 ways to get a quarter and 2 ways to get a nickel that doesn’t mean there’s 15 ways to get 30 cents. You have to multiply the two (or so I thought). But there’s not 26 combinations either. You quickly run into doubles.
You’re looking for the total number of combinations but the total number of permutations. So i developed a formula for coming up with the number of combinations given the number of possibilities for 2 outcomes.
To get the formula I drew a 4 x 3 rectangle (in the formula an N1 x N2 formula, where N2 <= N1) and labeled the top A-D and the side A-C. Combining the top with the side letter in each cell of the rectangle you get three combinations that have 2 permutations, AB, AC and BC. You’ll also notice that AD is not doubled because the side only goes down to C, meaning there is only one AD.
Anyhow… the formula I came up with is:
N1 * N2 - ( ( N2 ^ 2 - N2) / 2 )
I didn’t even recognize the shortcoming of this formula in this program until I had already coded it out. Here’s the code i came up with:
int change ( int cents )
{
int n1, n2; // postcondition : n1 > n2
if ( cents >= 25 )
{
if ( ( cents - 25 ) > 25 )
{
n1 = change (cents - 25);
n2 = 13;
}
else
{
n1 = 13;
n2 = change(cents - 25);
}
return ( n1 * n2 ) - ( ( n2 ^ 2 - n2 ) / (2) );
}
else if ( cents >= 10 )
{
if ( ( cents - 10 ) > 10 )
{
n1 = change (cents - 10);
n2 = 4;
}
else
{
n1 = 4;
n2 = change(cents - 10);
}
return ( n1 * n2 ) - ( ( n2 ^ 2 - n2 ) / (2) );
}
else if ( cents >= 5 )
{
n1 = 2;
n2 = 1;
return ( n1 * n2 ) - ( ( n2 ^ 2 - n2 ) / (2) );
}
else
return 1;
}
The problem, however, is that not each possible way to make a certain value is unique when combined with another set of possibilities. For example, when finding the amount of combinations for 30, you have two ways of making a nickel, and 13 ways of making a quarter. If you use this formula, the number of possiblities that aren’t unique is 1, giving you a total of 25. This is wrong, however, because there are quite a few possibilites canceled out because they are not unique. For example, if you make a quarter out of 3 nickels and 10 pennies and you make the nickel out of 5 pennies, this is no different than making the quarter out of 2 nickels and 15 pennies and the nickel by just using a nickel. 2 “different” permutations, but really just one combination.
So, that brings me to the question.
How do I go about doing this?
Any ideas/formulas/code/etc would be greatly appreciated, thanks.