In: Computer Science
MATLAB CODE
Let’s say you need to write a small script that takes in the total amount of money entered, and a cost, and returns the correct change in quarters/dimes/nickels/pennies.
function value = get_coin_value(coin)
%Set value to be the correct number based on coin.
%For example, if coin == 'q', value = 25
THIS IS THE CODE SO FAR FOR THE PARTS BEFORE THE ONE I NEED HELP WITH
if coin == 'q'
value = 25;
elseif coin == 'd'
value = 10;
elseif coin == 'n'
value = 5;
elseif coin == 'p'
value = 1;
else
value = 0;
end
%}
function total = insert_coins
%your code here
total = 0;
%Loop to keep asking user for coin till total is less than 115 cents
while(total<115)
coin = input('Enter q for quarter, d for dime, n for nickel,p for penny:','s');
total = total + get_coin_value(coin);
end
disp("Your NAU power juice has been dispensed")
end
THIS IS THE CODE I NEED HELP WITH GETTING TO WORK
function [quarters, dimes, nickels, pennies] = get_change(total)
%your code here
cost = 115;
total = insert_coins;
change = total - cost;
while change >= 25
quarters = 'q' + 1;
change = change - 25;
end
while change >= 10
dimes = 'd' + 1;
change = change - 10;
end
while change >= 5
nickels = 'n' + 1;
change = change - 5;
end
while change >= 1
pennies = 'p' + 1;
change = change - 1;
end
disp('Total change: ' + (total - cost))
disp('Quarters: ' + quarters)
disp('Dimes: ' + dimes)
disp('Nickels: ' + nickels)
disp('Pennies: ' + pennies)
end
ANSWER:
I have provided the properly commented
code in both text and image format so you can easily copy the code
as well as check for correct indentation.
I have provided the output image of the code so you can easily
cross-check for the correct output of the code.
Have a nice and healthy day!!
CODE TEXT (function get_change)
function [quarters, dimes, nickels, pennies] = get_change(total,cost)
%your code here
change = total - cost;
% defining quarters, dimes , nickels and pennies counter
quarters = 0; dimes= 0; nickels = 0; pennies = 0;
while change >= 25
quarters = quarters + 1;
change = change - 25;
end
while change >= 10
dimes = dimes + 1;
change = change - 10;
end
while change >= 5
nickels = nickels + 1;
change = change - 5;
end
while change >= 1
pennies = pennies + 1;
change = change - 1;
end
fprintf('Total change: %d\n',(total - cost));
fprintf('Quarters: %d\n',quarters);
fprintf('Dimes: %d\n',dimes);
fprintf('Nickels: %d\n',nickels);
fprintf('Pennies: %d\n',pennies);
end
CODE IMAGE (function get_change)
OUTPUT IMAGE