//********************************************************
//
// Assignment 6 - Structures
//
// Name: <Joseph Sul>
//
// Class: C Programming, <Spring 2024>
//
// Date: <3/3/2024>
//
// Description: Program which determines overtime and
// gross pay for a set of employees with outputs sent
// to standard output (the screen).
//
// Call by Value design
//
//********************************************************
// Define and Includes
#include <stdio.h>
// Define Constants
#define SIZE 5
#define STD_HOURS 40.0
#define OT_RATE 1.5
// Define a global structure to pass employee data between functions
// Note that the structure type is global, but you don't want a variable
// of that type to be global. Best to declare a variable of that type
// in a function like main or another function and pass as needed.
struct employee
{
long int clockNumber;
float wageRate;
float hours;
float overtimeHrs;
float grossPay;
};
// define prototypes here for each function except main
float getHours (long int clockNumber);
void printHeader (void);
void printEmp (long int clockNumber, float wageRate, float hours,
float overtimeHrs, float grossPay);
// TODO: Add your other function prototypes here
float calcOT (float hours);
float calcGross (float wageRate, float hours, float overtimeHrs);
int main ()
{
// Set up a local variable to store the employee information
struct employee employeeData[SIZE] = {
{ 98401, 10.60 },
{ 526488, 9.75 },
{ 765349, 10.50 }, // initialize clock and wage values
{ 34645, 12.25 },
{ 127615, 8.35 }
};
int i; // loop and array index
// Call functions as needed to read and calculate information
for (i = 0; i < SIZE; ++i)
{
// Prompt for the number of hours worked by the employee
employeeData[i].hours = getHours (employeeData[i].clockNumber);
// TODO: Add other function calls as needed to calculate overtime and gross
//Calculate the number of overtime hours worked if any
employeeData[i].overtimeHrs = calcOT (employeeData[i].hours);
//Calculate the gross pay factoring in all the hours worked including any overtime
employeeData[i].grossPay = calcGross (employeeData[i].wageRate, employeeData[i].hours, employeeData[i].overtimeHrs);
} // for
// Print the column headers
printHeader();
// print out each employee
for (i = 0; i < SIZE; ++i)
{
printEmp (employeeData[i].clockNumber,
employeeData[i].wageRate,
employeeData[i].hours,
employeeData[i].overtimeHrs,
employeeData[i].grossPay);
}
return(0); // success
} // 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; // 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");
// print the table header
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 out a single employee
printf("\n %06li %5.2f %4.1f %4.1f %8.2f", clockNumber, wageRate, hours,
overtimeHrs, grossPay);
} // printEmp
// TODO: Add your functions here
//*************************************************************
// Function: calcOT
//
// Purpose: Calculates the number of hours worked overtime if
// the hours worked is more than the standard work week of 40
// hours.
//
// Parameters:
//
// hours - Hours worked for the week
//
// Returns: The total number of overtime hours worked.
//
//**************************************************************
float calcOT (float hours)
{
float overtimeWorked; /* holds value of overtime hours worked*/
if (hours > STD_HOURS) /* condition of hours worked is more than 40 hours*/
{
overtimeWorked = hours - STD_HOURS; /* get the hours worked by subtracting the total hours worked by STD_HOURS (40)*/
}
else /* condition if the hours worked is 40 hours or less*/
{
overtimeWorked = 0; /* since no overtime was worked, set overtime hours worked to 0*/
}
return(overtimeWorked);
} //calcOT
//*************************************************************
// Function: calcGross
//
// Purpose: Calculates the total gross pay with factoring in if
// there was any overtime hours worked.
//
// Parameters:
//
// hours - Hours worked for the week
// wageRate - hourly wage rate
// overtimeHrs - overtime hours worked in a week
//
// Returns: The total gross pay.
//
//**************************************************************
float calcGross (float wageRate, float hours, float overtimeHrs)
{
float pay; /* holds value of gross pay*/
if (hours > STD_HOURS) /* condition to calculate the gross pay if the number of hours worked is more than 40 hours*/
{
pay = (STD_HOURS * wageRate) + (overtimeHrs * wageRate * OT_RATE); /* calculate gross pay by adding the STD_HOURS (40) multiplied by the wage rate and the number of hours worked multiplied by the wage rate and and the additional overtime rate */
}
else /* condition to calculate the gross pay if the hours worked is 40 hours or less*/
{
pay = (hours * wageRate); /* calculate the gross pay by multiplying the number of hours worked by the wage rate */
}
return(pay);
} //calcGross