Function that calculates the amount of money

8 Ansichten (letzte 30 Tage)
Lewis HC
Lewis HC am 15 Dez. 2022
Bearbeitet: Image Analyst am 15 Dez. 2022
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!

Antworten (3)

Steven Lord
Steven Lord am 15 Dez. 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)
sz = 1×2
1 10
numRows = sz(1)
numRows = 1
numCols = sz(2)
numCols = 10

Torsten
Torsten am 15 Dez. 2022
Verschoben: Image Analyst am 15 Dez. 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 Kommentare
Torsten
Torsten am 15 Dez. 2022
Verschoben: Image Analyst am 15 Dez. 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.
Lewis HC
Lewis HC am 15 Dez. 2022
I'm not sure if I'm right with this code:
thanks for your help!

Melden Sie sich an, um zu kommentieren.


Image Analyst
Image Analyst am 15 Dez. 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 Kommentare
Lewis HC
Lewis HC am 15 Dez. 2022
Thanks dear friend, I was doing something similar (code attached) but nevertheless it asks me to calculate this: rico[(1,1,1,1)]
Image Analyst
Image Analyst am 15 Dez. 2022
Bearbeitet: Image Analyst am 15 Dez. 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.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Financial Toolbox finden Sie in Help Center und File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by