//********************************************************
//
// Assignment 8 - Structures and Strings and Pointers
//
// Name: John McKenna
//
// Class: C Programming, Spring 2024
//
// Date: 3/29/2024
//
// 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.
//
// Array and Structure references are to be replaced with
// pointer references to speed up the processing of this code.
//
// Call by Reference design (using pointers)
//
//********************************************************
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define SIZE 5
#define STD_HOURS 40.0
#define OT_RATE 1.5
#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
struct employee {
char name[NAME_SIZE];
char tax_state[TAX_STATE_SIZE];
int clock_num;
float wage;
float regular; // Added regular hours worked
float overtime; // Added overtime hours worked
float gross_pay; // Added gross pay
float state_tax; // Added state tax
float fed_tax; // Added federal tax
float net_pay; // Added net pay
};
struct totals {
float hours;
float regular;
float overtime;
float gross_pay;
float state_tax;
float fed_tax;
float net_pay;
};
struct min_max {
float min_wage;
float max_wage;
float min_hours;
float max_hours;
float min_ot;
float max_ot;
float min_gross;
float max_gross;
float min_state;
float max_state;
float min_fed;
float max_fed;
float min_net;
float max_net;
};
void getHours(struct employee *emp_ptr, int theSize);
void printEmp(struct employee *emp_ptr, int theSize);
void calcEmployeeTotals(struct employee *emp_ptr, struct totals *emp_totals_ptr, int theSize);
void calcEmployeeMinMax(struct employee *emp_ptr, struct min_max *emp_MinMax_ptr, int theSize);
void calcOvertimeHrs(struct employee employeeData[], int theSize);
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);
void printEmpStatistics(struct totals employeeTotals, struct min_max employeeMinMax, int theSize);
int main() {
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 }
};
struct totals employeeTotals = {0,0,0,0,0,0,0};
struct totals *emp_totals_ptr = &employeeTotals;
struct min_max employeeMinMax = {0,0,0,0,0,0,0,0,0,0,0,0,0,0};
struct min_max *emp_min_max_ptr = &employeeMinMax;
getHours(employeeData, SIZE);
calcOvertimeHrs(employeeData, SIZE);
calcGrossPay(employeeData, SIZE);
calcStateTax(employeeData, SIZE);
calcFedTax(employeeData, SIZE);
calcNetPay(employeeData, SIZE);
calcEmployeeTotals(employeeData, emp_totals_ptr, SIZE);
calcEmployeeMinMax(employeeData, emp_min_max_ptr, SIZE);
printEmp(employeeData, SIZE);
printEmpStatistics(employeeTotals, employeeMinMax, SIZE);
return 0;
}
//**************************************************************
// 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:
//
// emp_ptr - pointer to array of employees (i.e., struct employee)
// theSize - the array size (i.e., number of employees)
//
// Returns: void (the employee hours gets updated by reference)
//
//**************************************************************
void getHours(struct employee *emp_ptr, int theSize) {
printf("Please enter the hours worked for each employee:\n"); for (int i = 0; i < theSize; ++i) {
printf("%s %s: ", emp_ptr
[i
].
name, emp_ptr
[i
].
tax_state); scanf("%f", &emp_ptr
[i
].
wage); }
}
//*************************************************************
// Function: printEmp
//
// Purpose: Prints out all the information for each employee
// in a nice and orderly table format.
//
// Parameters:
//
// emp_ptr - pointer to array of struct employee
// theSize - the array size (i.e., number of employees)
//
// Returns: void
//
//**************************************************************
void printEmp(struct employee *emp_ptr, int theSize) {
printf("*** Employee Information ***\n"); printf("-------------------------------------------------------\n"); printf("Name Tax Clock# Wage Regular Overtime Gross Pay State Tax Fed Tax Net Pay\n"); printf("-------------------------------------------------------\n"); for (int i = 0; i < theSize; ++i) {
printf("%-20s %-3s %06d %5.2f %5.2f %5.2f %5.2f %5.2f %5.2f %5.2f\n", emp_ptr[i].name, emp_ptr[i].tax_state, emp_ptr[i].clock_num, emp_ptr[i].wage,
emp_ptr[i].regular, emp_ptr[i].overtime, emp_ptr[i].gross_pay,
emp_ptr[i].state_tax, emp_ptr[i].fed_tax, emp_ptr[i].net_pay);
}
printf("-------------------------------------------------------\n"); }
//*************************************************************
// Function: calcEmployeeTotals
//
// Purpose: Performs a running total (sum) of each employee
// floating point member in the array of structures
//
// Parameters:
//
// emp_ptr - pointer to array of employees (structure)
// emp_totals_ptr - pointer to a structure containing the
// running totals of all floating point
// members in the array of employee structure
// that is accessed and referenced by emp_ptr
// theSize - the array size (i.e., number of employees)
//
// Returns:
//
// void (the employeeTotals structure gets updated by reference)
//
//**************************************************************
void calcEmployeeTotals(struct employee *emp_ptr, struct totals *emp_totals_ptr, int theSize) {
for (int i = 0; i < theSize; ++i) {
emp_totals_ptr->gross_pay += emp_ptr[i].gross_pay;
emp_totals_ptr->state_tax += emp_ptr[i].state_tax;
emp_totals_ptr->fed_tax += emp_ptr[i].fed_tax;
emp_totals_ptr->net_pay += emp_ptr[i].net_pay;
}
}
//*************************************************************
// 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 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:
//
// employeeMinMax - updated employeeMinMax structure
//
//**************************************************************
void calcEmployeeMinMax(struct employee *emp_ptr, struct min_max *emp_MinMax_ptr, int theSize) {
emp_MinMax_ptr->min_wage = emp_ptr[0].wage;
emp_MinMax_ptr->max_wage = emp_ptr[0].wage;
emp_MinMax_ptr->min_hours = emp_ptr[0].regular;
emp_MinMax_ptr->max_hours = emp_ptr[0].regular;
emp_MinMax_ptr->min_ot = emp_ptr[0].overtime;
emp_MinMax_ptr->max_ot = emp_ptr[0].overtime;
emp_MinMax_ptr->min_gross = emp_ptr[0].gross_pay;
emp_MinMax_ptr->max_gross = emp_ptr[0].gross_pay;
emp_MinMax_ptr->min_state = emp_ptr[0].state_tax;
emp_MinMax_ptr->max_state = emp_ptr[0].state_tax;
emp_MinMax_ptr->min_fed = emp_ptr[0].fed_tax;
emp_MinMax_ptr->max_fed = emp_ptr[0].fed_tax;
emp_MinMax_ptr->min_net = emp_ptr[0].net_pay;
emp_MinMax_ptr->max_net = emp_ptr[0].net_pay;
for (int i = 1; i < theSize; ++i) {
if (emp_ptr[i].wage < emp_MinMax_ptr->min_wage)
emp_MinMax_ptr->min_wage = emp_ptr[i].wage;
if (emp_ptr[i].wage > emp_MinMax_ptr->max_wage)
emp_MinMax_ptr->max_wage = emp_ptr[i].wage;
if (emp_ptr[i].regular < emp_MinMax_ptr->min_hours)
emp_MinMax_ptr->min_hours = emp_ptr[i].regular;
if (emp_ptr[i].regular > emp_MinMax_ptr->max_hours)
emp_MinMax_ptr->max_hours = emp_ptr[i].regular;
if (emp_ptr[i].overtime < emp_MinMax_ptr->min_ot)
emp_MinMax_ptr->min_ot = emp_ptr[i].overtime;
if (emp_ptr[i].overtime > emp_MinMax_ptr->max_ot)
emp_MinMax_ptr->max_ot = emp_ptr[i].overtime;
if (emp_ptr[i].gross_pay < emp_MinMax_ptr->min_gross)
emp_MinMax_ptr->min_gross = emp_ptr[i].gross_pay;
if (emp_ptr[i].gross_pay > emp_MinMax_ptr->max_gross)
emp_MinMax_ptr->max_gross = emp_ptr[i].gross_pay;
if (emp_ptr[i].state_tax < emp_MinMax_ptr->min_state)
emp_MinMax_ptr->min_state = emp_ptr[i].state_tax;
if (emp_ptr[i].state_tax > emp_MinMax_ptr->max_state)
emp_MinMax_ptr->max_state = emp_ptr[i].state_tax;
if (emp_ptr[i].fed_tax < emp_MinMax_ptr->min_fed)
emp_MinMax_ptr->min_fed = emp_ptr[i].fed_tax;
if (emp_ptr[i].fed_tax > emp_MinMax_ptr->max_fed)
emp_MinMax_ptr->max_fed = emp_ptr[i].fed_tax;
if (emp_ptr[i].net_pay < emp_MinMax_ptr->min_net)
emp_MinMax_ptr->min_net = emp_ptr[i].net_pay;
if (emp_ptr[i].net_pay > emp_MinMax_ptr->max_net)
emp_MinMax_ptr->max_net = emp_ptr[i].net_pay;
}
}
//*************************************************************
// Function: calcOvertimeHrs
//
// Purpose: Calculates the overtime hours worked by an employee
// in a given week for each employee.
//
// Parameters:
//
// employeeData - array of employees (i.e., struct employee)
// theSize - the array size (i.e., number of employees)
//
// Returns: void (the overtime hours gets updated by reference)
//
//**************************************************************
void calcOvertimeHrs(struct employee employeeData[], int theSize) {
for (int i = 0; i < theSize; ++i) {
if (employeeData[i].wage > STD_HOURS) {
employeeData[i].regular = STD_HOURS;
employeeData[i].overtime = employeeData[i].wage - STD_HOURS;
} else {
employeeData[i].regular = employeeData[i].wage;
employeeData[i].overtime = 0;
}
}
}
//*************************************************************
// Function: calcGrossPay
//
// Purpose: Calculates the gross pay based on the the normal pay
// and any overtime pay for a given week for each
// employee.
//
// Parameters:
//
// employeeData - array of employees (i.e., struct employee)
// theSize - the array size (i.e., number of employees)
//
// Returns: void (the gross pay gets updated by reference)
//
//**************************************************************
void calcGrossPay(struct employee employeeData[], int theSize) {
for (int i = 0; i < theSize; ++i) {
employeeData[i].gross_pay = (employeeData[i].regular * employeeData[i].wage) + (employeeData[i].overtime * (employeeData[i].wage * OT_RATE));
}
}
//*************************************************************
// 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 employees (i.e., struct employee)
// theSize - the array size (i.e., number of employees)
//
// Returns: void (the state tax gets updated by reference)
//
//**************************************************************
void calcStateTax(struct employee employeeData[], int theSize) {
for (int i = 0; i < theSize; ++i) {
if (strcmp(employeeData
[i
].
tax_state, "MA") == 0) { employeeData[i].state_tax = employeeData[i].gross_pay * MA_TAX_RATE;
} else if (strcmp(employeeData
[i
].
tax_state, "NH") == 0) { employeeData[i].state_tax = employeeData[i].gross_pay * NH_TAX_RATE;
} else if (strcmp(employeeData
[i
].
tax_state, "VT") == 0) { employeeData[i].state_tax = employeeData[i].gross_pay * VT_TAX_RATE;
} else if (strcmp(employeeData
[i
].
tax_state, "CA") == 0) { employeeData[i].state_tax = employeeData[i].gross_pay * CA_TAX_RATE;
} else {
employeeData[i].state_tax = employeeData[i].gross_pay * DEFAULT_TAX_RATE;
}
}
}
//*************************************************************
// Function: calcFedTax
//
// Purpose: Calculates the Federal Tax owed based on the gross
// pay for each employee
//
// Parameters:
//
// employeeData - array of employees (i.e., struct employee)
// theSize - the array size (i.e., number of employees)
//
// Returns: void (the federal tax gets updated by reference)
//
//**************************************************************
void calcFedTax(struct employee employeeData[], int theSize) {
for (int i = 0; i < theSize; ++i) {
employeeData[i].fed_tax = employeeData[i].gross_pay * FED_TAX_RATE;
}
}
//*************************************************************
// 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 (the net pay gets updated by reference)
//
//**************************************************************
void calcNetPay(struct employee employeeData[], int theSize) {
for (int i = 0; i < theSize; ++i) {
employeeData[i].net_pay = employeeData[i].gross_pay - (employeeData[i].state_tax + employeeData[i].fed_tax);
}
}
//*************************************************************
// 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) {
printf("\n\n*** Totals & Averages ***\n"); printf("-------------------------------------------------------\n"); printf(" Totals Minimum Maximum\n"); printf("-------------------------------------------------------\n"); printf("Hours %.2f %.2f %.2f\n", employeeTotals.hours, employeeMinMax.min_hours, employeeMinMax.max_hours);
printf("Regular %.2f %.2f %.2f\n", employeeTotals.regular, employeeMinMax.min_wage, employeeMinMax.max_wage);
printf("Overtime %.2f %.2f %.2f\n", employeeTotals.overtime, employeeMinMax.min_ot, employeeMinMax.max_ot);
printf("Gross Pay %.2f %.2f %.2f\n", employeeTotals.gross_pay, employeeMinMax.min_gross, employeeMinMax.max_gross);
printf("State Tax %.2f %.2f %.2f\n", employeeTotals.state_tax, employeeMinMax.min_state, employeeMinMax.max_state);
printf("Federal Tax %.2f %.2f %.2f\n", employeeTotals.fed_tax, employeeMinMax.min_fed, employeeMinMax.max_fed);
printf("Net Pay %.2f %.2f %.2f\n", employeeTotals.net_pay, employeeMinMax.min_net, employeeMinMax.max_net);
printf("-------------------------------------------------------\n"); }