Problem Solving with C++ (9th Edition)
9th Edition
ISBN: 9780133591743
Author: Walter Savitch
Publisher: PEARSON
expand_more
expand_more
format_list_bulleted
Question
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”.
Expert Solution & Answer
Want to see the full answer?
Check out a sample textbook solutionStudents 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.
Chapter 14 Solutions
Problem Solving with C++ (9th Edition)
Ch. 14.1 - Prob. 1STECh. 14.1 - Prob. 2STECh. 14.1 - Prob. 3STECh. 14.1 - Prob. 4STECh. 14.1 - Prob. 5STECh. 14.1 - If your program produces an error message that...Ch. 14.1 - Write an iterative version of the function cheers...Ch. 14.1 - Write an iterative version of the function defined...Ch. 14.1 - Prob. 9STECh. 14.1 - Trace the recursive solution you made to Self-Test...
Ch. 14.1 - Trace the recursive solution you made to Self-Test...Ch. 14.2 - What is the output of the following program?...Ch. 14.2 - Prob. 13STECh. 14.2 - Redefine the function power so that it also works...Ch. 14.3 - Prob. 15STECh. 14.3 - Write an iterative version of the one-argument...Ch. 14 - Prob. 1PCh. 14 - Prob. 2PCh. 14 - Write a recursive version of the search function...Ch. 14 - Prob. 4PCh. 14 - Prob. 5PCh. 14 - The formula for computing the number of ways of...Ch. 14 - Write a recursive function that has an argument...Ch. 14 - Prob. 3PPCh. 14 - Prob. 4PPCh. 14 - Prob. 5PPCh. 14 - The game of Jump It consists of a board with n...Ch. 14 - Prob. 7PPCh. 14 - Prob. 8PP
Knowledge Booster
Similar questions
- In a candy store, there are N different types of candies available and the prices of all the N different types of candies are provided to you. You are now provided with an attractive offer. You can buy a single candy from the store and get at most K other candies ( all are different types ) for free. Now you have to answer two questions. Firstly, you have to find what is the minimum amount of money you have to spend to buy all the N different candies. Secondly, you have to find what is the maximum amount of money you have to spend to buy all the N different candies. In both the cases you must utilize the offer i.e. you buy one candy and get K other candies for free. Example 1: Input: N = 4 K = 2 %3D candies[] = {3 2 1 4} Output: 3 7arrow_forwardThere are four people who want to cross a rickety bridge; they all begin on the same side. You have 17 minutes to get them all across to the other side. It is night, and they have one flashlight. A maximum of two people can cross the bridge at one time. Any party that crosses, either one or two people, must have the flashlight with them. The flashlight must be walked back and forth; it cannot be thrown, for example. Person 1 takes 1 minute to cross the bridge, person 2 takes 2 minutes, person 3 takes 5 minutes, and person 4 takes 10 minutes. A pair must walk together at the rate of the slower person's pace. Write the specification of an algorithm that solves the problem.arrow_forwardThere are a set of courses, each of them requiring a set of disjoint time intervals. For example, a course could require the time from 9am to 11am and 2pm to 3pm and 4pm to 5pm. You want to know, given a number K, if it’s possible to take at least K courses. You can only take one course at any single point in time (i.e. any two courses you choose can’t overlap). Show that the problem is NP-complete, which means that choosing courses is indeed a difficult thing in our life. Use a reduction from the Independent set problem.arrow_forward
- A hardware store is selling steel rods of various lengths. However, customers sometimes want to buy rods that the store doesn't carry. For example, if the store only carries 10cm and 30cm rods and a customer wants to buy a 25cm rod, then the order cannot be fulfilled. To boost sales, the store has come up with an idea to weld two rods together to make a longer rod. For example, if the store only carries 3cm and 5cm rods and a customer wants to buy a 8cm rod, then the order can be fulfilled by welding together 3-cm and 5-cm rods. Since rod welding can compromise the strength, the store will only weld up to TWO rods together. More precisely, this means, an order for x cm can be fulfilled if there is a single rod of length x or there are two rods (potentially of the same length) whose combined length is x. Your Task: Write a function process Orders (orders: List[int], sizes:List[int]) -> List[bool] to determine whether or not each of the given orders can be fulfilled. Here, orders is a…arrow_forwardSuppose you are a participant in a game show. You have a chance to win a motorbike. You are askedto select one of the 500 doors to open; the motorbike is behind one of the 500 doors; the other remaining doorsare losers and have balloons behind them. Once you select a door, the host of the game show, who knowsexactly what is behind each of the door, randomly opens 480 of the other doors all at once that s/he for sureknows are losing doors and have balloons behind them. Then s/he reoffers you – whether you would like toswitch to the other doors or keep your initial or original selection as before. Now in this case, you are goingto make decision based on probabilistic reasoning. Therefore, whenever you are reoffered by the host to dothe selections among the remaining unopened doors, what is the probability of winning for each of theremaining unopened doors (including your original selection)? Do you want to make a switch based on theprobabilistic reasoning? If you are switching, which…arrow_forwardA coin is flipped 8 times in a row (assume all outcomes are equally likely). For each of the following questions, you should write your answer as an expression. Do not give the final numeric value. For example, you should write C(4,2)/24 instead of 0.375. Q1.1 What is the probability that it lands on heads exactly four times?arrow_forward
- G E N E T I C - A L G O R I T H M OBJECT REWARD WEIGHT A 20 1 B 5 2 C 10 3 D 40 8 E 15 7 F 25 4 G 4 5 H 7 2 Here, Maximum weight = 12 The above problem is a 0/1 Knapsack problem. You have to carry the different objects in your bag in a way such that the reward is maximized without exceeding the weight limit. You can carry an object exactly once but you always have to carry the object labeled “H”. Encode the problem and create an initial population of 4 different chromosomes. Explain what would be an appropriate fitness function for this problem. Use the fitness function and perform natural selection to choose the 2 fittest chromosomes. Using the selected chromosomes perform a single point crossover to get 2 offspring. Perform mutation and check the fitness of the final offspring. Explain your work.arrow_forwardStart with a pile of n stones and successively split a pile into two smaller piles until each pile has only one Each time a split happens, multiply the number of stones in each of the two smaller piles. (For example, if a pile has 15 stones and you split it into a pile of 7 and another pile of 8 stones, multiply 7 and 8.) The goal of this problem is to show that no matter how the pile of n stones are split, the sum of the products computed at each split is equal to n(n - 1)/2. Using strong mathematical induction, prove that no matter how the pile of n stones are split, the sum of the products computed at each split is equal to n(n - 1)/2.arrow_forwardno need for a complete solution. I just need a simple solution with the correct answer. downvote if it is incorrect. skip if you already did this or else get a downvote.arrow_forward
- QCC Casino Royale offers its customers a happy way to go home with big bucks playing its “Dice” game. The Dice game is played by asking customers to roll a pair of dice. If customers roll a seven or 11, they win; any other roll and the customer loses. The odds of rolling a seven is 6 to 1 so that if customers bet 1 dollar and win, they receive 7 dollars. The odds of rolling an eleven are 18 to 1 so that if customers bet 1 dollar and win, they receive 19 dollars. Upon entering the casino, customers must be greeted with an appropriate welcoming message. They must be asked to indicate the amount of money that is in their bankroll to play. Customers cannot enter the casino with a bankroll greater than $1,000. Customers must play the game in rounds of 10 plays and the minimum bet for each play is 5 dollars; the maximum bet is 50 dollars. For each try at rolling the dice, customers must be asked how much they want to bet and for each try must be informed of the result and the…arrow_forwardSuppose, you have been given a non-negative integer which is the height of a 'house of cards'. To build such a house you at-least require 8 cards. To increase the level (or height) of that house, you would require four sides and a base for each level. Therefore, for the top level, you would require 8 cards and for each of the rest of the levels below you would require 5 extra cards. If you were asked to build level one only, you would require just 8 cards. Of course, the input can be zero; in that case, you do not build a house at all. Complete the recursive method below to calculate the number of cards required to build a 'house of cards' of specific height given by the parameter. def hocBuilder(height): #TO DO E Height 1 Height 2 Height 3arrow_forwardSuppose, you have been given a non-negative integer which is the height of a ‘house of cards’. To build such a house you at-least require 8 cards. To increase the level (or height) of that house, you would require four sides and a base for each level. Therefore, for the top level, you would require 8 cards and for each of the rest of the levels below you would require 5 extra cards. If you were asked to build level one only, you would require just 8 cards. Of course, the input can be zero; in that case, you do not build a house at all. Complete the recursive method below to calculate the number of cards required to build a ‘house of cards’ of specific height given by the parameter. there is a picture given in that section. public int hocBuilder (int height){ // TO DO } OR def hocBuilder(height): #TO DOarrow_forward
arrow_back_ios
SEE MORE QUESTIONS
arrow_forward_ios
Recommended textbooks for you
- Operations Research : Applications and AlgorithmsComputer ScienceISBN:9780534380588Author:Wayne L. WinstonPublisher:Brooks Cole
Operations Research : Applications and Algorithms
Computer Science
ISBN:9780534380588
Author:Wayne L. Winston
Publisher:Brooks Cole