2
\$\begingroup\$

My task is:

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:

  1. Basic pay rate = $10.00/hr
  2. Overtime (in excess of 40 hours) = time and a half
  3. 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 <stdio.h>
#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 = hours - 40;
        payedBeforeTax = (40 * PAYRATE) + (overHours * OVERTIME);
    }

    else
        payedBeforeTax = hours * PAYRATE;//defining the user first paycheck before taxes 

    if (payedBeforeTax <= 300)//paying only first rate case
    {
        firstRate = payedBeforeTax*0.15;
        payAfterTax =payedBeforeTax - firstRate;
        printf("your total gross is %d, your taxes to pay are %d, your net pay is %d", payedBeforeTax, firstRate, payAfterTax);
    }

    else if (payedBeforeTax > 300 && payedBeforeTax <= 450)//paying first and second rate
    {
        secondAmount = payedBeforeTax - 300;
        firstRate = (payedBeforeTax - secondAmount) * 0.15;
        secondRate = secondAmount * 0.20;
        payAfterTax = payedBeforeTax - (firstRate + secondRate);
        printf("your total gross is %d, your taxes to pay are %d, your net pay is %d", payedBeforeTax, firstRate + secondRate, payAfterTax);

    }

    else if (payedBeforeTax > 450)// paying all rates 
    {
        rest = payedBeforeTax - 450;
        secondAmount = (payedBeforeTax - 300) - rest;
        firstRate = (payedBeforeTax - (rest + secondAmount)) * 0.15;
        secondRate = secondAmount * 0.20;
        restOfRate = rest * 0.25;
        payAfterTax = payedBeforeTax - (firstRate + secondRate + restOfRate);
        printf("your total gross is %d, your taxes to pay are %d, your net pay is %d", payedBeforeTax, firstRate + secondRate + restOfRate, payAfterTax);

    }

    return payAfterTax;
}
\$\endgroup\$

2 Answers 2

3
\$\begingroup\$

I'd start extracting some more functions.

What about decomposing

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 associated tax amount.

\$\endgroup\$
2
  • 1
    \$\begingroup\$ +1 for separating regular/overtime pay and separating the rates. It's always better to wrap the solution around the problem than the other way around. \$\endgroup\$
    – Ryan Gates
    Commented Jan 29, 2013 at 17:08
  • \$\begingroup\$ (My favourite names probably included grossPay() and taxes().) \$\endgroup\$
    – greybeard
    Commented Feb 25, 2018 at 14:01
1
\$\begingroup\$

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 <stdio.h>

#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 is > 450
    else
        taxes = (300 * RATE1) + (150 * RATE2) + ((gross - 450) * RATE3);

    net = gross - taxes;
    printf("GROSS: %0.2f\nTAXES: %0.2f\nNET: %0.2f\n", gross, taxes, net);
}
printf("DONE\n");
return 0;
}
\$\endgroup\$
6
  • \$\begingroup\$ (Welcome to CR!) extra functions are redundant please refer explicitly to part of the question. going through the same exercises right now might better be a comment to your answer. \$\endgroup\$
    – greybeard
    Commented Feb 25, 2018 at 9:47
  • \$\begingroup\$ I explicitly referred to part of the question. \$\endgroup\$
    – Ruslan
    Commented Feb 25, 2018 at 10:38
  • \$\begingroup\$ Which extra functions are you referring to? \$\endgroup\$
    – greybeard
    Commented Feb 25, 2018 at 11:36
  • \$\begingroup\$ I referred to the only extra function the OP used, namely the NetPay function. The excercise is simple and creating extra functions is an overkill imo \$\endgroup\$
    – Ruslan
    Commented Feb 25, 2018 at 13:48
  • \$\begingroup\$ referred to the only extra function the OP used, namely the NetPay function well, without naming NetPay(), extra is not the same in everyone's eyes: see mariosangiorgio's procedural decomposition, the comment below it and the votes for each answer. (extra functions might even have meant features not in the requirement specification.) \$\endgroup\$
    – greybeard
    Commented Feb 25, 2018 at 13:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.