Results 1 to 13 of 13

Thread: Help!!! Making Change

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2006
    Posts
    8

    Help!!! Making Change

    Someone help please, Im trying to create a program where the user enters in a certain amount of money and I recursively, tell them how many ways they can make change from that amount of money. I dont have to state what the change is just how many different ways. Heres wut i got

    Code:
    Dim ways As Integer
        Private Sub btnStart_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnStart.Click
            Dim cash As Double
            cash = CDbl(txtInput.Text)
            If (cash <= 0) Then
                lblOutput.Text = ("You must have a positive amount of money to make change")
            Else
                Change(cash)
                lblOutput.Text = ("There are " & ways & " to make change from $" & cash & ".")
            End If
        End Sub
    
        Private Sub Change(ByVal cash As Double)
            Dim newcash As Double
            If (cash >= 2.0) Then
                newcash = cash - 2
                Change(newcash)
            End If
            If (cash >= 1.0) Then
                newcash = cash - 1
                Change(newcash)
            End If
            If (cash >= 0.25) Then
                newcash = cash - 0.25
                Change(newcash)
            End If
            If (cash >= 0.1) Then
                newcash = cash - 0.1
                Change(newcash)
            End If
            If (cash >= 0.05) Then
                newcash = cash - 0.05
                Change(newcash)
            End If
            If (cash >= 0.01) Then
                newcash = cash - 0.01
                Change(newcash)
            End If
            If (cash = 0) Then
                ways = ways + 1
            End If
        End Sub
    
        Private Sub btnReset_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnReset.Click
            lblOutput.Text = ("")
            txtInput.Text = ("")
            ways = 0
        End Sub
    End Class

  2. #2
    Hyperactive Member Chathura's Avatar
    Join Date
    Nov 2005
    Location
    Sri Lanka
    Posts
    345

    Re: Help!!! Making Change

    You have not indicated what is your problem here. However I think there is a problem with your logic. I think you always get "one way" as the result. If that the problem or other please put it here.

  3. #3

    Thread Starter
    New Member
    Join Date
    Nov 2006
    Posts
    8

    Re: Help!!! Making Change

    Yes, the problem is that i keep only getting one way. How do i make it so i get alll different ways?

  4. #4
    Hyperactive Member Chathura's Avatar
    Join Date
    Nov 2005
    Location
    Sri Lanka
    Posts
    345

    Re: Help!!! Making Change

    Ok. Your logic should be changed. Actually by now you are getting the best way. Now what you have to do is,
    1. count how many 2.0 s, 1.0 s, 0.25s, 0.05 and 0.01s in your best way.
    for ex: 5.53 will give 2 for 2.0s, 1 for 1.0s 0 for 0.25s 2 for 0.05s and 3 for 0.01s.
    2. Now write these methods
    i.) Change2s - This should find all the ways that other values can represent 2.0
    also you can assign it to a constant because it's always the same. Say this is w. As the count of 2.0s is 2 in my ex. w should be multiplied by factorial of 2.0s count(i.e. 2!=2*1) . This will result how many ways to represent 2.0 count.
    ii) Change1s- This should find all the ways that other values (except 2.0) can represent 1.0. This also a constant. You should do the same as in i.)
    ...
    ...
    Sum of all these 4 methods +1 will give you the correct result.

  5. #5

    Thread Starter
    New Member
    Join Date
    Nov 2006
    Posts
    8

    Re: Help!!! Making Change

    my main concern is there any mathematical way at gettin at how many ways its possible, i.e. like factorials or somthing?

  6. #6
    Hyperactive Member Chathura's Avatar
    Join Date
    Nov 2005
    Location
    Sri Lanka
    Posts
    345

    Re: Help!!! Making Change

    Quote Originally Posted by needhelp1234
    my main concern is there any mathematical way at gettin at how many ways its possible, i.e. like factorials or somthing?
    Yes, certainly,. I think already I've described it.
    Say you have a method to find factorial of an integer
    TotalWays=(fact(countof2.0s)*(No.of ways to make 2.0))+(fact(Countof1.0)*(No.of ways to make1.0)) + (fact(Countof 0.25)*(No.of ways to make0.25)) + (fact(Countof0.05)*(No.of ways to make0.05))+ (fact(Countof0.01)*(No.of ways to make0.01))
    Is the equation.
    You may have difficulty to find No.of ways to make "SomeValue"
    I'll describe
    As the 0.01 is the smallest unit you have only one way to make 0.01
    As the 0.05 is the next smaller, you have only two ways to make 0.05
    i.e. 1*0.05 or 5* 0.01
    As the 0.25 has two smaller units, you can make it in following ways
    i.) 1*0.25 or 5*0.05 or (4*0.05 And 5*0.01) or (3*0.05 And 10*0.01) or...or (25*0.01)
    i.e Therefore there are 7 ways
    Then 1.00 has three smaller units. You can make it in following ways
    i) 1*1.00 or 4*0.25 or (3*0.25 And rest 0.25 in above 7 ways) or (2*0.25 And rest 0.50 in above 7 ways)....
    i.e therefore there are 24 ways

    In this way you can build some model. And try to go through

  7. #7
    Hyperactive Member Chathura's Avatar
    Join Date
    Nov 2005
    Location
    Sri Lanka
    Posts
    345

    Re: Help!!! Making Change

    I'm sorry, Your code is not so bad. It gives a result which is correct to some extend. I'll give you a good code if I could find some time to do it. And also I didn't see that you have another value of 0.1

  8. #8

    Thread Starter
    New Member
    Join Date
    Nov 2006
    Posts
    8

    Re: Help!!! Making Change

    Thanks, a bunch. Sri Lankan eh, that makes 2 of us. But yeah im in class working on it ill post what i get at the end of class. Thanks for helping me out. It's due tomorrow so I'll try to get the best code I can.

    K This is what I dont know exactly how to do. I know how to individually get the ways with each coin but now how can i make a function that calculates the total amount of ways we can make change using all the coins not just one at a time. So two coins, 3, 4, etc at a time. Remember: I want to find the amount of ways to find change not which coins to use.
    Example : 0.05 has 2 ways 1 nickle 5 pennies...etc
    0.1 has 4 ways, 1 dime, 2 nickles, 1 nickle 5 pennies, 10 pennies
    Last edited by needhelp1234; Nov 30th, 2006 at 09:53 AM.

  9. #9

    Thread Starter
    New Member
    Join Date
    Nov 2006
    Posts
    8

    Re: Help!!! Making Change

    so yeah we tried and tried and were down to my last 2 housr before i sleep, and i am sooo lost and its due tomorrow oh my goodness. There were 5 programs i got all 4 and this ones givng me too much trouble...help!

  10. #10
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    Re: Help!!! Making Change

    what info do you have at the begining? Take a look on "Function Choose" in the"Unique Combinations" in my signature. It mitht help.

  11. #11

    Thread Starter
    New Member
    Join Date
    Nov 2006
    Posts
    8

    Re: Help!!! Making Change

    how can i use ur unique comibnations, recursively im startin to see how i can use it buh styll kinda blurry

  12. #12
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    Re: Help!!! Making Change

    The function "Choose" in the class works this way: let say there are 4 players and we want to know how many unique selection of two players we can have; whis wold be 6 combinations. Now I am not sure why you need to have any recursive calls to a function. If you want to call the function recursively then there should be conditions that would end the recursive calls. What condition would that be?

  13. #13

    Thread Starter
    New Member
    Join Date
    Nov 2006
    Posts
    8

    Re: Help!!! Making Change

    the assignment is to get it recursively but i have no clue this is the "challenge assignment"

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width