Change program

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • David Horne

    Change program

    I have been working on a change program for one of my classes that I
    am taking. I got it to work, but I had a section of code that I was
    trying to make more efficent. The section of code is below

    m_twentydollars = Math.Floor(m_am ount / 2000)
    m_tendollars = Math.Floor((m_a mount Mod 2000) / 1000)
    m_fivedollars = Math.Floor((m_a mount Mod 1000) / 500)
    m_onedollar = Math.Floor((m_a mount Mod 500) / 100)
    m_halfdollars = Math.Floor((m_a mount Mod 100) / 50)
    m_quarters = Math.Floor((m_a mount Mod 50) / 25)
    m_dimes = Math.Floor((m_a mount Mod 25) / 10)
    m_nickels = Math.Floor((m_a mount - (2000 *
    m_twentydollars ) - _
    (1000 * m_tendollars) - (500 * m_fivedollars) - _
    (100 * m_onedollar) - (50 * m_halfdollars) - (25 *
    m_quarters) - _
    (10 * m_dimes)) / 5)

    m_pennies = Math.Floor((m_a mount Mod 5) / 1)

    Is there a way to shorten the equation for m_nickles? This was the
    only way I could get m_nickels to work out right.
  • J French

    #2
    Re: Change program

    On 17 Nov 2003 17:57:34 -0800, [email protected] om (David Horne) wrote:
    [color=blue]
    >I have been working on a change program for one of my classes that I
    >am taking. I got it to work, but I had a section of code that I was
    >trying to make more efficent. The section of code is below
    >
    > m_twentydollars = Math.Floor(m_am ount / 2000)
    > m_tendollars = Math.Floor((m_a mount Mod 2000) / 1000)
    > m_fivedollars = Math.Floor((m_a mount Mod 1000) / 500)
    > m_onedollar = Math.Floor((m_a mount Mod 500) / 100)
    > m_halfdollars = Math.Floor((m_a mount Mod 100) / 50)
    > m_quarters = Math.Floor((m_a mount Mod 50) / 25)
    > m_dimes = Math.Floor((m_a mount Mod 25) / 10)[/color]

    Yerk !

    That looks as if you'll be handing out a lot more change than you
    ought

    Residue = m_Amount
    m_twentydollars = Math.Floor( Residue / 2000)
    Residue = Residue - ( m_twentydollars * 2000 )

    Now that can be encapsulated in a Function

    Units = CalcSum( Residue, UnitCents )

    so your calculation could be :-

    For L9 = 1 To Max
    Units( L9 ) = CalcSum( Residue, UnitCents( L9 ) )
    Next

    The setup would be :-

    UnitCents(1) = 2000
    UnitCents(2) = 1000
    UnitCents(3) = 500
    UnitCents(4) = 100
    UnitCents(5) = 50
    etc

    The thing about coding is 'abstraction'
    - cut things down to something generic
    - then re-use the 'generic' routine ... mercilessly



    Comment

    • David Horne

      #3
      Re: Change program

      erewhon@nowhere .com (J French) wrote in message news:<3fba15a5. 184300436@news. btclick.com>...[color=blue]
      > On 17 Nov 2003 17:57:34 -0800, [email protected] om (David Horne) wrote:
      >[color=green]
      > >I have been working on a change program for one of my classes that I
      > >am taking. I got it to work, but I had a section of code that I was
      > >trying to make more efficent. The section of code is below
      > >
      > > m_twentydollars = Math.Floor(m_am ount / 2000)
      > > m_tendollars = Math.Floor((m_a mount Mod 2000) / 1000)
      > > m_fivedollars = Math.Floor((m_a mount Mod 1000) / 500)
      > > m_onedollar = Math.Floor((m_a mount Mod 500) / 100)
      > > m_halfdollars = Math.Floor((m_a mount Mod 100) / 50)
      > > m_quarters = Math.Floor((m_a mount Mod 50) / 25)
      > > m_dimes = Math.Floor((m_a mount Mod 25) / 10)[/color]
      >
      > Yerk !
      >
      > That looks as if you'll be handing out a lot more change than you
      > ought
      >
      > Residue = m_Amount
      > m_twentydollars = Math.Floor( Residue / 2000)
      > Residue = Residue - ( m_twentydollars * 2000 )
      >
      > Now that can be encapsulated in a Function
      >
      > Units = CalcSum( Residue, UnitCents )
      >
      > so your calculation could be :-
      >
      > For L9 = 1 To Max
      > Units( L9 ) = CalcSum( Residue, UnitCents( L9 ) )
      > Next
      >
      > The setup would be :-
      >
      > UnitCents(1) = 2000
      > UnitCents(2) = 1000
      > UnitCents(3) = 500
      > UnitCents(4) = 100
      > UnitCents(5) = 50
      > etc
      >
      > The thing about coding is 'abstraction'
      > - cut things down to something generic
      > - then re-use the 'generic' routine ... mercilessly[/color]

      J French

      Thanks for your help. The reason that I could not use an array and the
      function that you used was of the requirements in the project. I could
      pass the amount to the function.

      David

      Comment

      • J French

        #4
        Re: Change program

        On 18 Nov 2003 12:24:17 -0800, [email protected] om (David Horne) wrote:

        <snip>[color=blue]
        >
        >Thanks for your help. The reason that I could not use an array and the
        >function that you used was of the requirements in the project. I could
        >pass the amount to the function.
        >
        >David[/color]

        Eh ?

        I gather this is 'homework' - normally I'm reluctant to help
        (there are exceptions to that rule - if talent is displayed ... )

        However in this case look at Enumerations

        Give 'teacher' a real shock

        The real answer lies in total abstraction
        .... as a hint ... INI files


        Comment

        Working...