hey,
I have to write a program that asks the user for an input amount (in cents) and the highest denominations of change (1c,5c,10c,20c & 50c). With these 2 inputs, the program then finds the distinct ways in which the change for the given amount can be made using the denomination as an upper bound, i.e. given 50cents, there are 57 ways in which the change can be given using 50 cents as the highest denomination, 56 ways using 20 cents as the highest denominations, 36 ways using 10 cents, 11 ways using 5 cents and only 1 way using 1cents.
This is part of my code:
int one_c() //one cent
{
return 1;
}
int five_c(int amount) //five cents
{
if (amount >= 0)
return five_c(amount-5) + one_c();
return 0;
}
int ten_c(int amount) //ten cents
{
if (amount >= 0)
return ten_c(amount-10) + five_c(amount);
return 0;
}
int twenty_c(int amount) //twenty cents
{
if (amount >= 0)
return twenty_c(amount-20) + ten_c(amount);
return 0;
}
int fifty_c(int amount) //fifty cents
{
if (amount >= 0)
return fifty_c(amount-50) + twenty_c(amount);
return 0;
}
Using these separate functions, I use the input denomination to call on the corresponding function (matching denomination).
What I want to do is to combine these functions into a recursive function so that my program can become more efficient.
Will be very grateful for any guidance.
Thanx.