tags:

views:

113

answers:

3

Hey everyone, could anyone help me out with doing the Dollars in this function. It does the change just fine but it does all of the amount back in change and i want to big bills ($1,$5,$10, & $20)'s in dollars and the change in Q/D/N/P's.

Dim Quarters As Integer
Dim Dimes As Integer
Dim Nickels As Integer
Dim Pennies As Integer

Sub GetChange(ByVal Amount As Currency, ByRef Quarters As Integer, ByRef Dimes As Integer, ByRef Nickels As Integer, ByRef Pennies As Integer)
Dim Cents As Integer

   Cents = Amount * 100
   Quarters = Cents \ 25
   Cents = Cents Mod 25
   Dimes = Cents \ 10
   Cents = Cents Mod 10
   Nickels = Cents \ 5
   Pennies = Cents Mod 5

End Sub

Call GetChange(5.56, Quarters, Dimes, Nickels, Pennies)

Any help would be awesome! :o)

Update, solved

Private Sub theUSChange(Amount)
    Dim USCurrency(9) As Currency
    Dim USCurrencyNames(9) As Currency
    Dim Amount As Currency
    Dim Result As Currency
    Dim I As Integer

    USCurrencyNames(0) = " Pennies: "
    USCurrency(0) = 0.01
    USCurrencyNames(1) = "   Dimes: "
    USCurrency(1) = 0.05
    USCurrencyNames(2) = " Nickles: "
    USCurrency(2) = 0.1
    USCurrencyNames(3) = "Quarters: "
    USCurrency(3) = 0.25

    USCurrencyNames(4) = "      $1: "
    USCurrency(4) = 1
    USCurrencyNames(5) = "      $5: "
    USCurrency(5) = 5
    USCurrencyNames(6) = "     $10: "
    USCurrency(6) = 10
    USCurrencyNames(7) = "     $20: "
    USCurrency(7) = 20
    USCurrencyNames(8) = "     $50: "
    USCurrency(8) = 50
    USCurrencyNames(9) = "    $100: "
    USCurrency(9) = 100

    For I = UBound(USCurrency) To LBound(USCurrency) Step -1
        Do While Amount >= USCurrency(I)
            Amount = Amount - USCurrency(I)
            Result = Result + 1
        Loop
        Debug.Print(USCurrencyNames(I) & Result)
        Result = 0
    Next
End Sub

call theUSChange(5.77)

OUTPUT:
    $100: 0
     $50: 0
     $20: 0
     $10: 0
      $5: 1
      $1: 0
Quarters: 3
 Nickles: 0
   Dimes: 0
 Pennies: 2

David

+1  A: 

Just add values for your bills. You already have a start.

Brad
A: 

This problem is worked out as an example in Concrete Mathematics using generating functions.

John D. Cook
John, I believe you're thinking of the "how many ways to change a dollar problem".
I. J. Kennedy
You're right. It's not exactly the same problem, but it's entirely analogous.
John D. Cook
A: 

First, I'd convert the $ amount you're dealing with to pennies.

Next find out how many whole coins/bills of each denomination you can 'fit' in the total number of pennies you're currently working with, starting with the highest denomination of each bill/coin you want to give change in.

If it's > 0, calculate the number of pennies you have left over after deducting for the change you just made and check the next highest valued denomination, etc.

These checks and calculations can be generalized and pulled into a function, like this:

' intTotalPennies is the total number of pennies youre working with when this function is called.  Notice it is a ByRef call; you want to keep a running total of pennies left over.
Private Function GetDenominationCount(intDenominationUnitValue As Integer, ByRef intTotalPennies As Integer) As Integer
    Dim intCount As Integer
    intCount = intTotalPennies \ intDenominationUnitValue
    intTotalPennies = intTotalPennies - (intCount * intDenominationUnitValue)
    GetDenominationCount = intCount
End Function

For each bill/coin denomination you're interested in making change for call the function like this:

' Snip calls for $100, $50, $20, $10,...

' The number of $5 bills as change.    
Dim int5DollarCount As Integer
int5DollarCount = GetDenominationCount(500, intTotalPennies)

Dim int1DollarCount As Integer
int1DollarCount = GetDenominationCount(100, intTotalPennies)

Dim intQuarterCount As Integer
intQuarterCount = GetDenominationCount(25, intTotalPennies)

' Snip calls for dimes and nickels.

Of course anything left over will already be in pennies.

The nice thing about this approach is if you want to tack on calculating change for other denominations (such as the $3 bill they're considering), you can easily do so. You can also explicitly remove denominations (the register's out of quarters, for example) without any problem.

Jay Riggs