View Single Post
Old 02-04-2007, 01:36 PM   #4 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,713
redhead is on a distinguished road
Here is one in ML
Code:
val values = [(100, "Dollars"), (25, "Quarters"), 
      (10, "Dimes"), (5, "Nickels"), (1, "Pennies")];

fun head([]) = []
    | head((x,y)::xs) = x :: head(xs);

fun tail([]) = []
    | tail((x,y)::xs) = y :: tail(xs);

fun combine([],[]) = []
    | combine(x, []) = []
    | combine([], y) = []
    | combine(x::xs, y::ys) = (x,y) :: combine(xs,ys);

fun count([], value, result) = result
    | count(x::xs, value, result) =
        if value = x then
                count(xs, value, result +1)
        else
                count(xs, value, result);

fun count_all([], []) = []
    | count_all([], y) = []
    | count_all(x, []) = []
    | count_all(x, y::ys) = count(x,y,0) :: count_all(x, ys);

fun change (coinvals, 0) = []
  | change ([], amount) = []
  | change (c::coinvals, amount) =
      if amount<c then change(coinvals, amount)
                  else c :: change(c::coinvals, amount-c);

fun make_change(value) = 
  combine(
      count_all(
         change(
            head(values), value
         ), head(values)
      ), tail(values)
  );
Quote:
I need a write a programming code for that question
I'm not here to do your homework, show us how far you've gotten, then we might help...

If you know functional programming, then my ML version will provide great help.. If not, you're not any further than you were to begin with..
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote