473,320 Members | 1,865 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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_amount / 2000)
m_tendollars = Math.Floor((m_amount Mod 2000) / 1000)
m_fivedollars = Math.Floor((m_amount Mod 1000) / 500)
m_onedollar = Math.Floor((m_amount Mod 500) / 100)
m_halfdollars = Math.Floor((m_amount Mod 100) / 50)
m_quarters = Math.Floor((m_amount Mod 50) / 25)
m_dimes = Math.Floor((m_amount Mod 25) / 10)
m_nickels = Math.Floor((m_amount - (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_amount 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.
Jul 17 '05 #1
3 3558
On 17 Nov 2003 17:57:34 -0800, ho*****@yahoo.com (David Horne) wrote:
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_amount / 2000)
m_tendollars = Math.Floor((m_amount Mod 2000) / 1000)
m_fivedollars = Math.Floor((m_amount Mod 1000) / 500)
m_onedollar = Math.Floor((m_amount Mod 500) / 100)
m_halfdollars = Math.Floor((m_amount Mod 100) / 50)
m_quarters = Math.Floor((m_amount Mod 50) / 25)
m_dimes = Math.Floor((m_amount Mod 25) / 10)


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

Jul 17 '05 #2
er*****@nowhere.com (J French) wrote in message news:<3f****************@news.btclick.com>...
On 17 Nov 2003 17:57:34 -0800, ho*****@yahoo.com (David Horne) wrote:
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_amount / 2000)
m_tendollars = Math.Floor((m_amount Mod 2000) / 1000)
m_fivedollars = Math.Floor((m_amount Mod 1000) / 500)
m_onedollar = Math.Floor((m_amount Mod 500) / 100)
m_halfdollars = Math.Floor((m_amount Mod 100) / 50)
m_quarters = Math.Floor((m_amount Mod 50) / 25)
m_dimes = Math.Floor((m_amount Mod 25) / 10)


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


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
Jul 17 '05 #3
On 18 Nov 2003 12:24:17 -0800, ho*****@yahoo.com (David Horne) wrote:

<snip>

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


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
Jul 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: yongsing | last post by:
I have a Windows 2000 Server machine that runs DB2 UDB v7 fixpak 11. I also have a Java program that uses JDBC to insert data into the database. Sometimes, the program encounters SQL0302N error...
2
by: david | last post by:
When you right click on a file in windows, there is a property of type of file, which seems to associate this file with an application for it. For example, when you double click on a .doc file, the...
1
by: nicksop | last post by:
How can i change of a setup project in vb .net For example how can i change the path of C:\program files\myprogram\program.exe and make it D:\program files\myprogram\program.exe
3
by: Ubergeek | last post by:
I have a project (Win32 application) that I want to change to a console application. Does anyone know what to edit in which file, to implement the change?
3
by: vatsa | last post by:
Hi all, we can simply change value of PS1 from command prompt . but I want to change the value of PS1 from within a c program. i tried to do that using getenv & putenv. but it is n't...
8
by: bim_bom | last post by:
Hi, is there any tool to change naming convention in c++ sources? I mean something, that parses cpp and h files in my project, and it finds, what variables are there declared. I think, it should...
3
by: W C Hull | last post by:
We have a request from Auditing to modify the password an a local workstation administrative account every 90 days. We are developing two programs - a VB6 GUI program that will allow the...
1
by: monkey1001 | last post by:
my program is suppose to show my due change and i got it working but my change and coins are wrong how can i improve it thank you..(its supposed to be in java)
0
by: shellegreen | last post by:
Hello, I need help with some password changes that I have to do in some iPAQs. The company that I work has 3 models of iPAQ and 3 different versions of Windows Mobile (Microsoft Pocket PC...
3
by: Alan J. Salmoni | last post by:
Hello everyone, I searched through groups to find an appropriate answer to this one but could only find these which didn't meet my program's needs:...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.