//********************************************************
//
// Assignment 5 - Functions
//
// Name: <replace with your name>
//
// Class: C Programming, <replace with Semester and Year>
//
// Date: <replace with the current date>
//
// Description: Program which determines overtime and
// gross pay for a set of employees with outputs sent
// to standard output (the screen).
//
// All functions are called by value
//
//********************************************************
#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);
// --- Added helper prototypes ---
float calcOvertime(float hours);
float calcGrossPay(float wageRate, float hours);
int main(void)
{
/* 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
float overtimeHrs[SIZE]; // overtime hours
float wageRate[SIZE] = {10.60f, 9.75f, 10.50f, 12.25f, 8.35f}; // hourly wage rate
int i; // loop and array index
// process each employee
for (i = 0; i < SIZE; ++i)
{
// Read in hours for employee
hours[i] = getHours(clockNumber[i]);
// Calculate overtime hours
overtimeHrs[i] = calcOvertime(hours[i]);
// Calculate gross pay
grossPay[i] = calcGrossPay(wageRate[i], hours[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;
} // main
//**************************************************************
// Function: getHours
//
// Purpose: Obtains input from user, the number of hours worked
// per employee and stores the result in a local variable
// that is passed back to the calling function.
//
// Parameters: clockNumber - The unique employee ID
//
// Returns: hoursWorked - hours worked in a given week
//
//**************************************************************
float getHours (long int clockNumber)
{
float hoursWorked = 0.0f; // hours worked in a given week
// Read in hours for employee
printf("\nEnter hours worked by emp # %06li: ", clockNumber);
scanf("%f", &hoursWorked);
// return hours back to the calling function
return hoursWorked;
} // getHours
//**************************************************************
// Function: printHeader
//
// Purpose: Prints the initial table header information.
//
// Parameters: none
//
// Returns: void
//
//**************************************************************
void printHeader (void)
{
printf("\n\n*** Pay Calculator ***\n");
printf("\nClock# Wage Hours OT Gross\n");
printf("-----------------------------------------------\n");
} // printHeader
//*************************************************************
// Function: printEmp
//
// Purpose: Prints out all the information for an employee
// in a nice and orderly table format.
//
// Parameters:
// clockNumber - unique employee ID
// wageRate - hourly wage rate
// hours - hours worked for the week
// overtimeHrs - overtime hours worked in a week
// grossPay - gross pay for the week
//
// Returns: void
//
//**************************************************************
void printEmp (long int clockNumber, float wageRate, float hours,
float overtimeHrs, float grossPay)
{
// Print one row with neat alignment:
// Clock# as 6 digits with leading zeros
// Wage shown with 2 decimals
// Hours and OT with 2 decimals
// Gross with 2 decimals
printf("%06li %6.2f %6.2f %6.2f %8.2f\n",
clockNumber, wageRate, hours, overtimeHrs, grossPay);
} // printEmp
//**************************************************************
// Function: calcOvertime
//
// Purpose: Computes overtime hours (> 40 hours).
//
// Parameters:
// hours - hours worked for the week
//
// Returns: overtime hours (0 if hours <= 40)
//
//**************************************************************
float calcOvertime(float hours)
{
if (hours > STD_WORK_WEEK)
return hours - STD_WORK_WEEK;
else
return 0.0f;
} // calcOvertime
//**************************************************************
// Function: calcGrossPay
//
// Purpose: Computes gross pay given wage rate and hours,
// applying time-and-a-half for hours beyond 40.
//
// Parameters:
// wageRate - hourly wage rate
// hours - hours worked for the week
//
// Returns: gross pay for the week
//
//**************************************************************
float calcGrossPay(float wageRate, float hours)
{
if (hours <= STD_WORK_WEEK)
{
return wageRate * hours;
}
else
{
float overtime = hours - STD_WORK_WEEK;
float regularPay = wageRate * STD_WORK_WEEK;
float overtimePay = wageRate * OVERTIME_RATE * overtime;
return regularPay + overtimePay;
}
} // calcGrossPay