Function that calculates the amount of money
조회 수: 8 (최근 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!