USING MATLAB For this problem you will be creating a \"tip table\" that could be
ID: 3792658 • Letter: U
Question
USING MATLAB
For this problem you will be creating a "tip table" that could be used to look up an amount to leave as a tip given the restaurant bill. In addition to the column for the bill, the table will contain columns for 15%, 18%, and 20% tips. Create and display a matrix with four columns: column 1 contains bill totals from $5 to $100 in increments of $5 column 2 contains the tip amount if the tip is 15% of the bill column 3 contains the tip amount if the tip is 18% of the bill column 4 contains the tip amount if the tip is 20% of the bill If you've constructed your matrix correctly, when it is displayed, the first few lines will look like: To receive full credit, you must make use of MATLAB's matrix and vector creation and manipulation abilities, e.g., you cannot enter the first column in element by element, columns 2 through 4 should be calculated using column 1 (ie., if you decided to modify the tip table to go from bill totals of $2 to $50 in increments of $3, exactly one line of the code you wrote would need to be changed).Explanation / Answer
%matlab code
initialBill = 5;
bill = [];
i = 1;
while initialBill <= 100
bill(i) = initialBill;
initialBill = initialBill + 5;
i = i + 1;
end
tipMatrix = zeros(length(bill),0);
for i=1:length(bill)
% total bill
tipMatrix(i,1) = bill(i);
% 15 percent tip
tipMatrix(i,2) = 0.15*bill(i);
% 18 percent tip
tipMatrix(i,3) = 0.18*bill(i);
% 20 percent tip
tipMatrix(i,4) = 0.20*bill(i);
end
disp(' BILL Tip(15%) Tip(18%) Tip(20%)');
disp(tipMatrix);
%{
output:
BILL Tip(15%) Tip(18%) Tip(20%)
5.00000 0.75000 0.90000 1.00000
10.00000 1.50000 1.80000 2.00000
15.00000 2.25000 2.70000 3.00000
20.00000 3.00000 3.60000 4.00000
25.00000 3.75000 4.50000 5.00000
30.00000 4.50000 5.40000 6.00000
35.00000 5.25000 6.30000 7.00000
40.00000 6.00000 7.20000 8.00000
45.00000 6.75000 8.10000 9.00000
50.00000 7.50000 9.00000 10.00000
55.00000 8.25000 9.90000 11.00000
60.00000 9.00000 10.80000 12.00000
65.00000 9.75000 11.70000 13.00000
70.00000 10.50000 12.60000 14.00000
75.00000 11.25000 13.50000 15.00000
80.00000 12.00000 14.40000 16.00000
85.00000 12.75000 15.30000 17.00000
90.00000 13.50000 16.20000 18.00000
95.00000 14.25000 17.10000 19.00000
100.00000 15.00000 18.00000 20.00000
%}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.