makeChange - money = round(money*100/100%code given to fix...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
function few = makeChange(money) %Given an amount of money in decimal form (i.e. 1.50), write a function %makeChange that finds the fewest number of pieces of standard U.S. %currency required to reach the inputted amount of money. The function %should return this number as a double, and should only consider one %dollar bills, quarters, dimes, nickels, and pennies when calculating %the number of pieces of money required. %NOTICE: %Due to the way that MATLAB handles numbers, the following line of code %is required to make your code run correctly. Add this line of code %on the line immediately after your function header, before any other %code, where in is the input variable of your function: %in = round(in*100)/100; %Constraints: %You must use recursion for this problem.
Background image of page 1
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: money = round(money*100)/100; %code given to fix Matlab few = 0; %initialize count at 0 if money==0 %terminating condition, because it is when there is no money left few = 0; %few will be 0 because no money is left elseif money>=1 %checks to account for all of the dollar bills first, because it is the %largest amount of money few = 1 + makeChange(money-1); %adds one to the recursively called makeChange and subtracts the dollar %found in the money elseif mod(money,.25)==0 %checks to account for all of the quarters few = 1 + makeChange(money-.25); elseif mod(money,.10)==0 few = 1 + makeChange(money-.10); elseif mod(money,.05)==0 few = 1 + makeChange(money-.05); elseif mod(money,.01)==0 few = 1 + makeChange(money-.01); end end...
View Full Document

This note was uploaded on 10/21/2010 for the course CS 1371 taught by Professor Stallworth during the Spring '08 term at Georgia Tech.

Ask a homework question - tutors are online