Question

In: Computer Science

MATLAB PROBLEM You are given an array, weights, that contains the weights of some cargo items...

MATLAB PROBLEM

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 use a "greedy" algorithm. That is, always load the heaviest item that will still fit on the truck. Keep loading until the remaining capcity of the truck is less than any of the remaining cargo items. For example, if the weights were {700, 400, 250, 100, 60}, you would load the 700-pound item, then the 250-pound item. The remaining items would be too heavy to fit on the truck. The weights will be generated randomly, so you cannot hard-code the answer - you must write a general algorithm to do it. 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.

CODE PROVIDED BELOW

%The weights of the cargo items are generated at random

%They are sorted in descending order

weights = sort(randi(500, 1, randi(10)), 'descend')

capacity = 1000; %cargo capacity of the truck

Solutions

Expert Solution

n=input('Enter how many cargo items: ')

maximum=input('Enter maximum value of cargo items: '

weights = randi(maximum,n,1);

weights = sort(weights);

fprintf("The randomly generated weights of cargo items are: \n")

disp(weights)

capacity=input('Enter capacity of truck: ')

loaded = 0;

i=n;

while(i>0)

                if (weights(i)<=capacity)

                                loaded = loaded + weights(i); %adding the weight to loaded

                                capacity = capacity - weights(i);%removing weight from the loaded

                end

                i=i-1;

end


Related Solutions

Matlab Given an array of monthly rainfall that covers some number of years (where each column...
Matlab Given an array of monthly rainfall that covers some number of years (where each column is a month and each row is a year) create a function YEARLY that prints the average rainfall for each year For example, if the information below were to be stored in a 5x12 matrix RAIN... J F M A M J J A S O N D 2003 1 2 1 2 1 2 1 2 1 2 1 2 2004 1 1...
45. Which statement correctly passes the array items to method takeArray? Array items contains 10 elements....
45. Which statement correctly passes the array items to method takeArray? Array items contains 10 elements. a. takeArray(items[9]) b. takeArray(items[]) c. takeArray(items) d. Arrays cannot be passed to methods – each item must be sent to the method separately. 46. When an argument is passed by reference, ____________. a. a copy of the argument’s value is passed to the called method b. the original value is removed from memory c. changes to the argument do not affect the original variable’s...
Problem 1: [10 Marks] Given a generic array with ‘n’ items (not only integers). Give a...
Problem 1: [10 Marks] Given a generic array with ‘n’ items (not only integers). Give a solution for checking whether there are any duplicate elements in the array or not? You just need to return a boolean (true or false). State the complexity of your solution. Can you come up with another solution that has O(n logn) complexity? Explain. Problem 2: [10 Marks] Now consider the same problem as problem 1, but this time you only have positive integer numbers...
You are given an array of n elements, and you notice that some of them are...
You are given an array of n elements, and you notice that some of them are duplicates, that is, they appear more than once in the array. Show how to remove all duplicates from the array in time O( n log2 n ).
1. Given an array of integers a dimension n. If the array contains the same number...
1. Given an array of integers a dimension n. If the array contains the same number of even and odd elements get (a1 + an) (a2 + an-1) ... 2. Given an array of integers dimension n. All array elements with even numbers preceding the first element to the maximum, multiplied by the maximum. 3. Given an array of dimension n. Insert after each zero element of the element in the middle (or the amount of secondary elements for even...
Consider the problem of finding if an array contains a pair of integers with a sum...
Consider the problem of finding if an array contains a pair of integers with a sum of 100. The array contains n integers. a. Define the signature (header) of a C++ function that solves this problem (hint: inputs/outputs of function) b. Write the pseudocode or C++ body of the function (extra credit: write the most efficient algorithm) c. What is the asymptotic complexity of your algorithm? d. What would be the complexity of the best algorithm for this problem if...
Consider the knapsack problem with the capacity C = 8 and 5 items with weights 6,...
Consider the knapsack problem with the capacity C = 8 and 5 items with weights 6, 4, 3, 7, 1. Find which items will exactly fill the knapsack using dynamic programming solution introduced in class. Show all your work. use I/O knapsack
Array with Pointers Find Continuous Sub-Array C++ Problem: Given an unsorted array A of size N...
Array with Pointers Find Continuous Sub-Array C++ Problem: Given an unsorted array A of size N of non-negative integers, find a continuous sub-array which adds to the given number. Declare dynamic arrays and use only pointers syntax (no [ ]’s or (ptr+i) stuff.     Input will be the number of input values to enter followed by the sum to compare with. Print out the continuous sub-array of values that are equal to sum or the message ‘No sum found’. There...
The Monty Hall problem solved on Matlab. Please show code. You are given a choice between...
The Monty Hall problem solved on Matlab. Please show code. You are given a choice between three doors. Behind one door is a new car and behind the other two doors is a goat. You first pick one of the doors at random. Then, a door with a goat is opened for you. You are then given the option to switch your door to the other unopened door. Should you do this? What are your odds of winning if you...
, we are given a set of n items. Each item weights between 0 and 1....
, we are given a set of n items. Each item weights between 0 and 1. We also have a set of bins (knapsacks). Each bin has a capacity of 1, i.e., total weight in any bin should be less than 1. The problem is to pack the items into as few bins as possible. For example Given items: 0.2, 0.5, 0.4, 0.7, 0.1, 0.3, 0.8 Opt Solution: Bin1[0.2, 0.8], Bin2[0.5, 0.4, 0.1], Bin3[0.7, 0.3] Each item must be placed...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT