Thread: Modularization question...

  1. #1
    Registered User
    Join Date
    Sep 2004

    Modularization question...

    Hello people,
    Choose from the two versions of this post:

    So, I'm making some functions, I think I'm real slick, when all of a sudden...WHAM! Right in the face...a question begins to form, making my skin start to crawl, and without me even realizing it, I'm scratching my head...ok enough of the lengthy introduction. So...

    I'm writing a program and making some calculation modules. Now the main data is the current salary employee's are getting paid, and then their current salary with an 8% increase tacked on.
    Also, the total payroll pre-increase, and the total payroll post-increase.
    The functions I have are Calculate_pay_increase: which simply receives a salary and does the math to return the increased salary
    The other function is calculate_total_payrolls: which would calculate the pre and post payrolls. However, am I adding more math than necessary when I do this? Should I, instead, calculate the total post-increase payroll within the Calculate_pay_increase function? Or is that making the function do too much? Or should I be willing to do extra calculations for the sake of the modules?

  2. #2
    Join Date
    Jul 2005
    So you're worried about the inefficiency of doing a little extra summing?


    What are you using to store the employee's pay? Presumably a number that gets rounded to the nearest cent or nearest dollar (or rounded down) -- Even if you aren't rounding, that's what they're going to end up getting paid, so you should round.

    You should re-add the list of new payrolls, after rounding them to the nearest cent or dollar. Multiplying the other total by 1.08 will give a result that is off by a few cents (or dollars) because of rounding.

  3. #3
    Registered User
    Join Date
    May 2005
    What you should be doing is writing an object-oriented program since you are using an object-oriented language.

    You've got an Employee object that has a salary data member and a getSalary member function. You have a Payroll object that has a totalPayroll data member, an adjustedPayroll data member (for the pay increase amount), and a list of pointers to Employee objects. You could then have a calculateTotalPayroll member function (in Payroll object) that calculates your total payroll, by running through its list of employees and calling a getSalary function for each, which it sums on the fly, and returns it. The same function could calculate your adjusted payroll if you set the function up with an argument that is the payIncrease percentage. Just pass it zero for standard payroll calculation. If the function receives anything other than zero in the argument, it adds the adjusted amount to the adjustedPayroll member instead of the totalPayroll member.

    Just an observation.

    If you intend to keep your functional approach, consider whether you actually need each employees post-increase pay amount for anything else. If not, then it would be appropriate to go ahead and have the Calculate_pay_increase function do the total post-increase payroll calculation.

  4. #4
    Registered User
    Join Date
    Nov 2002
    C++ is more of a multi-paradigm language. How well it supports OOP is questionable. However if efficiency is your concern, you should write the code in the most straightforward way possible, then run it throuhg a profiler to discover any bottlenecks. On *NIX, gprof is commonly used. MSVC++ come swith one too I beleive.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    This is where design comes in, where you sit down and plan all the major functional areas which you'll need before you even get as far as 'int main'
    In a short amount of time (far shorter than you've spent coding so far), you'd see your need for summing (or whatever) in several places, and that would assist you with writing the code the right way.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  2. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  3. Question...
    By TechWins in forum A Brief History of
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  4. Question about linked lists.
    By cheeisme123 in forum C++ Programming
    Replies: 6
    Last Post: 02-25-2003, 01:36 PM
  5. Question, question!
    By oskilian in forum A Brief History of
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM