Function that calculates the amount of money
14 ビュー (過去 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.
参考
カテゴリ
Help Center および File Exchange で Financial Toolbox についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!