Search Program on this blog

Tuesday 18 August 2015

Suppose we have a pile of coins. Write a function called rich that computes how much money we have. It takes one input argument that is a row vector whose 4 elements specify the number of pennies (1 cent), nickels (5 cents), dimes (10 cents), and quarters (25 cents) that we have (in the order listed here). The output of the function is the value of the total in dollars (not cents). For example, if we had five coins, four quarters and a penny, the answer would be 1.01.

function r = rich(m)
%Input m is a row vector of length 4
%Output r is a value of the total in dollars (not cents)
%Example:
%    m = [1 0 0 4]
%    r = rich(m)
r = (m*[1 5 10 25]')/100; %values of penny, nickels, dimes and quarters divided by 100 in dollars

1 comment:

  1. Can i know why the row vector of four elements is multilpied by m?

    ReplyDelete