Function that calculates the amount of money
8 views (last 30 days)
Show older comments
Write a function called rico that calculates how much money you have. The function must take an input argument that is a four-element row vector specifying the number of pennies, nickels, dimes, and quarters in order of list. The output of the function should be the total value in dollars, my small code is:
function F=rico(pennie,nickel,dime,quarter);
F=1*pennie+5*nickel+10*dime+25*quarter;
dollars=F/100;
end
Thank you for helping me dear friends!
0 Comments
Answers (3)
Steven Lord
on 15 Dec 2022
Your function fails to satisfy this requirement of your homework assignment:
The function must take an input argument that is a four-element row vector specifying the number of pennies, nickels, dimes, and quarters in order of list.
But if you don't want to (or are not allowed to) modify this function, you could write a wrapper function around this one that accepts the four-element row vector your assignment requires the function to take and "unpacks" it to call the function you've written with four separate input arguments. Consider this example and think about how you could adapt it to your assignment.
sz = size(1:10)
numRows = sz(1)
numCols = sz(2)
0 Comments
Torsten
on 15 Dec 2022
Moved: Image Analyst
on 15 Dec 2022
Either change
function F=rico(pennie,nickel,dime,quarter);
to
function dollars=rico(pennie,nickel,dime,quarter);
or change
dollars=F/100;
to
F=F/100;
3 Comments
Torsten
on 15 Dec 2022
Moved: Image Analyst
on 15 Dec 2022
And your function is supposed to have the form
function dollars = rico(x)
where x is 1 (1x4) row vector with x(1) = pennie,...
This function has 1 input , yours has 4.
Image Analyst
on 15 Dec 2022
No, you're not quite right in your latest code posted. It needs to be
% Test code
% coins = [1,1,1,1];
% dollars = rico(coins)
function dollars = rico(coins)
numPennies = coins(1);
numNickels = coins(2);
% etc.
% Then the rest of your code, almost as you had it.
F = 1*numPennies+5*numNickels+10*numDimes+25*numQuarters;
dollars=F/100;
2 Comments
Image Analyst
on 15 Dec 2022
Edited: Image Analyst
on 15 Dec 2022
That's not right. The requirement says "The function must take an input argument that is a four-element row vector" so you need to have ONE input argument, not four. It needs to be as I showed.
Also, you called it incorrectly. The parentheses need to be OUTSIDE the brackets, not inside. It's the brackets that makes your 4 1's into a single 4-element row vector, which is what is wanted.
By the way, I completed and tested my code and it works.
See Also
Categories
Find more on Financial Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!