//********************************************************
//
// Assignment 7 - Structures and Strings
//
// Name: Po Yuen
//
// Class: C Programming, Spring 2026
//
// Date: March 25, 2026
//
// 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.
//
// Call by Reference design
//
//********************************************************
// necessary header files
#include <stdio.h> // standard input/output for printf and scanf
#include <string.h> // string functions like strcpy, strcat, strcmp
#include <ctype.h> // character functions like toupper, islower
// define constants
#define SIZE 5 // number of employees to process
#define STD_HOURS 40.0 // standard weekly hours before overtime
#define OT_RATE 1.5 // overtime pay multiplier (time and a half)
#define MA_TAX_RATE 0.05 // Massachusetts state tax rate (5%)
#define NH_TAX_RATE 0.0 // New Hampshire state tax rate (0%)
#define VT_TAX_RATE 0.06 // Vermont state tax rate (6%)
#define CA_TAX_RATE 0.07 // California state tax rate (7%)
#define DEFAULT_TAX_RATE 0.08 // default state tax rate for all others (8%)
#define NAME_SIZE 20 // max size for a full formatted name
#define TAX_STATE_SIZE 3 // size for two-char state code plus null
#define FED_TAX_RATE 0.25 // federal tax rate for all employees (25%)
#define FIRST_NAME_SIZE 10 // max size for employee first name
#define LAST_NAME_SIZE 10 // max size for employee last name
// Define a structure type to store an employee name
// ... note how one could easily extend this to other parts
// parts of a name: Middle, Nickname, Prefix, Suffix, etc.
struct name
{
char firstName[FIRST_NAME_SIZE]; // employee first name
char lastName [LAST_NAME_SIZE]; // employee last name
};
// Define a structure type 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
{
struct name empName; // employee name (first and last)
char taxState [TAX_STATE_SIZE]; // two-character state tax code
long int clockNumber; // unique employee clock number
float wageRate; // hourly wage rate
float hours; // total hours worked this week
float overtimeHrs; // overtime hours beyond standard
float grossPay; // total gross pay before taxes
float stateTax; // calculated state tax amount
float fedTax; // calculated federal tax amount
float netPay; // take home pay after all taxes
};
// 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; // running total of all wage rates
float total_hours; // running total of all hours worked
float total_overtimeHrs; // running total of all overtime hours
float total_grossPay; // running total of all gross pay
float total_stateTax; // running total of all state taxes
float total_fedTax; // running total of all federal taxes
float total_netPay; // running total of all net pay
};
// this structure type defines the min and max values of all floating
// point items so they can be displayed in our final report
struct min_max
{
float min_wageRate; // minimum wage rate across all employees
float min_hours; // minimum hours worked across all employees
float min_overtimeHrs; // minimum overtime hours across all employees
float min_grossPay; // minimum gross pay across all employees
float min_stateTax; // minimum state tax across all employees
float min_fedTax; // minimum federal tax across all employees
float min_netPay; // minimum net pay across all employees
float max_wageRate; // maximum wage rate across all employees
float max_hours; // maximum hours worked across all employees
float max_overtimeHrs; // maximum overtime hours across all employees
float max_grossPay; // maximum gross pay across all employees
float max_stateTax; // maximum state tax across all employees
float max_fedTax; // maximum federal tax across all employees
float max_netPay; // maximum net pay across all employees
};
// define prototypes here for each function except main
void getHours (struct employee employeeData[], int theSize);
void calcOvertimeHrs (struct employee employeeData[], int theSize);
void calcGrossPay (struct employee employeeData[], int theSize);
void printHeader (void);
void printEmp (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 printEmpStatistics (struct totals employeeTotals,
struct min_max employeeMinMax,
int theSize);
//**************************************************************
// Function: main
//
// Purpose: Entry point of the program. Sets up the array of
// employee structures with initial data, calls all
// processing and output functions in the correct order,
// and returns a success status.
//
// Parameters: none
//
// Returns: int - 0 for success
//
//**************************************************************
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 }
};
// set up structure to store totals and initialize all to zero
struct totals employeeTotals = {0,0,0,0,0,0,0};
// set up structure to store min and max values and initialize all 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 the number of hours worked by each employee
getHours (employeeData, SIZE);
// Calculate the overtime hours for each employee
calcOvertimeHrs (employeeData, SIZE);
// Calculate the weekly gross pay for each employee
calcGrossPay (employeeData, SIZE);
// Calculate the state tax for each employee
calcStateTax (employeeData, SIZE);
// Calculate the federal tax for each employee
calcFedTax (employeeData, SIZE);
// Calculate the net pay after taxes for each employee
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 column headers
printHeader();
// Print out final information on each employee
printEmp (employeeData, SIZE);
// Print the totals, averages, min, and max for all float items
printEmpStatistics (employeeTotals, employeeMinMax, SIZE);
return (0); // success
} // main
//**************************************************************
// Function: getHours
//
// Purpose: Obtains input from user, the number of hours worked
// per employee and updates it in the array of structures
// for each employee.
//
// Parameters:
//
// employeeData - array of employees (i.e., struct employee)
// theSize - the array size (i.e., number of employees)
//
// Returns: void
//
//**************************************************************
void getHours (struct employee employeeData[], int theSize)
{
int i; // array and loop index
// loop through each employee to read in their hours
for (i = 0; i < theSize; ++i)
{
// Prompt user and read in hours for the current employee
printf("\nEnter hours worked by emp # %06li: ", employeeData
[i
].
clockNumber); scanf ("%f", &employeeData
[i
].
hours); } // for
} // getHours
//**************************************************************
// Function: printHeader
//
// Purpose: Prints the initial table header information including
// the report title and column headings for all employee
// data items.
//
// Parameters: none
//
// Returns: void
//
//**************************************************************
void printHeader (void)
{
// print the report title
printf ("\n\n*** Pay Calculator ***\n"); // print the top separator line
printf("\n--------------------------------------------------------------"); printf("-------------------"); // print the first row of column headers
printf("\nName Tax Clock# Wage Hours OT Gross "); // print the second row of column headers (sub-headers)
// print the bottom separator line under the header
printf("\n--------------------------------------------------------------"); printf("-------------------"); } // printHeader
//*************************************************************
// Function: printEmp
//
// Purpose: Prints out all the information for each employee
// in a nice and orderly table format.
//
// Parameters:
//
// employeeData - array of struct employee
// theSize - the array size (i.e., number of employees)
//
// Returns: void
//
//**************************************************************
void printEmp (struct employee employeeData[], int theSize)
{
int i; // array and loop index
// buffer to hold the formatted full name (first + space + last)
char name [FIRST_NAME_SIZE + LAST_NAME_SIZE + 1];
// loop through each employee and print their data
for (i = 0; i < theSize; ++i)
{
// Build the full name string by copying first name into buffer
strcpy (name
, employeeData
[i
].
empName.
firstName); // Append a space between first and last names
// Append the last name to complete the full name
strcat (name
, employeeData
[i
].
empName.
lastName);
// Print out a single employee row with all data items formatted
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
} // printEmp
//*************************************************************
// 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 to mark the end of employee rows
printf("\n--------------------------------------------------------------"); printf("-------------------");
// print the totals row for all 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 row by dividing each total by the number of employees
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 minimum values row for all floating point fields
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);
// print the maximum values row for all floating point fields
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
//*************************************************************
// Function: calcOvertimeHrs
//
// Purpose: Calculates the overtime hours worked by an employee
// in a given week for each employee. Any hours over
// the standard 40 hours are considered overtime.
//
// Parameters:
//
// employeeData - array of employees (i.e., struct employee)
// theSize - the array size (i.e., number of employees)
//
// Returns: void
//
//**************************************************************
void calcOvertimeHrs (struct employee employeeData[], int theSize)
{
int i; // array and loop index
// loop through each employee to calculate their overtime hours
for (i = 0; i < theSize; ++i)
{
// Determine if the employee worked more than standard hours
if (employeeData[i].hours >= STD_HOURS)
{
// overtime is total hours minus the standard hours
employeeData[i].overtimeHrs = employeeData[i].hours - STD_HOURS;
}
else // no overtime worked this week
{
// set overtime hours to zero
employeeData[i].overtimeHrs = 0;
}
} // for
} // calcOvertimeHrs
//*************************************************************
// Function: calcGrossPay
//
// Purpose: Calculates the gross pay based on the normal pay
// and any overtime pay for a given week for each
// employee. Overtime is paid at 1.5 times the
// normal wage rate.
//
// Parameters:
//
// employeeData - array of employees (i.e., struct employee)
// theSize - the array size (i.e., number of employees)
//
// Returns: void
//
//**************************************************************
void calcGrossPay (struct employee employeeData[], int theSize)
{
int i; // loop and array index
float theNormalPay; // normal pay for hours at regular rate
float theOvertimePay; // additional pay for overtime hours
// loop through each employee to calculate their gross pay
for (i=0; i < theSize; ++i)
{
// calculate normal pay: wage rate times regular (non-overtime) hours
theNormalPay = employeeData[i].wageRate *
(employeeData[i].hours - employeeData[i].overtimeHrs);
// calculate overtime pay: OT hours times 1.5 times the wage rate
theOvertimePay = employeeData[i].overtimeHrs *
(OT_RATE * employeeData[i].wageRate);
// gross pay is the sum of normal pay and any overtime pay
employeeData[i].grossPay = theNormalPay + theOvertimePay;
} // 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:
// MA = 5%, NH = 0%, VT = 6%, CA = 7%
// All other states = 8% (default)
//
// Parameters:
//
// employeeData - array of employees (i.e., struct employee)
// theSize - the array size (i.e., number of employees)
//
// Returns: void
//
//**************************************************************
void calcStateTax (struct employee employeeData[], int theSize)
{
int i; // loop and array index
// loop through each employee to calculate their state tax
for (i=0; i < theSize; ++i)
{
// Ensure the first character of taxState is uppercase
if (islower(employeeData
[i
].
taxState[0])) employeeData
[i
].
taxState[0] = toupper(employeeData
[i
].
taxState[0]); // Ensure the second character of taxState is uppercase
if (islower(employeeData
[i
].
taxState[1])) employeeData
[i
].
taxState[1] = toupper(employeeData
[i
].
taxState[1]);
// Determine the tax rate based on the employee's tax state
if (strcmp(employeeData
[i
].
taxState, "MA") == 0) {
// Massachusetts tax: 5% of gross pay
employeeData[i].stateTax = employeeData[i].grossPay * MA_TAX_RATE;
}
else if (strcmp(employeeData
[i
].
taxState, "NH") == 0) {
// New Hampshire tax: 0% of gross pay (no state income tax)
employeeData[i].stateTax = employeeData[i].grossPay * NH_TAX_RATE;
}
else if (strcmp(employeeData
[i
].
taxState, "VT") == 0) {
// Vermont tax: 6% of gross pay
employeeData[i].stateTax = employeeData[i].grossPay * VT_TAX_RATE;
}
else if (strcmp(employeeData
[i
].
taxState, "CA") == 0) {
// California tax: 7% of gross pay
employeeData[i].stateTax = employeeData[i].grossPay * CA_TAX_RATE;
}
else
{
// All other states use the default tax rate of 8%
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. All employees pay a flat
// federal tax rate of 25% regardless of their state.
//
// Parameters:
//
// employeeData - array of employees (i.e., struct employee)
// theSize - the array size (i.e., number of employees)
//
// Returns: void
//
//**************************************************************
void calcFedTax (struct employee employeeData[], int theSize)
{
int i; // loop and array index
// loop through each employee to calculate their federal tax
for (i=0; i < theSize; ++i)
{
// Federal tax is a flat 25% of gross pay for all employees
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 employees (i.e., struct employee)
// theSize - the array size (i.e., number of employees)
//
// Returns: void
//
//**************************************************************
void calcNetPay (struct employee employeeData[], int theSize)
{
int i; // loop and array index
float theTotalTaxes; // the combined total of state and federal tax
// loop through each employee to calculate their take home pay
for (i=0; i < theSize; ++i)
{
// sum up the state and federal taxes for this employee
theTotalTaxes = employeeData[i].stateTax + employeeData[i].fedTax;
// net pay is gross pay minus the total taxes paid
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.
// These totals are used for the Totals and Averages
// rows in the summary report.
//
// Parameters:
//
// employeeData - array of employees (i.e., struct employee)
// employeeTotals - structure containing a running totals
// of all fields above
// theSize - the array size (i.e., number of employees)
//
// 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
// loop through each employee and accumulate running totals
for (i = 0; i < theSize; ++i)
{
// add the current employee's wage rate to the running total
employeeTotals.total_wageRate += employeeData[i].wageRate;
// add the current employee's hours worked to the running total
employeeTotals.total_hours += employeeData[i].hours;
// add the current employee's overtime hours to the running total
employeeTotals.total_overtimeHrs += employeeData[i].overtimeHrs;
// add the current employee's gross pay to the running total
employeeTotals.total_grossPay += employeeData[i].grossPay;
// add the current employee's state tax to the running total
employeeTotals.total_stateTax += employeeData[i].stateTax;
// add the current employee's federal tax to the running total
employeeTotals.total_fedTax += employeeData[i].fedTax;
// add the current employee's net pay to the running total
employeeTotals.total_netPay += employeeData[i].netPay;
} // for
// return the updated totals structure back to the caller
return (employeeTotals);
} // calcEmployeeTotals
//*************************************************************
// Function: calcEmployeeMinMax
//
// Purpose: Determines the minimum and maximum values for each
// floating point data item across all employees.
// Initializes min and max to the first employee's
// values, then compares the remaining employees.
//
// Parameters:
//
// employeeData - array of employees (i.e., struct employee)
// employeeMinMax - structure containing the current min and
// max values for all floating point fields
// theSize - the array size (i.e., number of employees)
//
// Returns: employeeMinMax - updated employeeMinMax structure
//
//**************************************************************
struct min_max calcEmployeeMinMax (struct employee employeeData[],
struct min_max employeeMinMax,
int theSize)
{
int i; // array and loop index
// Initialize all min values to the first employee's data
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;
// Initialize all max values to the first employee's data
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;
// Loop through remaining employees (starting at index 1) and
// compare each data item to find the overall min and max
for (i = 1; i < theSize; ++i)
{
// --- Wage Rate: check for 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;
}
// --- Hours: check for 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;
}
// --- Overtime Hours: check for 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;
}
// --- Gross Pay: check for 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;
}
// --- State Tax: check for 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;
}
// --- Federal Tax: check for 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;
}
// --- Net Pay: check for 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;
}
} // for
// return all the updated min and max values to the calling function
return (employeeMinMax);
} // calcEmployeeMinMax