Latest update

# Calculate weekly net pay

2018-02-25 10:04:36

Write a program that requests the hours worked in a week and then

prints the gross pay, the taxes, and the net pay. Assume the

following:

Basic pay rate = \$10.00/hr

Overtime (in excess of 40 hours) = time and a half

Tax rate: 15% of the first \$300

20% of the next \$150

25% of the rest

Is my code way too long and complicated for that?

#include

#define PAYRATE 10 //basic pay rate per hour

#define OVERTIME 15 //in excess of 40 hours a week

int NetPay(int hours);

int main()

{

int userWeeklyHours;

printf("Please enter your total weekly working hours: \n");

scanf("%d", &userWeeklyHours);// get the user weekly hours

NetPay(userWeeklyHours);

return 0;

}

int NetPay(int hours)// implementing the function to calculate total pay, total taxes, and net pay

{

int firstRate, secondRate, restOfRate, secondAmount, rest, payAfterTax, payedBeforeTax, overHours;

if (hours > 40)

{

overHours

• I'd start extracting some more functions.

int NetPay(int hours){

int payedBeforeTaxes = payedBeforeTaxes(hours);

return applyTaxes(payedBeforeTaxes);

}

int payedBeforeTaxes(hours){

return regularPay(hours) + overtimePay(hours);

}

int applyTaxes(int amountBeforeTaxes){

int firstRateTaxes = applyFirstRate(amountBeforeTaxes);

int secondRateTaxes = applySecondRate(amountBeforeTaxes);

int thirdRateTaxes = applyThirdRate(amountBeforeTaxes);

return amountBeforeTaxes - firstRateTaxes -

secondRateTaxes - thirdRateTaxes;

}

At this point you just need to implement the functions with the appropriate code.

You'll notice that with this structure each function only does one thing and it will be much simpler and clearer.

Alternatively you can replace your apply taxes introducing an array of `(rate, threshold).

You will have to loop through the array, and at each step compute the amount you have to apply that tax rate and the asso

2018-02-25 10:26:20
• Your code is not that complicated but a bit long.

I'm going through the same exercises right now and I came up with a more compact solution. In my opinion, extra functions are redundant.

#include

#define BASIC 10.0 // basic payrate

#define OVERTIME 15.0 // overtime payrate in excess of 40 hours

#define RATE1 0.15 // taxrate of the first \$300

#define RATE2 0.2 // taxrate of the next \$150

#define RATE3 0.25 // taxrate of the rest

int main(void)

{

float gross, taxes, net;

int hour;

while(scanf("%d", &hour) == 1) //quits if no int or more than 1 int entered

{

// if gross is <= 400

if ((gross = hour * BASIC) <= 400)

{

if (gross <= 300.0)

taxes = gross * RATE1;

else

taxes = (300 * RATE1) + ((gross - 300) * RATE2);

}

// if gross is <= 450

else if ((gross = ((40 * BASIC) + ((hour - 40) * OVERTIME)) <= 450))

taxes = (300 * RATE1) + ((gross - 300) * RATE2);

// if gross

2018-02-25 10:46:41