Function that calculates the amount of money
7 次查看(过去 30 天)
显示 更早的评论
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 个评论
回答(3 个)
Steven Lord
2022-12-15
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 个评论
Torsten
2022-12-15
移动:Image Analyst
2022-12-15
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 个评论
Torsten
2022-12-15
移动:Image Analyst
2022-12-15
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
2022-12-15
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 个评论
Image Analyst
2022-12-15
编辑:Image Analyst
2022-12-15
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.
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!