Hello, currently im working on a homework problem that deals with recursion, the problem goes as follows:
Suppose you can buy chocolate bars from a vending machine for $1 each.
Inside every chocolate bar is a coupon. You can redeem 7 coupons for 1
chocolate bar from the machine.
For example, if you have $20, you can initially buy 20 chocolate bars.
This gives you 20 coupons. You can redeem 14 coupons for 2 additional
chocolate bars. These 2 chocolate bars have 2 more coupons, so you now have a
total of 8 coupons. This gives you enough to redeem for 1 final chocolate bar.
Write a recursive function that would accept the amount of dollars and
coupons as its parameters and output the total number of chocolate bars that
can be bought (including those redeemed via coupons).
Write a test program that would allow the user to enter amount of dollars
and then use the recursive function to compute the number of chocolate bars
the user can buy, and output the number to the screen.
So far I can print the total ammount of choocolate bars with the coupons, but Im stuck on figuring out how to incorporate the extra free coupons with the reedemed ones.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
|
void getBars(int Money, int Coupons)
{
if (Coupons < 7)
{
cout << "You can buy " << Money << " Chocolate Bars" << endl;
}
else
{
getBars(Money + 1, Coupons - 7);
}
}
int main()
{
int Money;
cout << "Enter money in dollars: ";
cin >> Money;
int Coupon = Money;
getBars(Money, Coupon);
}
|
For example, if i put in that I have 20 dollars, then the output is 22 chocolate bars when it should be 23, because with 20 dollars ill have 20 coupons minus 14, so ill have a remainder of 6 coupons, but since I got two extra bars, those coupons are now a total of 8, show now i am able to get an extra free one.