SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Walter Savitch ,julia Lobur
Chapter:
Recursion
Exercise:
Programming Projects
Question:10 | ISBN:9780321531346 | Edition: 7

Question

Suppose we can buy chocolate bars from the vending machine for $1

each. Inside every chocolate bar is a coupon. We can redeem 7 coupons

for 1 chocolate bar from the machine. We would like to know how many

chocolate bars can be eaten, including those redeemed via coupon, if we

have n dollars.

For example, if we have $20 then we can initially buy 20 chocolate bars.

This gives us 20 coupons. We can redeem 14 coupons for 2 additional

chocolate bars. These two additional chocolate bars have 2 more coupons,

so we now have a total of 8 coupons when added to the 6 left over from the

original purchase. This gives us enough to redeem for 1 final chocolate bar.

As a result we now have 23 chocolate bars and 2 leftover coupons.

Write a recursive solution to this problem that inputs from the user the

number of dollars to spend on chocolate bars and outputs how many

chocolate bars you can collect after spending all your money and

redeeming as many coupons as possible. Your recursive function will be

based upon the number of coupons owned.

TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Sorry the answer is not available at the moment…

If you are able to find the answer, please make sure to post it here. So that your Juniors have smile on their lips and feel happy.

Spread the 'tradition of sharing'.