In: Computer Science
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
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: