Recursion Function

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.
Last edited on
First, please post code with code tags. See https://www.cplusplus.com/articles/jEywvCM9/

Do not print anything in the function. Make it return the number of bars. Print the result in the main program.

In your example, you had $20 and 0 coupons at start. With those you get 20 bars and 20 coupons.
If you have 20 coupons you get two bars and you still have (20-14+2) 8 coupons.
If you have 8 coupons you get one bar and you still have (8-7+1) 2 coupons.
If you have 2 coupons you get nothing, but keep the coupons.
Therefore, you got 0+1+2+20 = 23 bars.

Lets call your getBars(20,0):
1. 0 is less than 7, so: "You can buy 20 Chocolate Bars"
2. That's it?

But your main program calls getBars(20,20)??
1. Lets call getBars(21,13)
2. Lets call getBars(22,6)
3. 6 is less than 7, so: "You can buy 22 Chocolate Bars"

If you had $20 and 20 coupons, you could:
1. Buy 20 bars and have 40 coupons
2. Buy 5 bars and have (40-35+5) 10 coupons
3. Buy one more bar and have 4 coupons leftover
That is: buy 26 bars?
Thank you for your input! I apologize for the lack of code tags.

I think i might not be understanding the problem itself. I was under the impression that the the user will input the amount of money they have and calculate how much total bars that they can recieve with just that amount. So if I have 20$, I store that in the Money variable, since I get 1 coupon per chocolate bar and each bar is 1$, then it is given that Money=Coupons. My function increments the amount of "Money" or "Chocolate Bars" by +1 everytime my Coupon variable goes down by 7 since each bar is 7 coupons. So if I enter 20 dollars, my program outputs that I get 22 bars, and 6 left over which means I can't get anymore bars.

My problem comes in when I have to account for the extra 2 coupons that the user recieved because of the 14 coupons they traded for the two bars. So in reality I have 8 which means the total bars are 23, I dont know where/how in my function I could input that in so that the recursive function retrieves 23 instead of 22.
we understand your assignment, don't need to repeat it over and over again.

let's simplify your function, say that it only ask for coupons
int getBars(int coupons);
you may write the base case as
1
2
if (coupons < 7)
   return 0;


now, with n coupons you may get n/7 bars, ┬┐how many coupons do you end with?
The coupons you are left with is the remainder of n/7
With c coupons, you get 0 chocolate bars if c is less than 7, else you get c / 7 chocolate bars plus the number of chocolate bars you can get with your remaining coupons plus those extracted from the new chocolate bars.
Last edited on
Recursion is one of those cs topics that you really need to master by understanding the fundamentals and getting to grips with it yourself. It can hard to grasp. But once you do then you can apply the idea to many different situations. IMO its one area where just providing code doesn't help.

The main idea about recursion is that the function calls itself with different arguments and that there is at least one condition of the args that stops the recursion.

So perhaps as a function declaration you could have:

unsigned getNoBars(unsigned money, unsigned coupons);

So barsBought = money + coupons / 7
money = 0 and couponsLeft = coupons % 7 + barsBought

The condition to stop would be money == 0 and coupons < 7 as in this case you can't buy any more bars.

So the return value from the function is barsBought + getNoBars(0, couponsLeft)

For anyone else following this thread, possibly:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>

unsigned getNoBars(unsigned money, unsigned coupons = 0) {
	if (money == 0 && coupons < 7)
		return 0;

	const auto barsBought {money + coupons / 7};

	return barsBought + getNoBars(0, barsBought + coupons % 7);
}

int main() {
	std::cout << getNoBars(20) << '\n';
}



23

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
int get_bars_1(int c) 
{
  return c < 7? 0: (c / 7) + get_bars_1((c / 7) + (c % 7)); 
}

int get_bars_2(int coupons, int bars = 0) 
{
  if (coupons < 7) return bars;

  int const q = coupons / 7;
  int const r = coupons % 7;
    
  coupons = q + r;
  bars += q;
    
  return get_bars_2(coupons, bars);
}

int get_bars_3(int money, int coupons)
{
  return get_bars_1(money + coupons);
}
Last edited on
@mbozzi: get_bars_3(1, 0) return 0, should be 1
Thanks! I believe Line 21 should have read
return money + get_bars_1(money + coupons);
That's what I get for not testing my code before posting it. :-(
Topic archived. No new replies allowed.