Question

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 with items

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

Code:

r1=randi(1000,10, 1)   
w=1000
c=0
sort(r1,'descend')
for i= 1:10
if r1(i)<=w
w=w-r1(i)
c=c+r1(i)
end
end

disp(w)
disp(c)


Image of the Code:

Image of Input and Output:

Explanation:

  • First we took a random array of integers.
  • Then we sorted them in descending order.
  • Then we take the highest weight and checked it with truck's capacity. If the wight is less than the capacity then it can be loaded otherwise move on to the next weight.
  • If the weight is loaded then we reduce the capacity and increase the load of the truck.
  • This is Greedy approach.

If the answer helped please upvote. It means a lot. For any query comment below.

Add a comment
Know the answer?
Add Answer to:
Please use MATLAB You are given an array, weights, that contains the weights of some cargo...
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
  • Please help me answer all 3 questions in this case. The numbers and questions asked, in...

    Please help me answer all 3 questions in this case. The numbers and questions asked, in this case, is different than the other cases similar to it, so please don't copy and past other answers from the same case. Also if you could please provide steps to how you're getting the answers, if possible show it in excel with formulas, please. Answer the following questions using the information provided above. Show all of your calculations in an Excel file. Make...

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
ADVERTISEMENT