#include <stdio.h>
// constants
#define SIZE 5 // total number of employees in the payroll system
#define OVERTIME_RATE 1.5f // overtime pay multiplier (time and a half)
#define STD_WORK_WEEK 40.0f // number of regular hours before overtime begins
// 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 entire payroll process. The function gathers
employee work hours, calculates overtime and gross pay,
stores results in arrays, prints a payroll report, and
displays totals and averages for all employees.
Parameters:
None
Returns:
int - program exit status (0 indicates successful execution)
********************************************************************/
int main()
{
long int clockNumber[SIZE] = {98401,526488,765349,34645,127615};
// array storing each employee's clock number (ID)
float grossPay[SIZE];
// array storing calculated gross pay for each employee
float hours[SIZE];
// array storing hours worked for each employee
float overtimeHrs[SIZE];
// array storing overtime hours for each employee
float wageRate[SIZE] = {10.60,9.75,10.50,12.25,8.35};
// array storing hourly wage rate for each employee
float totalWage = 0.0f;
// accumulator storing total of all wage rates
float totalHours = 0.0f;
// accumulator storing total hours worked by all employees
float totalOT = 0.0f;
// accumulator storing total overtime hours worked
float totalGross = 0.0f;
// accumulator storing total gross pay for all employees
int i;
// loop counter used to iterate through employee arrays
// ----------------------------------------------------------
// First loop: gather employee hours and calculate payroll
// ----------------------------------------------------------
for (i = 0; i < SIZE; ++i)
{
// Prompt user to enter hours worked for current employee
hours[i] = getHours(clockNumber[i]);
// Determine overtime hours based on hours worked
overtimeHrs[i] = calcOvertime(hours[i]);
// Calculate total gross pay including overtime
grossPay[i] = calcGross(hours[i], wageRate[i], overtimeHrs[i]);
// Update running totals for final payroll summary
totalWage += wageRate[i];
totalHours += hours[i];
totalOT += overtimeHrs[i];
totalGross += grossPay[i];
}
// Print payroll report header
printHeader();
// ----------------------------------------------------------
// Second loop: display payroll information for each employee
// ----------------------------------------------------------
for (i = 0; i < SIZE; ++i)
{
printEmp(clockNumber[i], wageRate[i], hours[i],
overtimeHrs[i], grossPay[i]);
}
// Display totals and averages for all employees
printTotals(totalWage, totalHours, totalOT, totalGross);
return 0; // indicate successful program execution
}
/********************************************************************
Function: getHours
Purpose: Prompts the user to enter the number of hours worked
by a specific employee and returns the entered value.
Parameters:
clockNumber - unique identification number for the employee
Returns:
float - number of hours worked entered by the user
********************************************************************/
float getHours (long int clockNumber)
{
float hoursWorked;
// variable used to store the number of hours entered by the user
// Ask user to input hours worked for the employee
printf("\n Hours worked by emp # %06li: ", clockNumber
);
// Read the user input from the keyboard
scanf ("%f", &hoursWorked
);
// Return the entered value to the calling function
return hoursWorked;
}
/********************************************************************
Function: calcOvertime
Purpose: Determines how many overtime hours an employee worked
based on the standard work week limit.
Parameters:
hours - total hours worked by the employee
Returns:
float - number of overtime hours worked
********************************************************************/
float calcOvertime (float hours)
{
float overtime;
// variable storing the calculated overtime hours
// Check whether hours exceed the standard 40-hour work week
if (hours > STD_WORK_WEEK)
overtime = hours - STD_WORK_WEEK; // extra hours are overtime
else
overtime = 0.0f; // no overtime if ≤ 40 hours
// Return calculated overtime hours
return overtime;
}
/********************************************************************
Function: calcGross
Purpose: Calculates the total gross pay for an employee including
both regular pay and overtime pay.
Parameters:
hours - total hours worked
wageRate - hourly pay rate for the employee
overtimeHrs - number of overtime hours worked
Returns:
float - total gross pay for the employee
********************************************************************/
float calcGross (float hours, float wageRate, float overtimeHrs)
{
float regularPay;
// pay earned from regular (non-overtime) hours
float overtimePay;
// pay earned from overtime hours
float gross;
// total gross pay (regular pay + overtime pay)
// Calculate pay for regular hours only
regularPay = (hours - overtimeHrs) * wageRate;
// Calculate pay for overtime hours using overtime multiplier
overtimePay = overtimeHrs * wageRate * OVERTIME_RATE;
// Total gross pay equals regular pay plus overtime pay
gross = regularPay + overtimePay;
// Return total calculated gross pay
return gross;
}
/********************************************************************
Function: printHeader
Purpose: Displays the formatted header for the payroll report,
including the title and column labels.
Parameters:
None
Returns:
Nothing
********************************************************************/
void printHeader (void)
{
// Print report title
printf("\n\n*** Pay Calculator ***\n");
// Print table borders and column headings
printf("\n----------------------------------------------------------\n"); printf("Clock# Wage Hours OT Gross\n"); printf("----------------------------------------------------------\n"); }
/********************************************************************
Function: printEmp
Purpose: Displays payroll information for a single employee in
formatted column layout.
Parameters:
clockNumber - employee identification number
wageRate - hourly wage rate
hours - total hours worked
overtimeHrs - overtime hours worked
grossPay - total calculated gross pay
Returns:
Nothing
********************************************************************/
void printEmp (long int clockNumber, float wageRate, float hours,
float overtimeHrs, float grossPay)
{
// Display employee payroll data in aligned columns
printf("%06li %5.2f %5.1f %5.1f %8.2f\n", clockNumber, wageRate, hours, overtimeHrs, grossPay);
}
/********************************************************************
Function: printTotals
Purpose: Prints total and average payroll statistics for all
employees in the report.
Parameters:
totalWage - sum of all employee wage rates
totalHours - total hours worked by all employees
totalOT - total overtime hours worked
totalGross - total gross payroll amount
Returns:
Nothing
********************************************************************/
void printTotals(float totalWage, float totalHours,
float totalOT, float totalGross)
{
// Print separator line for totals section
printf("----------------------------------------------------------\n");
// Display total values
printf("Total %5.2f %5.1f %5.1f %8.2f\n", totalWage, totalHours, totalOT, totalGross);
// Display 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);
}