fork download
  1. //********************************************************
  2. //
  3. // Assignment 7 - Structures and Strings
  4. //
  5. // Name: John Semenuk
  6. //
  7. // Class: C Programming, Spring 2026
  8. //
  9. // Date: 29-Mar-2026
  10. //
  11. // Description: Program calculates overtime, gross pay, state & federal tax,
  12. // net pay, and prints totals, averages, min/max values
  13. // for a group of employees. Uses structures, arrays, and functions.
  14. //
  15. //********************************************************
  16.  
  17. #include <stdio.h>
  18. #include <string.h>
  19. #include <ctype.h>
  20.  
  21. //****************************
  22. // Constants
  23. //****************************
  24. #define SIZE 5 // Number of employees
  25. #define STD_HOURS 40.0 // Standard work hours per week
  26. #define OT_RATE 1.5 // Overtime pay multiplier
  27. #define MA_TAX_RATE 0.05 // Massachusetts state tax rate
  28. #define NH_TAX_RATE 0.0 // New Hampshire state tax rate
  29. #define VT_TAX_RATE 0.06 // Vermont state tax rate
  30. #define CA_TAX_RATE 0.07 // California state tax rate
  31. #define DEFAULT_TAX_RATE 0.08 // Default tax rate for all other states
  32. #define FED_TAX_RATE 0.25 // Federal tax rate
  33. #define FIRST_NAME_SIZE 10 // Max length of first name
  34. #define LAST_NAME_SIZE 10 // Max length of last name
  35. #define TAX_STATE_SIZE 3 // Size for 2-character state abbreviation + null
  36.  
  37. //****************************
  38. // Structures
  39. //****************************
  40.  
  41. // Stores an employee's first and last name
  42. struct name {
  43. char firstName[FIRST_NAME_SIZE];
  44. char lastName[LAST_NAME_SIZE];
  45. };
  46.  
  47. // Stores all data related to an employee
  48. struct employee {
  49. struct name empName; // Employee's name
  50. char taxState[TAX_STATE_SIZE]; // Employee's state for tax purposes
  51. long int clockNumber; // Employee clock number
  52. float wageRate; // Hourly wage rate
  53. float hours; // Total hours worked
  54. float overtimeHrs; // Overtime hours worked
  55. float grossPay; // Total gross pay
  56. float stateTax; // Calculated state tax
  57. float fedTax; // Calculated federal tax
  58. float netPay; // Net pay after taxes
  59. };
  60.  
  61. // Stores totals for all employees
  62. struct totals {
  63. float total_wageRate;
  64. float total_hours;
  65. float total_overtimeHrs;
  66. float total_grossPay;
  67. float total_stateTax;
  68. float total_fedTax;
  69. float total_netPay;
  70. };
  71.  
  72. // Stores min and max values for employee stats
  73. struct min_max {
  74. float min_wageRate, max_wageRate;
  75. float min_hours, max_hours;
  76. float min_overtimeHrs, max_overtimeHrs;
  77. float min_grossPay, max_grossPay;
  78. float min_stateTax, max_stateTax;
  79. float min_fedTax, max_fedTax;
  80. float min_netPay, max_netPay;
  81. };
  82.  
  83. //****************************
  84. // Function Prototypes
  85. //****************************
  86.  
  87. /*
  88.  * Function: getHours
  89.  * Purpose: Prompt user to enter hours worked for an employee
  90.  * Parameters: long int clockNumber - employee's clock number
  91.  * Returns: float - number of hours worked
  92.  */
  93. float getHours(long int clockNumber);
  94.  
  95. /*
  96.  * Function: calcOvertimeHrs
  97.  * Purpose: Calculate overtime hours based on standard hours
  98.  * Parameters: float hours - total hours worked
  99.  * Returns: float - number of overtime hours
  100.  */
  101. float calcOvertimeHrs(float hours);
  102.  
  103. /*
  104.  * Function: calcGrossPay
  105.  * Purpose: Calculate gross pay including overtime
  106.  * Parameters: float wageRate - hourly wage
  107.  * float hours - total hours worked
  108.  * float overtimeHrs - overtime hours
  109.  * Returns: float - total gross pay
  110.  */
  111. float calcGrossPay(float wageRate, float hours, float overtimeHrs);
  112.  
  113. /*
  114.  * Function: calcStateTax
  115.  * Purpose: Calculate state tax based on state code
  116.  * Parameters: float grossPay - gross pay
  117.  * char taxState[] - 2-letter state code
  118.  * Returns: float - calculated state tax
  119.  */
  120. float calcStateTax(float grossPay, char taxState[]);
  121.  
  122. /*
  123.  * Function: calcFedTax
  124.  * Purpose: Calculate federal tax on gross pay
  125.  * Parameters: float grossPay - gross pay
  126.  * Returns: float - calculated federal tax
  127.  */
  128. float calcFedTax(float grossPay);
  129.  
  130. /*
  131.  * Function: calcNetPay
  132.  * Purpose: Calculate net pay after taxes
  133.  * Parameters: float grossPay - gross pay
  134.  * float stateTax - state tax
  135.  * float fedTax - federal tax
  136.  * Returns: float - net pay
  137.  */
  138. float calcNetPay(float grossPay, float stateTax, float fedTax);
  139.  
  140. /*
  141.  * Function: printHeader
  142.  * Purpose: Print table header for employee payroll
  143.  * Parameters: None
  144.  * Returns: void
  145.  */
  146. void printHeader(void);
  147.  
  148. /*
  149.  * Function: printEmp
  150.  * Purpose: Print details of one employee
  151.  * Parameters: employee data items
  152.  * Returns: void
  153.  */
  154. void printEmp(char firstName[], char lastName[], char taxState[], long int clockNumber,
  155. float wageRate, float hours, float overtimeHrs, float grossPay,
  156. float stateTax, float fedTax, float netPay);
  157.  
  158. /*
  159.  * Function: calcEmployeeTotals
  160.  * Purpose: Update totals structure with values from one employee
  161.  * Parameters: employee data items, struct totals employeeTotals
  162.  * Returns: struct totals - updated totals
  163.  */
  164. struct totals calcEmployeeTotals(float wageRate, float hours, float overtimeHrs,
  165. float grossPay, float stateTax, float fedTax,
  166. float netPay, struct totals employeeTotals);
  167.  
  168. /*
  169.  * Function: calcEmployeeMinMax
  170.  * Purpose: Update min/max structure with values from one employee
  171.  * Parameters: employee data items, struct min_max employeeMinMax, int arrayIndex
  172.  * Returns: struct min_max - updated min/max values
  173.  */
  174. struct min_max calcEmployeeMinMax(float wageRate, float hours, float overtimeHrs,
  175. float grossPay, float stateTax, float fedTax,
  176. float netPay, struct min_max employeeMinMax,
  177. int arrayIndex);
  178.  
  179. /*
  180.  * Function: printEmpStatistics
  181.  * Purpose: Print totals, averages, min, and max statistics
  182.  * Parameters: struct totals employeeTotals, struct min_max employeeMinMax, int theSize
  183.  * Returns: void
  184.  */
  185. void printEmpStatistics(struct totals employeeTotals, struct min_max employeeMinMax, int theSize);
  186.  
  187. //****************************
  188. // Main Program
  189. //****************************
  190. int main() {
  191. int i; // Loop counter
  192.  
  193. // Initialize employee data
  194. struct employee employeeData[SIZE] = {
  195. { {"Connie", "Cobol"}, "MA", 98401, 10.60 },
  196. { {"Mary", "Apl"}, "NH", 526488, 9.75 },
  197. { {"Frank", "Fortran"}, "VT", 765349, 10.50 },
  198. { {"Jeff", "Ada"}, "NY", 34645, 12.25 },
  199. { {"Anton", "Pascal"}, "CA", 127615, 8.35 }
  200. };
  201.  
  202. struct totals employeeTotals = {0}; // Initialize totals
  203. struct min_max employeeMinMax = {0}; // Initialize min/max values
  204.  
  205. // Process each employee
  206. for (i = 0; i < SIZE; ++i) {
  207. employeeData[i].hours = getHours(employeeData[i].clockNumber); // Input hours
  208. employeeData[i].overtimeHrs = calcOvertimeHrs(employeeData[i].hours); // Calc OT
  209. employeeData[i].grossPay = calcGrossPay(employeeData[i].wageRate, employeeData[i].hours, employeeData[i].overtimeHrs); // Calc gross
  210. employeeData[i].stateTax = calcStateTax(employeeData[i].grossPay, employeeData[i].taxState); // Calc state tax
  211. employeeData[i].fedTax = calcFedTax(employeeData[i].grossPay); // Calc federal tax
  212. employeeData[i].netPay = calcNetPay(employeeData[i].grossPay, employeeData[i].stateTax, employeeData[i].fedTax); // Calc net pay
  213.  
  214. // Update totals and min/max
  215. employeeTotals = calcEmployeeTotals(employeeData[i].wageRate, employeeData[i].hours,
  216. employeeData[i].overtimeHrs, employeeData[i].grossPay,
  217. employeeData[i].stateTax, employeeData[i].fedTax,
  218. employeeData[i].netPay, employeeTotals);
  219. employeeMinMax = calcEmployeeMinMax(employeeData[i].wageRate, employeeData[i].hours,
  220. employeeData[i].overtimeHrs, employeeData[i].grossPay,
  221. employeeData[i].stateTax, employeeData[i].fedTax,
  222. employeeData[i].netPay, employeeMinMax, i);
  223. }
  224.  
  225. // Print results
  226. printHeader();
  227. for (i = 0; i < SIZE; ++i) {
  228. printEmp(employeeData[i].empName.firstName, employeeData[i].empName.lastName,
  229. employeeData[i].taxState, employeeData[i].clockNumber,
  230. employeeData[i].wageRate, employeeData[i].hours,
  231. employeeData[i].overtimeHrs, employeeData[i].grossPay,
  232. employeeData[i].stateTax, employeeData[i].fedTax, employeeData[i].netPay);
  233. }
  234.  
  235. // Print totals, averages, min/max
  236. printEmpStatistics(employeeTotals, employeeMinMax, SIZE);
  237.  
  238. return 0;
  239. }
  240.  
  241. //**************************************************************
  242. // Function Implementations
  243. //**************************************************************
  244.  
  245. float getHours(long int clockNumber) {
  246. float theHoursWorked; // Stores input hours
  247. printf("\nEnter hours worked by emp # %06li: ", clockNumber);
  248. scanf("%f", &theHoursWorked);
  249. return theHoursWorked;
  250. }
  251.  
  252. float calcOvertimeHrs(float hours) {
  253. float overtime = (hours > STD_HOURS) ? (hours - STD_HOURS) : 0; // OT calculation
  254. return overtime;
  255. }
  256.  
  257. float calcGrossPay(float wageRate, float hours, float overtimeHrs) {
  258. float normalPay = wageRate * (hours - overtimeHrs); // Regular pay
  259. float overtimePay = overtimeHrs * (OT_RATE * wageRate); // OT pay
  260. return normalPay + overtimePay; // Total gross pay
  261. }
  262.  
  263. float calcStateTax(float grossPay, char taxState[]) {
  264. float tax; // Stores calculated state tax
  265. taxState[0] = toupper(taxState[0]);
  266. taxState[1] = toupper(taxState[1]);
  267.  
  268. // Determine state tax
  269. if (strcmp(taxState, "MA") == 0) tax = grossPay * MA_TAX_RATE;
  270. else if (strcmp(taxState, "NH") == 0) tax = grossPay * NH_TAX_RATE;
  271. else if (strcmp(taxState, "VT") == 0) tax = grossPay * VT_TAX_RATE;
  272. else if (strcmp(taxState, "CA") == 0) tax = grossPay * CA_TAX_RATE;
  273. else tax = grossPay * DEFAULT_TAX_RATE;
  274.  
  275. return tax;
  276. }
  277.  
  278. float calcFedTax(float grossPay) {
  279. return grossPay * FED_TAX_RATE; // Federal tax
  280. }
  281.  
  282. float calcNetPay(float grossPay, float stateTax, float fedTax) {
  283. return grossPay - (stateTax + fedTax); // Net pay after taxes
  284. }
  285.  
  286. void printHeader(void) {
  287. printf("\n\n*** Pay Calculator ***\n");
  288. printf("\n--------------------------------------------------------------");
  289. printf("-------------------");
  290. printf("\nName Tax Clock# Wage Hours OT Gross ");
  291. printf(" State Fed Net");
  292. printf("\n State Pay ");
  293. printf(" Tax Tax Pay");
  294. printf("\n--------------------------------------------------------------");
  295. }
  296.  
  297. void printEmp(char firstName[], char lastName[], char taxState[], long int clockNumber,
  298. float wageRate, float hours, float overtimeHrs, float grossPay,
  299. float stateTax, float fedTax, float netPay) {
  300. char name[FIRST_NAME_SIZE + LAST_NAME_SIZE + 1]; // Combined full name
  301. strcpy(name, firstName);
  302. strcat(name, " ");
  303. strcat(name, lastName);
  304. printf("\n%-20.20s %-2.2s %06li %5.2f %4.1f %4.1f %7.2f %6.2f %7.2f %8.2f",
  305. name, taxState, clockNumber, wageRate, hours, overtimeHrs, grossPay, stateTax, fedTax, netPay);
  306. }
  307.  
  308. struct totals calcEmployeeTotals(float wageRate, float hours, float overtimeHrs,
  309. float grossPay, float stateTax, float fedTax,
  310. float netPay, struct totals employeeTotals) {
  311. // Add this employee's values to running totals
  312. employeeTotals.total_wageRate += wageRate;
  313. employeeTotals.total_hours += hours;
  314. employeeTotals.total_overtimeHrs += overtimeHrs;
  315. employeeTotals.total_grossPay += grossPay;
  316. employeeTotals.total_stateTax += stateTax;
  317. employeeTotals.total_fedTax += fedTax;
  318. employeeTotals.total_netPay += netPay;
  319.  
  320. return employeeTotals;
  321. }
  322.  
  323. struct min_max calcEmployeeMinMax(float wageRate, float hours, float overtimeHrs,
  324. float grossPay, float stateTax, float fedTax,
  325. float netPay, struct min_max employeeMinMax,
  326. int arrayIndex) {
  327. // Initialize min/max with first employee
  328. if (arrayIndex == 0) {
  329. employeeMinMax.min_wageRate = employeeMinMax.max_wageRate = wageRate;
  330. employeeMinMax.min_hours = employeeMinMax.max_hours = hours;
  331. employeeMinMax.min_overtimeHrs = employeeMinMax.max_overtimeHrs = overtimeHrs;
  332. employeeMinMax.min_grossPay = employeeMinMax.max_grossPay = grossPay;
  333. employeeMinMax.min_stateTax = employeeMinMax.max_stateTax = stateTax;
  334. employeeMinMax.min_fedTax = employeeMinMax.max_fedTax = fedTax;
  335. employeeMinMax.min_netPay = employeeMinMax.max_netPay = netPay;
  336. } else {
  337. // Update min/max values for each metric
  338. if (wageRate < employeeMinMax.min_wageRate) employeeMinMax.min_wageRate = wageRate;
  339. if (wageRate > employeeMinMax.max_wageRate) employeeMinMax.max_wageRate = wageRate;
  340. if (hours < employeeMinMax.min_hours) employeeMinMax.min_hours = hours;
  341. if (hours > employeeMinMax.max_hours) employeeMinMax.max_hours = hours;
  342. if (overtimeHrs < employeeMinMax.min_overtimeHrs) employeeMinMax.min_overtimeHrs = overtimeHrs;
  343. if (overtimeHrs > employeeMinMax.max_overtimeHrs) employeeMinMax.max_overtimeHrs = overtimeHrs;
  344. if (grossPay < employeeMinMax.min_grossPay) employeeMinMax.min_grossPay = grossPay;
  345. if (grossPay > employeeMinMax.max_grossPay) employeeMinMax.max_grossPay = grossPay;
  346. if (stateTax < employeeMinMax.min_stateTax) employeeMinMax.min_stateTax = stateTax;
  347. if (stateTax > employeeMinMax.max_stateTax) employeeMinMax.max_stateTax = stateTax;
  348. if (fedTax < employeeMinMax.min_fedTax) employeeMinMax.min_fedTax = fedTax;
  349. if (fedTax > employeeMinMax.max_fedTax) employeeMinMax.max_fedTax = fedTax;
  350. if (netPay < employeeMinMax.min_netPay) employeeMinMax.min_netPay = netPay;
  351. if (netPay > employeeMinMax.max_netPay) employeeMinMax.max_netPay = netPay;
  352. }
  353. return employeeMinMax;
  354. }
  355.  
  356. void printEmpStatistics(struct totals employeeTotals, struct min_max employeeMinMax, int theSize) {
  357. printf("\n--------------------------------------------------------------");
  358. printf("-------------------");
  359.  
  360. // Print totals
  361. printf("\nTotals: %5.2f %5.1f %5.1f %7.2f %6.2f %7.2f %8.2f",
  362. employeeTotals.total_wageRate, employeeTotals.total_hours, employeeTotals.total_overtimeHrs,
  363. employeeTotals.total_grossPay, employeeTotals.total_stateTax, employeeTotals.total_fedTax,
  364. employeeTotals.total_netPay);
  365.  
  366. // Print averages
  367. if (theSize > 0)
  368. printf("\nAverages: %5.2f %5.1f %5.1f %7.2f %6.2f %7.2f %8.2f",
  369. employeeTotals.total_wageRate/theSize, employeeTotals.total_hours/theSize,
  370. employeeTotals.total_overtimeHrs/theSize, employeeTotals.total_grossPay/theSize,
  371. employeeTotals.total_stateTax/theSize, employeeTotals.total_fedTax/theSize,
  372. employeeTotals.total_netPay/theSize);
  373.  
  374. // Print min values
  375. printf("\nMinimum: %5.2f %5.1f %5.1f %7.2f %6.2f %7.2f %8.2f",
  376. employeeMinMax.min_wageRate, employeeMinMax.min_hours, employeeMinMax.min_overtimeHrs,
  377. employeeMinMax.min_grossPay, employeeMinMax.min_stateTax, employeeMinMax.min_fedTax,
  378. employeeMinMax.min_netPay);
  379.  
  380. // Print max values
  381. printf("\nMaximum: %5.2f %5.1f %5.1f %7.2f %6.2f %7.2f %8.2f",
  382. employeeMinMax.max_wageRate, employeeMinMax.max_hours, employeeMinMax.max_overtimeHrs,
  383. employeeMinMax.max_grossPay, employeeMinMax.max_stateTax, employeeMinMax.max_fedTax,
  384. employeeMinMax.max_netPay);
  385. }
Success #stdin #stdout 0.01s 5296KB
stdin
51.0   
42.5
37.0
45.0
40.0
stdout
Enter hours worked by emp # 098401: 
Enter hours worked by emp # 526488: 
Enter hours worked by emp # 765349: 
Enter hours worked by emp # 034645: 
Enter hours worked by emp # 127615: 

*** Pay Calculator ***

---------------------------------------------------------------------------------
Name                Tax  Clock# Wage   Hours  OT   Gross   State  Fed      Net
                   State                           Pay     Tax    Tax      Pay
--------------------------------------------------------------
Connie Cobol         MA  098401 10.60  51.0  11.0  598.90  29.95  149.73   419.23
Mary Apl             NH  526488  9.75  42.5   2.5  426.56   0.00  106.64   319.92
Frank Fortran        VT  765349 10.50  37.0   0.0  388.50  23.31   97.12   268.07
Jeff Ada             NY  034645 12.25  45.0   5.0  581.88  46.55  145.47   389.86
Anton Pascal         CA  127615  8.35  40.0   0.0  334.00  23.38   83.50   227.12
---------------------------------------------------------------------------------
Totals:                         51.45 215.5  18.5 2329.84 123.18  582.46  1624.19
Averages:                       10.29  43.1   3.7  465.97  24.64  116.49   324.84
Minimum:                          8.35  37.0   0.0  334.00   0.00   83.50   227.12
Maximum:                         12.25  51.0  11.0  598.90  46.55  149.73   419.23