using matlab: Number of pennies Complete the function ConvertToPennies() so that
ID: 1996211 • Letter: U
Question
using matlab:
Number of pennies
Complete the function ConvertToPennies() so that the function returns the total number of pennies given a number of dollars and (optionally) a number of pennies.
Ex: ConvertToPennies(5 , 6) returns 506 and ConvertToPennies(8) returns 800.
complete the code:
function totalPennies = ConvertToPennies(numDollars, numPennies)
% numDollars: Number of dollars
% numPennies: Number of pennies (optional)
% Function output: Total number of pennies
% ConvertToPennies() returns the total number of pennies given
% the number of dollars and optionally a number of pennies
totalPennies = 0;
end
Explanation / Answer
function totalPennies = ConvertToPennies(numDollars, numPennies)
% numDollars: Number of dollars
% numPennies: Number of pennies (optional)
% Function output: Total number of pennies
% ConvertToPennies() returns the total number of pennies given
% the number of dollars and optionally a number of pennies
totalPennies = 0;
if nargin==2%nargin return number of inputs passed
totalPennies = (numDollars*100)+numPennies;
else
totalPennies = numDollars*100;
end
end
Command Window Output:
>> ConvertToPennies(5 , 6)
ans =
506
>> ConvertToPennies(8)
ans =
800
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.