Question: Do problem 6.4 (recursively) on page 313. The input should be captured on a single line in a window, such as for the example, but

Do problem 6.4 (recursively) on page 313. The input should be captured on a single line in a window, such as for the example, but with the target FIRST followed by the weights, on page 306 (there will be no repeated values). Permit the user to enter all the numbers on one line: **********THERE WILL BE NO DEDUCT IF YOU DECIDE TO USE THE CONSOLE FOR INPUT. 20 11 8 7 6 5 In this case the output would be: 8 7 5 Be sure and show all possible solutions! This is important! Only partial credit if this is not done. It would be nice to have the output also in a window, but if you prefer the console, that is ok. You may assume that the largest capacity (as well as any individual weight) is 100 and the largest number of weights is 25. You may also assume that the weights are sorted from largest to smallest. The basic idea is to send a capacity and an array of weights to a recursive method and to either insert the weight or not. In either case call the method again with a reduced capacity and a shorter array OR with the same capacity and a shorter array. There should be a base case(s) for easy capacity and/or easy array. IF you do it this way, you would probably return another array which would be the potential solution array which of course would only be printed it it is truly a solution. There are multiple ways to attack the problem but recursion MUST be used! A design issue is whether to send two arrays or whether you want to send one array and return another. It is your choice! 

Do problem 6.4 (recursively) on page 313. The input should be captured

64 Write a program that solves the knapsack problem for an arbitrary knapsack capacity and series of weights. Assume the weights are stored in an array. Hint: The arguments to the recursive knapsack() function are the target weight and the array index where the remaining items start

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!