Originally Posted by
edesign
have u ever heard of flow charts..or pseudocode???Try making a rough flowchart before starting with the code...It helps when you are new to it..and having such kind of practice will also help when u want to write big codes..
have written a pseudo code ,now try developing it in c
input employee no::
while(employeeno<=10)
Input no of hours
if hours-40>0:calculate overtime(12*(40-hours))
else no overtime for this employee
next employee
}
OK, there's a start.
Now imagine you're the paymaster at the small company of ten employee's, and you are figuring this out, by hand.
Since each employee will have an employee number > 0, I would use that as a test condition for your first while loop:
Code:
employee_no = 1 /* otherwise the while loop is entirely skipped, perhaps */
while(employee_no > 0)
set values:
ot = 0, work hours = 0, regular pay = 0, ot_pay = 0, full pay = 0, employee = 0
print a prompt for what to enter, and then input their employee number
if(employee number == 0)
then continue
Without the continue (or break), you'll be asked to fill in all the data for the employee with
the zero employee number, which you don't want. The "continue" just moves control to
the very bottom of the loop, which will then bring control right to the test at the top of the
while loop, which employee number 0 will fail, and then break out of the loop, completely.
print a prompt for no. of hours, and input that integer since no fractions of hours allowed.
calculate overtime hours:
if work hours > 40
then ot = work hours - 40
calculate pay:
regular pay = 8 * work hours
if(ot > 0)
ot_pay = 12 * OT (12 * each hour of overtime)
full pay = regular pay + ot_pay
print employee no., regular pay, ot_pay, and full pay
And loop back up.
If you break the pay down, into small parts (regular, ot_pay, full pay), then it's just easier
to make up the logic for it, and it's easier to troubleshoot it when there's a problem.
<Apologies, I got into the logic, and didn't notice the "the overtime" part of the problem.>
All those sets at the top of the loop may seem unneeded, but variables like that in a loop, can give you the craziest errors.
Code this up, and step through it several times. See if I've made any errors!
Change it around a bit, as you like. See if that new version's logic is good or not.
Most of us learn best by doing, not by just reading about it.