Problem Solving with C++ (9th Edition)
Problem Solving with C++ (9th Edition)
9th Edition
ISBN: 9780133591743
Author: Walter Savitch
Publisher: PEARSON
Question
Book Icon
Chapter 14, Problem 7PP
Program Plan Intro

Chocolate Bars and Coupons

Program plan:

  • Include required header file.
  • Declare the function for compute chocolate bars
  • Define main function.
    • Declare variable for “nDollars” and “result”.
    • Create prompt statement for ask the number of dollars.
    • Read the number of dollars from the user.
    • Call the function “calculateChocolateBars” with “nDollars” and “coupons” and then store the result in a variable “result”.
    • Finally display the result.
  • Define “calculateChocolateBars” function with argument “nDollars” and “coupons”.
    • If the value of “nDollars” is equal to “0” and the value of “coupons” is less than “7”, then returns nothing.
    • Otherwise, first assigns the value of dollars to a variable “res”. Then compute the value of coupons.
    • Finally returns the number of chocolate bars by recursively call the function “calculateChocolateBars” with dividing and moduling the coupons with “7” and then add with “res”.

Blurred answer
Students have asked these similar questions
Suppose we can buy a chocolate bar from the vending machine for $1 each.Inside every chocolate bar is a coupon. We can redeem six coupons for onechocolate bar from the machine. This means that once you have startedbuying chocolate bars from the machine, you always have some coupons.We would like to know how many chocolate bars can be eaten if we startwith N dollars and always redeem coupons if we have enough for an additional chocolate bar.For example, with 6 dollars we could consume 7 chocolate bars afterpurchasing 6 bars giving us 6 coupons and then redeeming the 6 couponsfor one bar. This would leave us with one extra coupon. For 11 dollars, wecould have consumed 13 chocolate bars and still have one coupon left.For 12 dollars, we could have consumed 14 chocolate bars and have twocoupons left.
Suppose a person can buy a chocolate bar from the vending machine for $1 each.  Inside every chocolate bar is a coupon.  A person can redeem 3 coupons for one chocolate bar from the machine.  This means that once a person has started buying chocolate bars from the machine, he/she always has some coupons. A person would like to know how many chocolate bars can be bought, if a person starts with N dollars and always redeem coupons, if he/she has enough for an additional chocolate bar. For example: With 3 dollars a person could buy 4 chocolate bars after purchasing 3 bars giving him/her 3 coupons and then redeeming the 3 coupons for one bar.  This would leave him/her with one extra coupon. Thus, will have 4 chocolate bars and still have one coupon leftover. For 11 dollars, a person can have 16 chocolate bars and still have one coupon leftover.  For 12 dollars, a person can have 17 chocolate bars and have two coupons leftover. Write a complete Python program that prompts a buyer to input…
We have n glasses which can hold a varying amount of liquid. We will say that glass1 is size q1 quarts and glass2 is size q2 quarts, etc. We now give you a size, say x quarts. You must give me a glass of exactly x quarts.  You are allowed to fill any glass to exactly its size and to pour any liquid from one glass into another.  When you pour, you either have to empty the glass you are pouring from or fill the glass you are pouring into.  Can you do it?More concrete example: G1 has size 10 quarts.  G2 has size 4 quarts.  G3 has size 1 quart.  You can get a 5 quart drink by filling G1, pouring 4 quarts from it into G2 and 1 quart of it into G1.  After that, G1 has exactly 5 quarts. In the same problem, we could get 8 quarts by filling G2 twice and emptying it into G1 twice.   Given This: Write an algorithm to do this and determine its complexity.
Knowledge Booster
Background pattern image
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
Operations Research : Applications and Algorithms
Computer Science
ISBN:9780534380588
Author:Wayne L. Winston
Publisher:Brooks Cole