Answered step by step
Verified Expert Solution
Link Copied!

Question

00
1 Approved Answer

Please use MATLAB You are given an array, weights, that contains the weights of some cargo items in pounds. You want to load a truck

Please use MATLAB

image text in transcribed

You are given an array, weights, that contains the weights of some cargo items in pounds. You want to load a truck with items from the list, up to its capacity. The truck has a maximum capacity of 1,000 pounds. For this problem, you want to find the maximum possible weight that can be loaded onto the truck. The greedy algorithm is not guaranteed to achieve this optimal result. For example, if the weights were (25 452 473 246 245), you would load a total of 989 pounds = 473+246+245+25 This differs from the greedy algorithm, which would load 950 pounds = 473+452+25. A brute-force approach, which is inefficient but guaranteed to find the right result, is to generate all possible permutations of the weights, using the built-in function perms(), and apply a "dumb" algorithm to each permutation, which simply loads the weights in order until the capacity is reached. Applying this dumb algorithm to all possible permutations and taking the best result always finds the optimal solution. The weights will be generated randomly, so you cannot hard-code the answer - you must write a general algorithm to do it. Because the inefficient algorithm described above scales exponentially with the number of weights, it is only practical for a relatively small number. In the problem there will be no more than 10 items. Once the loading is complete, print a message indicating how many pounds of cargo were loaded onto the truck. NOTE: To satisfy the auto-grader, update the variable capacity so that it contains the remaining capacity of the truck. For example, if 943 pounds of cargo are loaded, capacity should be 57

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions