I worked on it a bit more and here's what I have so far. It's getting close, but I'm still not getting the correct answers. Again, the program has 2 parts, Part 1 is done and working alright. I just need you guys to look at Part 2, where I have to output the number of different ways to provide a certain change.
So if the user inputs 10cents, the output should be 4 (10pennies, 1 dime, 2 nickels, 1nickel and 5pennies)
or if the user inputs 25cents, it should output 13
Here's the code and I appreciate any help
CODE
#include <iostream>
using namespace std;
int calculate(int);
int num_ways = 1; //Number of Ways to break the change
int num_ways2 = 0; //num_ways used in the recursive case
int index, index2, num, test;
//Declare Money Array and Money Quantity Array
int money[12] = {10000, 5000, 2000, 1000, 500, 200, 100, 50, 25, 10, 5, 1};
char name[12][20] = {"$100", "$50", "$20", "$10", "$5", "$2", "$1", "Half Dollar", "Quarter", "Dime", "Nickel", "Penny"};
int quantity[12];
int quantity2[12];
//Declare other Variables
double change, change1, change2;
int main()
{
//Ask user for change amount
cout << "How much change did you get? ";
cin >> change;
cout << " " << endl;
//Display the results
cout << "The most efficient way to provide the change amount $" << change
<< " is:" << endl << endl;
change1 = change * 100;
//Loop
for (index = 0; index < 12; index++)
{
quantity[index] = 0;
while (change1 >= money[index])
{
quantity[index]++;
change1 = change1 - money[index];
}
if (quantity[index] > 0)
cout << quantity[index] << " " << name[index] << endl;
}
cout << endl << endl;
// PART 2 - Provide the number of different ways to provide change if it is $10 or less
cout << "************PART 2************" << endl << endl;
cout << endl << "If your change is $10 or less," << endl
<< "I will show you the number of different ways you can get your change."
<< endl << endl;
cout << "Change is " << change << endl;
change2 = change * 100;
if (change > 10)
cout << "Your change is greater than $10." << endl;
else if (change == 0)
cout << "You don't have any change." << endl;
else
{
test = 1;
while (test > 0)
{
test = 0;
for (index = 10; index > 2; index--)
{
while (quantity[index] > 0)
{
change2 = quantity[index] * money[index];
calculate(quantity[index]);
num_ways = num_ways + (num_ways * num_ways2);
test = test + quantity[index];
quantity[index]--;
}
}
}
cout << "There are " << num_ways << " different ways to provide the change amount of $"
<< change << "." << endl;
}
return 0;
}
// START RECURSION
int calculate(int num)
{
if (num == 0)
return 1;
else
{
while (change2 >= money[index + 1])
{
quantity[index + 1]++;
change2 = change2 - money[index + 1];
}
num_ways2++;
return (num - 1);
}
}