Question

In: Computer Science

You found two things at Mapua! The first item weighs weight1 and is worth value1, and...

  1. You found two things at Mapua! The first item weighs weight1 and is worth value1, and the second item weighs weight2 and is worth value2. What is the total maximum value of the items you can take with you, assuming that your max weight capacity is maxW and you can't come back for the items later?

Test Run

Value1

Weight1

Value2

Weight2

MAXWEIGHT

OUTPUT

1

10

5

6

4

8

10

2

10

5

6

4

9

16

3

10

2

11

3

1

0

4

15

2

20

3

2

15

5

2

5

3

4

5

3

6

4

3

3

4

4

4

7

3

5

3

8

10

3

Please make a matlab program

Solutions

Expert Solution

MATLAB Code:

% Test cases values are stored as matrix
matrix = [[10, 5, 6, 4, 8];[10, 5, 6, 4, 9];
    [10,2,11,3,1]; [15,2,20,3,2]; [2,5,3,4,5];
    [4,3,3,4,4];[3,5,3,8,10]];

% Values are displayed
for i = 1: length(matrix)
    output = outputFunction(matrix(i,1), matrix(i,2), matrix(i,3), matrix(i,4), matrix(i,5));
    disp("Test Run: " + i + " :: Value1: " + matrix(i,1) + ", Weight1: " + matrix(i,2) + ", Value2: " + matrix(i,3) + ...
        ", Weight2: " + matrix(i,4) + ", MaxWeight: " + matrix(i,5) + ", OUTPUT: " + output);
end

% outputFunction starts here
function output = outputFunction(value1, weight1, value2, weight2, maxweight)
    value = 0;
    if(value1 > value2)
        if(weight1 <= maxweight)
            if(weight1 + weight2 <= maxweight)
                value = value1 + value2;
            else
                value = value1;
            end
        else
            if(weight2 <= maxweight)
                value = value2;
            end
        end
    elseif(value2 > value1)
        if(weight2 <= maxweight)
            if(weight1 + weight2 <= maxweight)
                value = value1 + value2;
            else
                value = value2;
            end
        else
            if(weight1 <= maxweight)
                value = value1;
            end
        end
    elseif(value1 == value2)
        if(weight1 + weight2 <= maxweight)
            value = value1 + value2;
        else
            if(weight1 > weight2)
                if(weight1 <= maxweight)
                    value = value1;
                elseif(weight2 <= maxweight)
                    value = value2;
                else
                    value = 0;
                end
            elseif(weight2 > weight1)
                if(weight2 <= maxweight)
                    value = value2;
                elseif(weight1 <= maxweight)
                    value = value1;
                else
                    value = 0;
                end
            else
                value = value1;
            end
        end
    end
    output = value;
end

OUTPUT:


Related Solutions

What is the first one or two item you lookf or do when you review the...
What is the first one or two item you lookf or do when you review the financial statements for a company that you are seeing for the first time. What would you look for first, and why?
You found an item totaling $48,000 treated as miscellaneous expenses
You found an item totaling $48,000 treated as miscellaneous expenses. You seek clarification from the tax payer and was told that from 1 July 2020, he was appointed as a part-time consultant to a company and received a total of $48,000 for the period of 1 July 2020 to 31 December 2020. However, he did not declare it as income as he argued that it is not related to his business. Instead he treated it as miscellaneous expenses, a replacement...
List at least three things you found most interesting about interpersonal communication, explaining why you found...
List at least three things you found most interesting about interpersonal communication, explaining why you found them interesting. Explain at least two things from the chapter you think will help you in your interpersonal relationships and how you think they will be useful. Finally, describe what you consider to be your most common conflict management strategies and whether you would prefer to use a different strategy in the future. Give specific examples.
Blake Company purchased two identical inventory items. The item purchased first cost $24.00, and the item...
Blake Company purchased two identical inventory items. The item purchased first cost $24.00, and the item purchased second cost $25.00. Blake sold one of the items for $44.00. Which of the following statements is true? Ending inventory will be lower if Blake uses the weighted-average rather than the FIFO inventory cost flow method. Cost of goods sold will be higher if Blake uses the FIFO rather than the weighted-average inventory cost flow method. The dollar amount assigned to ending inventory...
Student Loan. Is it worth it? After reading the article found in the following site (you...
Student Loan. Is it worth it? After reading the article found in the following site (you may have to copy and paste), post substantive comments on the student loan issue. http://www.huffingtonpost.com/john-t-delaney/the-hidden-student-loan-debt-problem_b_6343800.html
Student Loan. Is it worth it? After reading the article found in the following site (you...
Student Loan. Is it worth it? After reading the article found in the following site (you may have to copy and paste), post substantive comments on the student loan issue. http://www.huffingtonpost.com/john-t-delaney/the-hidden-student-loan-debt-problem_b_6343800.html
REA diagrams make things easier, but the question is whether they are worth the time and...
REA diagrams make things easier, but the question is whether they are worth the time and cost. What things might you be able to see using an REA diagram that might not be apparent otherwise?  
What is a bond indenture and what sorts of things are found in the indenture?
What is a bond indenture and what sorts of things are found in the indenture?
Which of the following item cannot be found on a fims income statement?
Which of the following item cannot be found on a fims income statement?inventoriestaxsalescogs
Determine the labor costs required to manufacture the 100th item if the first item requires 50...
Determine the labor costs required to manufacture the 100th item if the first item requires 50 days to produce and the learning curve is 82%. The manufacturing process requires two 9-hour shifts working each day, with 8 employees per shift. The employees are paid $17.50 per hour.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT