#include <stdio.h>
// constants
#define SIZE 5
#define OVERTIME_RATE 1.5f
#define STD_WORK_WEEK 40.0f
// function prototypes
float getHours (long int clockNumber);
void printHeader (void);
void printEmp (long int clockNumber, float wageRate, float hours,
float overtimeHrs, float grossPay);
// Other required function prototypes
float calculateOvertime (float hours);
float calculateGrossPay (float wageRate, float hours, float overtimeHrs);
int main()
{
/* Variable Declarations */
long int clockNumber[SIZE] = {98401, 526488, 765349, 34645, 127615}; // ID
float grossPay[SIZE]; // gross pay
float hours[SIZE]; // hours worked in a given week
int i; // loop and array index
float overtimeHrs[SIZE]; // overtime hours
float wageRate[SIZE] = {10.60, 9.75, 10.50, 12.25, 8.35}; // hourly wage rate
// process each employee
for (i = 0; i < SIZE; ++i)
{
// Read in hours for employee
hours[i] = getHours (clockNumber[i]);
// Function call to calculate overtime hours
overtimeHrs[i] = calculateOvertime (hours[i]);
// Function call to calculate gross pay
grossPay[i] = calculateGrossPay (wageRate[i], hours[i], overtimeHrs[i]);
}
// print the header info
printHeader();
// print out each employee
for (i = 0; i < SIZE; ++i)
{
// Print all the employees - call by value
printEmp (clockNumber[i], wageRate[i], hours[i],
overtimeHrs[i], grossPay[i]);
}
return (0);
}
//**************************************************************
// Function: getHours
// Purpose: Obtains input from user for hours worked per employee
//**************************************************************
float getHours (long int clockNumber)
{
float hoursWorked;
printf("Enter hours worked for clock #%ld: ", clockNumber
); scanf("%f", &hoursWorked
); return hoursWorked;
}
//**************************************************************
// Function: calculateOvertime
// Purpose: Computes the number of hours worked past 40
//**************************************************************
float calculateOvertime (float hours)
{
if (hours > STD_WORK_WEEK)
{
return hours - STD_WORK_WEEK;
}
return 0.0f;
}
//**************************************************************
// Function: calculateGrossPay
// Purpose: Calculates regular pay and adds 1.5x overtime pay
//**************************************************************
float calculateGrossPay (float wageRate, float hours, float overtimeHrs)
{
float regularHours = hours - overtimeHrs;
float regularPay = regularHours * wageRate;
float overtimePay = overtimeHrs * (wageRate * OVERTIME_RATE);
return regularPay + overtimePay;
}
//**************************************************************
// Function: printHeader
// Purpose: Prints the table column headers
//**************************************************************
void printHeader (void)
{
printf("\n--------------------------------------------------------------\n"); printf("%-10s %-10s %-10s %-12s %-10s\n", "Clock #", "Wage Rate", "Hours", "OT Hours", "Gross Pay");
printf("--------------------------------------------------------------\n"); }
//**************************************************************
// Function: printEmp
// Purpose: Prints aligned fields for a single employee record
//**************************************************************
void printEmp (long int clockNumber, float wageRate, float hours,
float overtimeHrs, float grossPay)
{
printf("%-10ld $%-9.2f %-10.1f %-12.1f $%-9.2f\n", clockNumber, wageRate, hours, overtimeHrs, grossPay);
}