Question: Solve the problem given in Exercise 8.6. Using a recursive algorithm. You must use recursion. Your code CANNOT use any loops (for, while, etc) to

 Solve the problem given in Exercise 8.6. Using a recursive algorithm.

Solve the problem given in Exercise 8.6. Using a recursive algorithm.

You must use recursion. Your code CANNOT use any loops (for, while, etc) to cycle through the weights except to calculate sums or to print to the screen.

In addition to just a boolean of can it be balanced, also output the positioning of the masses to achieve balance. the test code will call this function:

 

/* calculate the positions of the weights such that balance can be achieved (if possible) if it is not possible to balance, return false and set left, right to empty if it is possible, return true and set left, right to the arrangement resulting in balance NOTE: there may be more than one solution, as long as yours balances, it will pass the test. fill left with the weights necessary to place to the left of center (along with the target) fill right with the weights necessary to place to the right of center balance => left + target == right */

bool findBalance(int target, const std::vector &weights, std::vector &left, std::vector &right); 

378 Recursive Strategies I am the only child of parents who weighed, measured, and priced everything; for whom what could not be weighed, measured, and priced had no existence 6. -Charles Dickens, Little Dorrit, 1857 In Dickens's time, merchants measured many commodities using weights and a two-pan balance -a practice that continues in many parts of the world today If you are using a limited set of weights, however, you can measure only certain quantities. For example, suppose that you have only two weights: a 1-ounce weight and a 3-ounce weight. With these weights you can easily measure out 4 ounces, as shown: It is somewhat more interesting to discover that you can also measure out 2 ounces by shifting the 1-ounce weight to the other side, as follows: Write a recursive function bool isMeasurable (int target, Vector &weights) that determines whether it is possible to measure out the desired target amount with a given set of weights, which is stored in the vector weights For example, suppose that sampleWeights has been initialized like this: Vector sampleWeights; sampleweights += 1, 3; Given these values, the function call isMeasurable (2, sampleWeights) should return true because it is possible to measure out 2 ounces using the sample weight set as illustrated in the preceding diagram. On the other hand

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!