A certain university in western Massachusetts has a dining hall where the cost o
ID: 3938906 • Letter: A
Question
A certain university in western Massachusetts has a dining hall where the cost of a sandwich is based upon the contents. You can make your own. The array cost details the individual costs such that entry 1 (peanut butter) costs $1.00: A group of hungry engineering students has submitted an order: So, student 1 wants peanut butter and jelly, student 2 wants bologna and cheese while the third student wants ham and cheese, for a total of (1.00+0.50)+(1.75+1.00) +(2.25+1.00) = 7.50. create the cost vector and order array: compute the cost of the order (your solution must work for a different order, i.e. more students), the number of elements is always 2 for a sandwich.Explanation / Answer
% matlab code
% cost vector
cost = [1.0, 0.5, 1.75, 2.25, 1.00];
disp("Cost: ");
disp(cost);
orderCost = 0;
students = input(" Enter number of students: ");
i = 1;
for i = 1:students
fprintf(" Enter order1 of student %d: ",i);
order(i,1) = input("");
fprintf("Enter order2 of student %d: ",i);
order(i,2) = input("");
orderCost = orderCost + cost(order(i,1)) + cost(order(i,1));
end
fprintf(" Total cost of order: %f ",orderCost);
%{
output:
Cost:
1.00000 0.50000 1.75000 2.25000 1.00000
Enter number of students: 5
Enter order1 of student 1: 1
Enter order2 of student 1: 2
Enter order1 of student 2: 1
Enter order2 of student 2: 5
Enter order1 of student 3: 4
Enter order2 of student 3: 5
Enter order1 of student 4: 3
Enter order2 of student 4: 2
Enter order1 of student 5: 1
Enter order2 of student 5: 5
Total cost of order: 14.000000
%}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.