Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

MatLab: Help is needed Write a function called givemechange.m whose purpose is t

ID: 3689540 • Letter: M

Question

MatLab: Help is needed

Write a function called givemechange.m whose purpose is to determine how much change should be returned when given a price and an amount tendered. There should be two input parameters, price and tendered, in that order. There should be two output parameters, change and changelist, in that order. The output parameter change is simply the total change and the output parameter changelist is a vector containing the number of 20s, 10s, 5s, 1s, quarters, dimes, nickels, and pennies that are needed to return that amount of change. For example, the function call [C, L] =givemechange (35.27,100) should return C=64.73 and L=[3,0,0,4,2,2,0,3].

Explanation / Answer

function [C,L] = givemechange(price,ten)
C = ten - price;
L = [1.7,0,0,0,0,0,0,0];
tot = C;
L(1)= floor(tot/20);
tot = tot - 20*L(1);
L(2)= floor(tot/10);
tot = tot - 10*L(2);
L(3)= floor(tot/5);
tot = tot - 5*L(3);
L(4)= floor(tot/1);
tot = tot - L(4);
L(5)= floor(tot*100/25);
tot = tot - 0.25*L(5);
L(6)= floor(tot*100/10);
tot = tot - 0.1*L(6);
L(7)= floor(tot*100/5);
tot = tot - 0.05*L(7);
L(8)= tot*100;
tot = tot - 0.01*L(8);
end
[C,L]=givemechange(35.27,100);
disp(C);
disp(L);