Rewrite program in C language using appropriate functions and parameter passing.

Question

Rewrite program in C language using appropriate functions and parameter passing. Please have the output at the end.

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>

double computeGrossPay(double, double, int*, double*, double*);
double computeFederalTax(double, double);
double computeStateTax(double, double);
double computePaycheckAmount(double, double, double);

int main()
{
int overtimeEmployeeCount = 0;
double totalOvertimeHours = 0, totalOvertimeAmountPaid = 0;
double numberOfHours;
double hourlyWage;
char name[4][100];
double federaltaxAmount[4];
double grossPay[4];
double federalTaxrate;
double stateTaxrate;
double paycheckAmount[4];
double stateTaxAmount[4];

int count = 0;
while (count < 4) {
printf("Enter name of the employee : ");
gets_s(name[count]);
printf("Enter hours worked : ");
scanf("%lf", &numberOfHours);
printf("Enter hourly rate : ");
scanf("%lf", &hourlyWage);
printf("Enter federal tax rate : ");
scanf("%lf", &federalTaxrate);
printf("Enter state tax rate : ");
scanf("%lf", &stateTaxrate);

grossPay[count] = computeGrossPay(hourlyWage, numberOfHours, &overtimeEmployeeCount, &totalOvertimeHours, &totalOvertimeAmountPaid);

federaltaxAmount[count] = computeFederalTax(grossPay[count], federalTaxrate);

stateTaxAmount[count] = computeStateTax(grossPay[count], stateTaxrate);

paycheckAmount[count] = computePaycheckAmount(grossPay[count], federaltaxAmount[count], stateTaxAmount[count]);
count++;
gets_s(name[count]);
}
for (int i = 0; i < 4; i++) {
printf("Name \t%s\nGross pay \t\t$%.2f \nFederal Tax Amount \t$%.2f \nState Tax Amount \t%6.2f \nPaycheck amount \t$%.2f \n", name[i], grossPay[i], federaltaxAmount[i], stateTaxAmount[i], paycheckAmount[i]);
printf("\n");
}
printf("%d employees worked overtime\ntotal Overtime Hours worked\t%.2f\ntotal Overtime Amount Paid\t$%.2f", overtimeEmployeeCount, totalOvertimeHours, totalOvertimeAmountPaid);
return 0;
}

double computeGrossPay(double hourlyWage, double numberOfHours, int* overtimeEmployeeCount, double* totalOvertimeHours, double* totalOvertimeAmountPaid) {
double overtimeWage;
int overtimeHours;
double grossPay;
if (numberOfHours <= 40) {
grossPay = hourlyWage * numberOfHours;
}
else if (numberOfHours > 40) {
overtimeWage = hourlyWage * 1.5;
overtimeHours = numberOfHours - 40;
grossPay = (hourlyWage * 40) + (overtimeHours * overtimeWage);
*overtimeEmployeeCount = *overtimeEmployeeCount + 1;
*totalOvertimeHours = *totalOvertimeHours + overtimeHours;
*totalOvertimeAmountPaid = *totalOvertimeAmountPaid + (overtimeHours * overtimeWage);
}
return grossPay;
}
double computeFederalTax(double grossPay, double federalTaxrate) {
return grossPay * federalTaxrate / 100;
}
double computeStateTax(double grossPay, double stateTaxrate) {
return stateTaxrate * grossPay / 100;
}
double computePaycheckAmount(double grossPay, double federaltaxAmount, double stateTaxAmount) {
return grossPay - (federaltaxAmount + stateTaxAmount);
}

Explanation

In this, we rewrite the program in the C language using appropriate functions and parameter passing.

Functions are used to make the program easy and simple. And to use the function we need to follow three parts.

They are as follows.

Function prototype:

In this, we will declare the function. Along with its parameters. And what will it return?
A function prototype include: return_type Function_Name(parametrs)

Function definition:

In this, we will define the function. Also, it will have all the information of what task it will perform.

Function Call: In this, we will call the function.

Code

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>

//function declarations
double computeGrossPay(double, double, int*, double*, double*);
double computeFederalTax(double, double);
double computeStateTax(double, double);
double computePaycheckAmount(double, double, double);

//main function
void main()
{	//variable declaration
  int overtimeEmployeeCount = 0;
  double totalOvertimeHours = 0, totalOvertimeAmountPaid = 0;
  double numberOfHours;
  double hourlyWage;
  char name[4][100];
  double federaltaxAmount[4];
  double grossPay[4];
  double federalTaxrate;
  double stateTaxrate;
  double paycheckAmount[4];
  double stateTaxAmount[4];
  int count = 0,i;

  while (count < 4)
   {
    printf("Enter name of the employee : ");
    gets(name[count]);
    printf("Enter hours worked : ");
    scanf("%lf", &numberOfHours);
    printf("Enter hourly rate : ");
    scanf("%lf", &hourlyWage);
    printf("Enter federal tax rate : ");
    scanf("%lf", &federalTaxrate);
    printf("Enter state tax rate : ");
    scanf("%lf", &stateTaxrate);

    grossPay[count] = computeGrossPay(hourlyWage, numberOfHours, &overtimeEmployeeCount, &totalOvertimeHours, &totalOvertimeAmountPaid);
 	federaltaxAmount[count] = computeFederalTax(grossPay[count], federalTaxrate);
    stateTaxAmount[count] = computeStateTax(grossPay[count], stateTaxrate);
    paycheckAmount[count] = computePaycheckAmount(grossPay[count], federaltaxAmount[count], stateTaxAmount[count]);
    count++;
    gets(name[count]);
  }

  for (i = 0; i < 4; i++) 
  {
    printf("Name \t%s\nGross pay \t\t$%.2f \nFederal Tax Amount \t$%.2f \nState Tax Amount \t%6.2f \nPaycheck amount \t$%.2f \n", name[i], grossPay[i], federaltaxAmount[i], stateTaxAmount[i], paycheckAmount[i]);
    printf("\n");
  }
  printf("%d employees worked overtime \ntotal Overtime Hours worked\t%.2f\ntotal Overtime Amount Paid\t$%.2f", overtimeEmployeeCount, totalOvertimeHours, totalOvertimeAmountPaid);

}
//end of main function
//function definition for calculation of GrossPay
double computeGrossPay(double hourlyWage, double numberOfHours, int* overtimeEmployeeCount, double* totalOvertimeHours, double* totalOvertimeAmountPaid) 
{
  double overtimeWage;
  int overtimeHours;
  double grossPay;

  if (numberOfHours <= 40) 
  {
    grossPay = hourlyWage*numberOfHours;
  }
  else if (numberOfHours > 40) 
  {
    overtimeWage = hourlyWage * 1.5;
    overtimeHours = numberOfHours - 40;
    grossPay = (hourlyWage * 40) + (overtimeHours * overtimeWage);
    *overtimeEmployeeCount = *overtimeEmployeeCount + 1;
    *totalOvertimeHours = *totalOvertimeHours + overtimeHours;
    *totalOvertimeAmountPaid = *totalOvertimeAmountPaid + (overtimeHours * overtimeWage);
  }
  return grossPay;
}

//function to calculate federal Tax
double computeFederalTax(double grossPay, double federalTaxrate) 
{
  return grossPay * federalTaxrate / 100;
}

//function to calculate State Tax
double computeStateTax(double grossPay, double stateTaxrate) 
{
  return stateTaxrate * grossPay / 100;
}

//function to compute PaycheckAmount
double computePaycheckAmount(double grossPay, double federaltaxAmount, double stateTaxAmount) 
{
  return grossPay - (federaltaxAmount + stateTaxAmount);
}

Output

Rewrite program in C

 

Also read OFFICE table & RESIDENTS table.

Share this post

Leave a Reply

Your email address will not be published. Required fields are marked *