Question

Chocolate bars are sold for $1 each. For every three chocolate wrappers you turn in, you...

Chocolate bars are sold for $1 each. For every three chocolate wrappers you turn in, you receive $1 back. Implement a function iteratively which determines how many chocolate bars you can buy with x dollars.

Expected results from function call:

num_chocolatebars(0); //prints 0

num_chocolatebars(2); //prints 2

num_chocolatebars(3); //prints 4 (you spend 3 dollars on chocolate bars, you get three wrappers, then you receive one dollar back which you spend on another chocolate bar)

num_chocolatebars(5); //prints 5 + (5/3) (integer division) = 6

num_chocolatebars(9); //prints 9 + (9/3) + (9/3/3) = 13

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include <iostream>

using namespace std;

int num_chocolatebars(int amount)

{

int result = 0;

while(amount > 0)

{

result += amount; // counting the bars

amount = amount / 3; // giving back 1/3rd

}

return result;

}

int main() {

cout << num_chocolatebars(0) << endl; //prints 0

cout << num_chocolatebars(2) << endl; //prints 2

cout << num_chocolatebars(3) << endl; //prints 4 (you spend 3 dollars on chocolate bars, you get three wrappers, then you receive one dollar back which you spend on another chocolate bar)

cout << num_chocolatebars(5) << endl; //prints 5 + (5/3) (integer division) = 6

cout << num_chocolatebars(9) << endl; //prints 9 + (9/3) + (9/3/3) = 13

}

Add a comment
Know the answer?
Add Answer to:
Chocolate bars are sold for $1 each. For every three chocolate wrappers you turn in, you...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • The Good Chocolate Company makes a variety of chocolate candies, including chocolate bars sold in a...

    The Good Chocolate Company makes a variety of chocolate candies, including chocolate bars sold in a package. The quality control department takes 6 packages at a time every half hour and records the weights of the bars. .a) Construct the necessary control charts for the Good Chocolate Company and indicate if the process is in control, and if the control chart isreliable enough to monitor the process in the future .b) Specifications limits for each bar are 320 grams to...

  • 1. Homer is a deeply committed lover of chocolate. Assume his preferences are Cobb-Douglas over chocolate...

    1. Homer is a deeply committed lover of chocolate. Assume his preferences are Cobb-Douglas over chocolate bars (denoted by C on the x-axis) and a numeraire good (note: we use the notion of a numeraire good to represent spending on all other consumption goods – in this example, that means everything other than chocolate bars – its price is always $1). a. Homer earns a salary that provides him a monthly income of $360. Last month, when the price of...

  • PART 2: Which test-statistic, z, t, or neither? A candy factory that produces chocolate bars claims...

    PART 2: Which test-statistic, z, t, or neither? A candy factory that produces chocolate bars claims each bar weighs 50 grams, at least that is what is printed on the label. Of course, there is bound to be a little variation. An inspector randomly chooses 9 bars from one day's output of 4000 bars. The average weight of the 9 bars is only 47 grams with an SD of 3 grams. The inspector wishes to test the null hypothesis that...

  • In C++ please! Please include .cpp and .hpp files! Thank you! Recursive Functions Goals Create and...

    In C++ please! Please include .cpp and .hpp files! Thank you! Recursive Functions Goals Create and use recursive functions In this lab, we will write a program that uses three recursive functions. Requirements: Important: You must use the array for this lab, no vectors allowed. First Recursive Function Write a function that recursively prints a string in reverse. The function has ONLY one parameter of type string. It prints the reversed character to the screen followed by a newline character....

  • basic physics knowledge (: Problem 2) Consider six bars numbered 1 through 6. The ends of each bar are labeled A and B, and the middle of each bar is labeled m. Assume that any bars that are mag...

    basic physics knowledge (: Problem 2) Consider six bars numbered 1 through 6. The ends of each bar are labeled A and B, and the middle of each bar is labeled m. Assume that any bars that are magnets have their poles on the ends A and B. A student makes the following observations: 2m interacts with 4A 1A is attracted to 2B 3B does not interact with 6B 4B is repelled by 6A. 2A interacts with 6m 5A is...

  • In C code 1. Write and submit the algorithm OR flowchart to indicate you understand the...

    In C code 1. Write and submit the algorithm OR flowchart to indicate you understand the problem 2. Create a project and name the source code lastname_firstname_prog5.c 3. Use the prog5.c as a guide, copy/ paste into your project source code *** build run and test, the outline code - You will need to declare an integer variable called again and initialize it 4. Add the function prototype and implement the function definition for the Greeting function 5. Add the...

  • Python Complete the player_turn() function that completes a single turn of the game Tic-Tac-Toe (also called...

    Python Complete the player_turn() function that completes a single turn of the game Tic-Tac-Toe (also called Noughts and Crosses). The function takes 3 parameters: 1. The first parameter is called board. It is a list of length 3, where each entry is a string with 3 characters representing a row on a Tic-Tac-Toe board. Each character in a row represents a slot on the Tic-Tac-Toe board with the "#" character indicating an empty slot. An empty board looks like this:...

  • You must use recursion to solve each problem. You cannot use loops in this homework. You...

    You must use recursion to solve each problem. You cannot use loops in this homework. You cannot import any module. A couple of the tasks have individual restrictions; note them, as we will remove points for any task that does not follow the requirements. def factorial_evens(num): Implement a function to calculate and return the product of all even numbers from 1 up to num (inclusive if num is even). o Assumption: num will be an integer greater than or equal...

  • please use c++ language 1. Request three different integers from the console. a) Write a swap...

    please use c++ language 1. Request three different integers from the console. a) Write a swap function that swaps two integer values. b) Write a sort function which accepts three integers as input then sorts them from largest to smallest using the swap function. c) Output the integers before and after sorting. Example 1 Output (input in bold italics) Enter three different integers: 3 2 4 3 2 4 4 3 2 Example 2 Output (input in bold italics) Enter...

  • One thousand raffle tickets are sold at $1 each. Three tickets will be drawn at random...

    One thousand raffle tickets are sold at $1 each. Three tickets will be drawn at random (without replacement), and each will pay $198. Suppose you buy 5 tickets. (A) Create a payoff table for 0, 1, 2, and 3 winning tickets among the 5 tickets you purchased. (If you do not have any winning tickets, you lose $5, if you have 1 winning ticket, you net $193 since your initial $5 will not be returned to you, and so on.)...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT