On Wednesday 21 June 2006 16:26, Graham Smith wrote:
>
>
> def money_breakup(amt)
>
>       a = [0, 0, 0, 0]
>       j = 3
>       for i in [50, 20, 10, 5]
>               a[j] = amt / i
>               amt = amt - ( i * a[j] )
>               j = j -1
>       end
>
>       if amt != 0.0
>               puts "Error invalid amount entered"
>       end
>
>       return a
> end

I have modified the above method using a Multi-Dimensional Array. This way if 
you need to add another monetary denomination, you just add it to the array 
and the rest of the code should not need altering.

def money_breakup(amt)

        notes=[ [50, 20, 10, 5], 
                       [0, 0, 0, 0] ]
        
        j = notes[0].length - 1
        for i in notes[0]
                notes[1][j] = amt / i
                amt -= i * notes[1][j]
                j -= 1
        end

        if amt != 0.0
                puts "Error invalid amount - Remainder = #{amt.to_s} (Should be 
0)"
        end

        return notes[1]
end


-- 
Regards,

Graham Smith
_______________________________________________
coders mailing list
[email protected]
http://lists.slug.org.au/listinfo/coders

Reply via email to