Problem Solving with C++ (10th Edition)
Problem Solving with C++ (10th Edition)
10th Edition
ISBN: 9780134448282
Author: Walter Savitch, Kenrick Mock
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 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…
Consider the problem of making change for n cents using the fewest number of coins. Assume that we live in a country where coins come in k dierent denominations c1, c2, . . . , ck, such that the coin values are positive integers, k ≥ 1, and c1 = 1, i.e., there are pennies, so there is a solution for every value of n. For example, in case of the US coins, k = 4, c1 = 1, c2 = 5, c3 = 10, c4 = 25, i.e., there are pennies, nickels, dimes, and quarters. To give optimal change in the US for n cents, it is sufficient to pick as many quarters as possible, then as many dimes as possible, then as many nickels as possible, and nally give the rest in pennies.   Design a bottom-up (non-recursive) O(nk)-time algorithm that makes change for any set of k different coin denominations. Write down the pseudocode and analyze its running time. Argue why your choice of the array and the order in which you fill in the values is the correct one. Notice how it is a lot easier to analyze the running time of…
Consider the problem of making change for n cents using the fewest number of coins. Assume that we live in a country where coins come in k dierent denominations c1, c2, . . . , ck, such that the coin values are positive integers, k ≥ 1, and c1 = 1, i.e., there are pennies, so there is a solution for every value of n. For example, in case of the US coins, k = 4, c1 = 1, c2 = 5, c3 = 10, c4 = 25, i.e., there are pennies, nickels, dimes, and quarters. To give optimal change in the US for n cents, it is sufficient to pick as many quarters as possible, then as many dimes as possible, then as many nickels as possible, and nally give the rest in pennies. Design a bottom-up (non-recursive) O(nk)-time algorithm that makes change for any set of k different coin denominations. Write down the pseudocode and analyze its running time. Argue why your choice of the array and the order in which you ll in the values is the correct one.
Knowledge Booster
Background pattern image
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning
Text book image
Operations Research : Applications and Algorithms
Computer Science
ISBN:9780534380588
Author:Wayne L. Winston
Publisher:Brooks Cole