//********************************************************
//
// Assignment 7 - Structures and Strings
//
// Name: Timothy Stockton
//
// Class: C Programming, Summer 2025
//
// Date: July 13th, 2025
//
// Description: Program which determines overtime and
// gross pay for a set of employees with outputs sent
// to standard output (the screen).
//
// This assignment also adds the employee name, their tax state,
// and calculates the state tax, federal tax, and net pay. It
// also calculates totals, averages, minimum, and maximum values.
//
// Functions are called by reference
//
//********************************************************
// necessary header files
#include <stdio.h>
#include <string.h>
#include <ctype.h>
// constants
#define SIZE 5 // number of employees to process
#define OVERTIME_RATE 1.5f
#define STD_WORK_WEEK 40.0f
#define MA_TAX_RATE 0.05
#define NH_TAX_RATE 0.0
#define VT_TAX_RATE 0.06
#define CA_TAX_RATE 0.07
#define DEFAULT_TAX_RATE 0.08
#define NAME_SIZE 20
#define TAX_STATE_SIZE 3
#define FED_TAX_RATE 0.25
#define FIRST_NAME_SIZE 10
#define LAST_NAME_SIZE 10
// define a structure type to store an employee name
struct name
{
char firstName [10];
char lastName [10];
};
// define a structure type to pass employee data between functions
struct employee
{
struct name empName;
char taxState [3];
long int clockNumber;
float wageRate;
float hours;
float overtimeHrs;
float normalPay;
float overtimePay;
float grossPay;
float stateTax;
float fedTax;
float netPay;
};
// this structure type defines the totals of all floating point items
// so they can be totaled and used also to calculate averages
struct totals
{
float total_wageRate;
float total_hours;
float total_overtimeHrs;
float total_grossPay;
float total_stateTax;
float total_fedTax;
float total_netPay;
};
// this structure type defines the min and max values of all floating
// point items so they can be displayed in the final report
struct min_max
{
float min_wageRate;
float min_hours;
float min_overtimeHrs;
float min_grossPay;
float min_stateTax;
float min_fedTax;
float min_netPay;
float max_wageRate;
float max_hours;
float max_overtimeHrs;
float max_grossPay;
float max_stateTax;
float max_fedTax;
float max_netPay;
};
// function prototypes
void getHours (struct employee employeeData[], int theSize);
void calcOvertime (struct employee employeeData[], int theSize,
float overtimeRate, float standardWeek);
void calcGrossPay (struct employee employeeData[], int theSize);
void calcStateTax (struct employee employeeData[], int theSize);
void calcFedTax (struct employee employeeData[], int theSize);
void calcNetPay (struct employee employeeData[], int theSize);
struct totals calcEmployeeTotals (struct employee employeeData[],
struct totals employeeTotals, int theSize);
struct min_max calcEmployeeMinMax (struct employee employeeData[],
struct min_max employeeMinMax, int theSize);
void printHeader (void);
void printEmps (struct employee employeeData[], int theSize);
void printEmpStatistics (struct totals employeeTotals,
struct min_max employeeMinMax, int theSize);
int main()
{
// set up a local variable to store the employee information
// initialize the name, tax state, clock number, and wage rate
struct employee employeeData[SIZE] = {
{ {"Connie", "Cobol"}, "MA", 98401, 10.60},
{ {"Mary", "Apl"}, "NH", 526488, 9.75 },
{ {"Frank", "Fortran"}, "VT", 765349, 10.50 },
{ {"Jeff", "Ada"}, "NY", 34645, 12.25 },
{ {"Anton", "Pascal"},"CA",127615, 8.35 }
};
// structure variable to store totals and initialize
// all the members to zero
struct totals employeeTotals = {0,0,0,0,0,0,0};
// structure variable to store min/max values, initialize
// all the members to zero
struct min_max employeeMinMax = {0,0,0,0,0,0,0,0,0,0,0,0,0,0};
// call functions as needed to read and calculate information
// prompt for and read in the hours worked for each employee
getHours (employeeData, SIZE);
// calculate overtime hours and pay (as well as normal pay)
calcOvertime (employeeData, SIZE, OVERTIME_RATE, STD_WORK_WEEK);
// calculate gross pay
calcGrossPay (employeeData, SIZE);
// calculate the state tax
calcStateTax (employeeData, SIZE);
// calculate the federal tax
calcFedTax (employeeData, SIZE);
// calculate the net pay after taxes
calcNetPay (employeeData, SIZE);
// keep a running sum of the employee totals
// Note: This remains a Call by Value design
employeeTotals = calcEmployeeTotals (employeeData, employeeTotals, SIZE);
// keep a running update of the employee minimum and maximum values
// Note: This remains a Call by Value design
employeeMinMax = calcEmployeeMinMax (employeeData,
employeeMinMax,
SIZE);
// print the initial table header
printHeader ();
// print out final information on each employee
printEmps (employeeData, SIZE);
// print the totals and averages for all float items
printEmpStatistics (employeeTotals, employeeMinMax, SIZE);
return (0);
} // main
//***************************************************************
// Function: getHours
//
// Purpose: Obtains input from user, the number of hours worked
// per employee, and stores the results in the referenced array
// of employee structures.
//
// Parameters:
//
// employeeData - Array of employee structures
// theSize - Number of employees to process
//
// Returns: void
//
//**************************************************************
void getHours (struct employee employeeData[], int theSize)
{
int i; // loop and array index
// Read in hours for each employee
for (i= 0; i < theSize; ++i)
{
printf("\nEnter hours worked by emp # %06li: ", employeeData
[i
].
clockNumber); scanf ("%f", &employeeData
[i
].
hours); }
} // getHours
//***************************************************************
// Function: calcOvertime
//
// Purpose: Calculates the normal pay and overtime pay of all employees
// based on the hours worked, wage rate, overtime rate, and standard
// week hours. Stores the results in the referenced array
// of employee structures.
//
// Parameters:
//
// employeeData - Array of employee structures
// theSize - Number of employees to process
// overtimeRate - The multiplier to wages for overtime hours
// standardWeek - Number of hours in a standard work week, no overtime
//
// Returns: void
//
//**************************************************************
void calcOvertime (struct employee employeeData[], int theSize,
float overtimeRate, float standardWeek)
{
int i; // loop and array index
// Process each employee one at a time
for (i = 0; i < theSize; i++)
{
// Calculate overtime and gross pay for employee
if (employeeData[i].hours > standardWeek)
{
employeeData[i].overtimeHrs = employeeData[i].hours - standardWeek;
employeeData[i].overtimePay = employeeData[i].overtimeHrs *
(employeeData[i].wageRate * overtimeRate);
employeeData[i].normalPay = standardWeek * employeeData[i].wageRate;
}
else // no OT
{
employeeData[i].overtimeHrs = 0;
employeeData[i].overtimePay = 0;
employeeData[i].normalPay = employeeData[i].hours * employeeData[i].wageRate;
}
} // end for
} // calcOvertime
//***************************************************************
// Function: calcGrossPay
//
// Purpose: Calculates the gross pay fr each employee, the total pay for the week
// including normal and overtime pay. Stores the results in the referenced array
// of employee structures.
//
// Parameters:
//
// employeeData - Array of employee structures
// theSize - Number of employees to process
//
// Returns: void
//
//**************************************************************
void calcGrossPay (struct employee employeeData[], int theSize)
{
int i; // loop and array index
// Process each employee one at a time
for (i = 0; i < theSize; i++)
{
// Calculate Gross Pay
employeeData[i].grossPay = employeeData[i].normalPay + employeeData[i].overtimePay;
} // end for
} // calcGrossPay
//***************************************************************
// Function: calcStateTax
//
// Purpose: Calculates the State Tax owed based on gross pay
// for each employee. State tax rate is based on the
// the designated tax state based on where the
// employee is actually performing the work. Each
// state decides their tax rate.
//
// Parameters:
//
// employeeData - Array of employee structures
// theSize - Number of employees to process
//
// Returns: void
//
//**************************************************************
void calcStateTax (struct employee employeeData[], int theSize)
{
int i; // loop and array index
// calculate state tax based on where employee works
for (i=0; i < theSize; ++i)
{
// Make sure tax state is all uppercase
if (islower(employeeData
[i
].
taxState[0])) employeeData
[i
].
taxState[0] = toupper(employeeData
[i
].
taxState[0]); if (islower(employeeData
[i
].
taxState[1])) employeeData
[i
].
taxState[1] = toupper(employeeData
[i
].
taxState[1]);
// calculate state tax based on where employee resides
if (strcmp(employeeData
[i
].
taxState, "MA") == 0) employeeData[i].stateTax = employeeData[i].grossPay * MA_TAX_RATE;
else if (strcmp(employeeData
[i
].
taxState, "NH") == 0) employeeData[i].stateTax = employeeData[i].grossPay * NH_TAX_RATE;
else if (strcmp(employeeData
[i
].
taxState, "VT") == 0) employeeData[i].stateTax = employeeData[i].grossPay * VT_TAX_RATE;
else if (strcmp(employeeData
[i
].
taxState, "CA") == 0) employeeData[i].stateTax = employeeData[i].grossPay * CA_TAX_RATE;
else
// any other state is the default rate
employeeData[i].stateTax = employeeData[i].grossPay * DEFAULT_TAX_RATE;
} // for
} // calcStateTax
//***************************************************************
// Function: calcFedTax
//
// Purpose: Calculates the Federal Tax owed based on the gross
// pay for each employee
//
// Parameters:
//
// employeeData - Array of employee structures
// theSize - Number of employees to process
//
// Returns: void
//
//**************************************************************
void calcFedTax (struct employee employeeData[], int theSize)
{
int i; // loop and array index
// calculate the federal tax for each employee
for (i=0; i < theSize; ++i)
{
// Fed Tax is the same for all regardless of state
employeeData[i].fedTax = employeeData[i].grossPay * FED_TAX_RATE;
} // for
} // calcFedTax
//***************************************************************
// Function: calcNetPay
//
// Purpose: Calculates the net pay as the gross pay minus any
// state and federal taxes owed for each employee.
// Essentially, their "take home" pay.
//
// Parameters:
//
// employeeData - Array of employee structures
// theSize - Number of employees to process
//
// Returns: void
//
//**************************************************************
void calcNetPay (struct employee employeeData[], int theSize)
{
int i; // loop and array index
float theTotalTaxes; // the total state and federal tax
// calculate the take home pay for each employee
for (i=0; i < theSize; ++i)
{
// calculate the total state and federal taxes
theTotalTaxes = employeeData[i].stateTax + employeeData[i].fedTax;
// calculate the net pay
employeeData[i].netPay = employeeData[i].grossPay - theTotalTaxes;
} // for
} // calcNetPay
//***************************************************************
// Function: calcEmployeeTotals
//
// Purpose: Performs a running total (sum) of each employee
// floating point member in the array of structures
//
// Parameters:
//
// employeeData - Array of employee structures
// employeeTotals - structure used to track totals as processed
// theSize - Number of employees to process
//
// Returns: employeeTotals - updated totals in the updated
// employeeTotals structure
//
//**************************************************************
struct totals calcEmployeeTotals (struct employee employeeData[],
struct totals employeeTotals, int theSize)
{
int i; // loop and array index
// total up each floating point item for all employees
for (i = 0; i < theSize; ++i)
{
// add current employee data to our running totals
employeeTotals.total_wageRate += employeeData[i].wageRate;
employeeTotals.total_hours += employeeData[i].hours;
employeeTotals.total_overtimeHrs += employeeData[i].overtimeHrs;
employeeTotals.total_grossPay += employeeData[i].grossPay;
employeeTotals.total_stateTax += employeeData[i].stateTax;
employeeTotals.total_fedTax += employeeData[i].fedTax;
employeeTotals.total_netPay += employeeData[i].netPay;
} // for
return (employeeTotals);
} // calcEmployeeTotals
//***************************************************************
// Function: calcEmployeeMinMax
//
// Purpose: Accepts various floating point values from an
// employee and adds to a running update of min
// and max values
//
// Parameters:
//
// employeeData - Array of employee structures
// employeeMinMax - structure used to track values as processed
// theSize - Number of employees to process
//
// Returns: employeeMinMax - updated employeeMinMax structure
//
//**************************************************************
struct min_max calcEmployeeMinMax (struct employee employeeData[],
struct min_max employeeMinMax, int theSize)
{
int i; // array and loop index
// if this is the first set of data items, set
// them to the min and max
employeeMinMax.min_wageRate = employeeData[0].wageRate;
employeeMinMax.min_hours = employeeData[0].hours;
employeeMinMax.min_overtimeHrs = employeeData[0].overtimeHrs;
employeeMinMax.min_grossPay = employeeData[0].grossPay;
employeeMinMax.min_stateTax = employeeData[0].stateTax;
employeeMinMax.min_fedTax = employeeData[0].fedTax;
employeeMinMax.min_netPay = employeeData[0].netPay;
// set the max to the first element members
employeeMinMax.max_wageRate = employeeData[0].wageRate;
employeeMinMax.max_hours = employeeData[0].hours;
employeeMinMax.max_overtimeHrs = employeeData[0].overtimeHrs;
employeeMinMax.max_grossPay = employeeData[0].grossPay;
employeeMinMax.max_stateTax = employeeData[0].stateTax;
employeeMinMax.max_fedTax = employeeData[0].fedTax;
employeeMinMax.max_netPay = employeeData[0].netPay;
// compare the rest of the items to each other for min and max
for (i = 1; i < theSize; ++i)
{
// check if current wageRate is the new min and/or max
if (employeeData[i].wageRate < employeeMinMax.min_wageRate)
{
employeeMinMax.min_wageRate = employeeData[i].wageRate;
}
if (employeeData[i].wageRate > employeeMinMax.max_wageRate)
{
employeeMinMax.max_wageRate = employeeData[i].wageRate;
}
// check if current hours is the new min and/or max
if (employeeData[i].hours < employeeMinMax.min_hours)
{
employeeMinMax.min_hours = employeeData[i].hours;
}
if (employeeData[i].hours > employeeMinMax.max_hours)
{
employeeMinMax.max_hours = employeeData[i].hours;
}
// check if current overtimeHrs is the new min and/or max
if (employeeData[i].overtimeHrs < employeeMinMax.min_overtimeHrs)
{
employeeMinMax.min_overtimeHrs = employeeData[i].overtimeHrs;
}
if (employeeData[i].overtimeHrs > employeeMinMax.max_overtimeHrs)
{
employeeMinMax.max_overtimeHrs = employeeData[i].overtimeHrs;
}
// check if current grossPay is the new min and/or max
if (employeeData[i].grossPay < employeeMinMax.min_grossPay)
{
employeeMinMax.min_grossPay = employeeData[i].grossPay;
}
if (employeeData[i].grossPay > employeeMinMax.max_grossPay)
{
employeeMinMax.max_grossPay = employeeData[i].grossPay;
}
// check if current stateTax is the new min and/or max
if (employeeData[i].stateTax < employeeMinMax.min_stateTax)
{
employeeMinMax.min_stateTax = employeeData[i].stateTax;
}
if (employeeData[i].stateTax > employeeMinMax.max_stateTax)
{
employeeMinMax.max_stateTax = employeeData[i].stateTax;
}
// check if current fedTax is the new min and/or max
if (employeeData[i].fedTax < employeeMinMax.min_fedTax)
{
employeeMinMax.min_fedTax = employeeData[i].fedTax;
}
if (employeeData[i].fedTax > employeeMinMax.max_fedTax)
{
employeeMinMax.max_fedTax = employeeData[i].fedTax;
}
// check if current netPay is the new min and/or max
if (employeeData[i].netPay < employeeMinMax.min_netPay)
{
employeeMinMax.min_netPay = employeeData[i].netPay;
}
if (employeeData[i].netPay > employeeMinMax.max_netPay)
{
employeeMinMax.max_netPay = employeeData[i].netPay;
}
} // else if
// return all the updated min and max values to the calling function
return (employeeMinMax);
} // calcEmployeeMinMax
//**************************************************************
// 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("\n--------------------------------------------------------------"); printf("-------------------"); printf("\nName Tax Clock# Wage Hours OT Gross ");
printf("\n--------------------------------------------------------------"); printf("-------------------");
} // printHeader
//**************************************************************
// Function: printEmps
//
// Purpose: Prints out all the employee information in a
// nice and orderly table format.
//
// Parameters:
//
// employeeData - Array of employee structures
// theSize - Number of employees to process
//
// Returns: void
//
//**************************************************************
void printEmps (struct employee employeeData[], int theSize)
{
int i; // loop and array index
// used to format the employee name
char name [FIRST_NAME_SIZE + LAST_NAME_SIZE + 1];
// for each employee
for (i = 0; i < theSize; ++i)
{
// While you could just print the first and last name in the printf
// statement that follows, you could also use various C string library
// functions to format the name exactly the way you want it. Breaking
// the name into first and last members additionally gives you some
// flexibility in printing. This also becomes more useful if we decide
// later to store other parts of a person's name. I really did this just
// to show you how to work with some of the common string functions.
strcpy (name
, employeeData
[i
].
empName.
firstName); strcat (name
, " "); // add a space between first and last names strcat (name
, employeeData
[i
].
empName.
lastName);
// Print out a single employee
printf("\n%-20.20s %-2.2s %06li %5.2f %4.1f %4.1f %7.2f %6.2f %7.2f %8.2f", name, employeeData[i].taxState, employeeData[i].clockNumber,
employeeData[i].wageRate, employeeData[i].hours,
employeeData[i].overtimeHrs, employeeData[i].grossPay,
employeeData[i].stateTax, employeeData[i].fedTax,
employeeData[i].netPay);
} // for
} // printEmps
//**************************************************************
// Function: printEmpStatistics
//
// Purpose: Prints out the summary totals and averages of all
// floating point value items for all employees
// that have been processed. It also prints
// out the min and max values.
//
// Parameters:
//
// employeeTotals - a structure containing a running total
// of all employee floating point items
// employeeMinMax - a structure containing all the minimum
// and maximum values of all employee
// floating point items
// theSize - the total number of employees processed, used
// to check for zero or negative divide condition.
//
// Returns: void
//
//**************************************************************
void printEmpStatistics (struct totals employeeTotals,
struct min_max employeeMinMax, int theSize)
{
// print a separator line
printf("\n--------------------------------------------------------------"); printf("-------------------");
// print the totals for all the floating point fields
printf("\nTotals: %5.2f %5.1f %5.1f %7.2f %6.2f %7.2f %8.2f", employeeTotals.total_wageRate,
employeeTotals.total_hours,
employeeTotals.total_overtimeHrs,
employeeTotals.total_grossPay,
employeeTotals.total_stateTax,
employeeTotals.total_fedTax,
employeeTotals.total_netPay);
// make sure you don't divide by zero or a negative number
if (theSize > 0)
{
// print the averages for all the floating point fields
printf("\nAverages: %5.2f %5.1f %5.1f %7.2f %6.2f %7.2f %8.2f", employeeTotals.total_wageRate/theSize,
employeeTotals.total_hours/theSize,
employeeTotals.total_overtimeHrs/theSize,
employeeTotals.total_grossPay/theSize,
employeeTotals.total_stateTax/theSize,
employeeTotals.total_fedTax/theSize,
employeeTotals.total_netPay/theSize);
} // if
// print the min and max values
printf("\nMinimum: %5.2f %5.1f %5.1f %7.2f %6.2f %7.2f %8.2f", employeeMinMax.min_wageRate,
employeeMinMax.min_hours,
employeeMinMax.min_overtimeHrs,
employeeMinMax.min_grossPay,
employeeMinMax.min_stateTax,
employeeMinMax.min_fedTax,
employeeMinMax.min_netPay);
printf("\nMaximum: %5.2f %5.1f %5.1f %7.2f %6.2f %7.2f %8.2f", employeeMinMax.max_wageRate,
employeeMinMax.max_hours,
employeeMinMax.max_overtimeHrs,
employeeMinMax.max_grossPay,
employeeMinMax.max_stateTax,
employeeMinMax.max_fedTax,
employeeMinMax.max_netPay);
} // printEmpStatistics