#include <stdio.h>
// constants
#define SIZE 5 // number of employees
#define OVERTIME_RATE 1.5f // overtime pay multiplier
#define STD_WORK_WEEK 40.0f // standard hours before overtime
// function prototypes
float getHours (long int clockNumber);
float calcOvertime (float hours);
float calcGross (float hours, float wageRate, float overtimeHrs);
void printHeader (void);
void printEmp (long int clockNumber, float wageRate, float hours,
float overtimeHrs, float grossPay);
void printTotals(float totalWage, float totalHours,
float totalOT, float totalGross);
/********************************************************************
Function: main
Purpose: Controls the payroll program. It collects employee hours,
calculates overtime and gross pay, prints the payroll
report, and displays totals and averages.
********************************************************************/
int main()
{
long int clockNumber[SIZE] = {98401,526488,765349,34645,127615}; // employee clock numbers
float grossPay[SIZE]; // array to store gross pay for each employee
float hours[SIZE]; // array to store hours worked
float overtimeHrs[SIZE]; // array to store overtime hours
float wageRate[SIZE] = {10.60,9.75,10.50,12.25,8.35}; // hourly wage rates
float totalWage = 0.0f; // sum of all wage rates
float totalHours = 0.0f; // total hours worked by all employees
float totalOT = 0.0f; // total overtime hours
float totalGross = 0.0f; // total gross pay
int i; // loop counter
// Loop through all employees to collect hours and calculate payroll values
for (i = 0; i < SIZE; ++i)
{
// Get hours worked from the user
hours[i] = getHours(clockNumber[i]);
// Calculate overtime hours
overtimeHrs[i] = calcOvertime(hours[i]);
// Calculate gross pay including overtime
grossPay[i] = calcGross(hours[i], wageRate[i], overtimeHrs[i]);
// Accumulate totals for report summary
totalWage += wageRate[i];
totalHours += hours[i];
totalOT += overtimeHrs[i];
totalGross += grossPay[i];
}
// Print the report header
printHeader();
// Loop again to print each employee's payroll data
for (i = 0; i < SIZE; ++i)
{
printEmp(clockNumber[i], wageRate[i], hours[i],
overtimeHrs[i], grossPay[i]);
}
// Print totals and averages
printTotals(totalWage, totalHours, totalOT, totalGross);
return 0;
}
/********************************************************************
Function: getHours
Purpose: Prompts the user to enter the hours worked for a specific
employee and returns that value.
Parameters:
clockNumber - employee identification number
Returns:
float - number of hours worked
********************************************************************/
float getHours (long int clockNumber)
{
float hoursWorked; // stores the number of hours entered by the user
// Prompt user to enter hours worked for the given employee
printf("\n Hours worked by emp # %06li: ", clockNumber
);
// Read the hours from keyboard input
scanf ("%f", &hoursWorked
);
// Return the hours worked to the calling function
return hoursWorked;
}
/********************************************************************
Function: calcOvertime
Purpose: Determines how many overtime hours an employee worked.
Parameters:
hours - total hours worked
Returns:
float - overtime hours
********************************************************************/
float calcOvertime (float hours)
{
float overtime; // variable to store calculated overtime hours
// Check if employee worked more than standard work week
if (hours > STD_WORK_WEEK)
overtime = hours - STD_WORK_WEEK; // overtime is extra hours above 40
else
overtime = 0.0f; // no overtime if 40 or fewer hours worked
// Return overtime hours
return overtime;
}
/********************************************************************
Function: calcGross
Purpose: Calculates the total gross pay including regular and
overtime pay.
Parameters:
hours - total hours worked
wageRate - hourly pay rate
overtimeHrs - overtime hours worked
Returns:
float - total gross pay
********************************************************************/
float calcGross (float hours, float wageRate, float overtimeHrs)
{
float regularPay; // pay for regular hours
float overtimePay; // pay earned from overtime
float gross; // total gross pay
// Calculate pay for regular hours
regularPay = (hours - overtimeHrs) * wageRate;
// Calculate overtime pay using overtime rate multiplier
overtimePay = overtimeHrs * wageRate * OVERTIME_RATE;
// Total gross pay is the sum of regular and overtime pay
gross = regularPay + overtimePay;
// Return the calculated gross pay
return gross;
}
/********************************************************************
Function: printHeader
Purpose: Prints the payroll report header and column titles.
Parameters:
None
Returns:
Nothing
********************************************************************/
void printHeader (void)
{
// Print title of payroll report
printf("\n\n*** Pay Calculator ***\n");
// Print table formatting lines and column headers
printf("\n----------------------------------------------------------\n"); printf("Clock# Wage Hours OT Gross\n"); printf("----------------------------------------------------------\n"); }
/********************************************************************
Function: printEmp
Purpose: Prints payroll information for one employee.
Parameters:
clockNumber - employee clock number
wageRate - employee hourly wage
hours - hours worked
overtimeHrs - overtime hours worked
grossPay - calculated gross pay
Returns:
Nothing
********************************************************************/
void printEmp (long int clockNumber, float wageRate, float hours,
float overtimeHrs, float grossPay)
{
// Print employee payroll information in formatted columns
printf("%06li %5.2f %5.1f %5.1f %8.2f\n", clockNumber, wageRate, hours, overtimeHrs, grossPay);
}
/********************************************************************
Function: printTotals
Purpose: Displays total and average payroll statistics for all
employees.
Parameters:
totalWage - sum of all wage rates
totalHours - total hours worked
totalOT - total overtime hours
totalGross - total gross pay
Returns:
Nothing
********************************************************************/
void printTotals(float totalWage, float totalHours,
float totalOT, float totalGross)
{
// Print separator line
printf("----------------------------------------------------------\n");
// Print total values
printf("Total %5.2f %5.1f %5.1f %8.2f\n", totalWage, totalHours, totalOT, totalGross);
// Print averages by dividing totals by number of employees
printf("Average %5.2f %5.1f %5.1f %8.2f\n", totalWage / SIZE,
totalHours / SIZE,
totalOT / SIZE,
totalGross / SIZE);
}