Thread: Simple program, which I cant do! Beginner question...

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    8

    Question Simple program, which I cant do! Beginner question...

    Hi, I'm sure this will be horribly simple to most here but as a beginner it's 'doing my head in'

    I've got a simple exercise to work through. The brief is as follows:

    "A man is paid at the hourly rate of $8 per hour for the first 35 hours worked. After that, overtime is paid at 1.5 times the hourly rate for the next 25 hours worked and twice the hourly rate for further hours worked. Write a program to input the number of hours worked in a week, and calculate and display his basic pay, overtime pay and gross pay."

    So, the basic hours part is fine. It's after that point that I'm hitting a wall. How to calculate the first 35 hours at one pay rate THEN the rest at a second and third rate.

    If anyone can show me some code or even just a few pointers it would help. And no, your not doing my 'homework' for me - have lots of other exercises of a similar nature, I'm just trying to grasp the principle. Said it was simple.....

    Cheers.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Post your latest attempt then.
    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.

  3. #3
    Making mistakes
    Join Date
    Dec 2008
    Posts
    476
    You can do it in three for-loops. First, count until 35 hours and add $8 per hour. Then, continue with 25 hours and $1.5 per hour. Then write the last loop adding overtime pay until infinity. At each loop, check if you have reached the number.

  4. #4
    Registered User
    Join Date
    Mar 2009
    Posts
    399
    You don't need any loops. There are three cases:

    A) He has worked less than or equal to 35 hours.
    B) He has worked more than 35 hours but less than or equal to 35+25 = 60 hours
    C) He has worked more than 60 hours.

    To calculate his pay in case C for example, you simply add 8*35 + 8*1.5*25 + 8*2*(n-60) where n is the number of hours worked.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Just break it down, you only have 3 cases to content with: ST (straight time), OT (over time), and DT (double time).

    Break out each part, apply the proper rate to each type, and you're done. This is simple arithmetic, with the trouble being you're doing it in a RL word problem.

    Think it through and picture an employee for each case:

    E (employee) pay = 8 * hours worked for ST (straight time
    E pay = 8 * ST hours + OT rate * OT hours,
    E pay = 8 * ST hours + OT rate * OT hours + DT rate * DT hours

    A paper and pencil to start, may help organize your thoughts on this.

    And << Welcome to the forum, Lee! >>

  6. #6
    Making mistakes
    Join Date
    Dec 2008
    Posts
    476
    @Memloop: It doesn't work for values < 60, as in that case you subtract too much. (You subtract 16 for every hour below 60, while it should be 8 or 12).
    @Adak: Yes, this is probably the best solution

  7. #7
    Registered User
    Join Date
    Mar 2009
    Posts
    399
    > @Memloop: It doesn't work for values < 60, as in that case you subtract too much. (You subtract 16 for every hour below 60, while it should be 8 or 12).

    If he has worked less than 60 hours then we're not dealing with case C which is what my example showed ...

  8. #8
    Making mistakes
    Join Date
    Dec 2008
    Posts
    476
    Oh, then of course your algorithm would be way faster.

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Can't we all wait for the OP to post some code?
    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. Simple String Manipulation (Beginner question)
    By valdro in forum C Programming
    Replies: 4
    Last Post: 02-16-2009, 07:16 PM
  2. Replies: 1
    Last Post: 12-30-2007, 10:08 AM
  3. Mystery bug in simple program? - I am beginner
    By archie123 in forum C++ Programming
    Replies: 7
    Last Post: 04-08-2005, 07:23 AM
  4. Array question on my simple program?
    By correlcj in forum C++ Programming
    Replies: 11
    Last Post: 10-21-2002, 02:16 PM
  5. Simple Question: How do yo uend a C program?
    By S. Omnipotence in forum C Programming
    Replies: 7
    Last Post: 01-16-2002, 07:29 AM