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. // Uses structures for employee info, totals, and min/max statistics.
  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 to process
  25. #define STD_HOURS 40.0 // Standard hours before overtime
  26. #define OT_RATE 1.5 // Overtime multiplier
  27. #define MA_TAX_RATE 0.05 // State tax rates
  28. #define NH_TAX_RATE 0.0
  29. #define VT_TAX_RATE 0.06
  30. #define CA_TAX_RATE 0.07
  31. #define DEFAULT_TAX_RATE 0.08
  32. #define FED_TAX_RATE 0.25
  33. #define NAME_SIZE 20
  34. #define TAX_STATE_SIZE 3
  35. #define FIRST_NAME_SIZE 10
  36. #define LAST_NAME_SIZE 10
  37.  
  38. //*******************************************************
  39. // Structures
  40. //*******************************************************
  41.  
  42. // Stores first and last names for an employee
  43. struct name {
  44. char firstName[FIRST_NAME_SIZE];
  45. char lastName[LAST_NAME_SIZE];
  46. };
  47.  
  48. // Employee structure containing name, clock number, hours, pay, taxes
  49. struct employee {
  50. struct name empName; // Employee's first and last name
  51. char taxState[TAX_STATE_SIZE]; // Employee's tax state
  52. long int clockNumber; // Unique employee ID
  53. float wageRate; // Hourly wage
  54. float hours; // Hours worked
  55. float overtimeHrs; // Calculated overtime hours
  56. float grossPay; // Gross pay including overtime
  57. float stateTax; // State tax amount
  58. float fedTax; // Federal tax amount
  59. float netPay; // Net pay after taxes
  60. };
  61.  
  62. // Totals structure to track cumulative sums for all employees
  63. struct totals {
  64. float total_wageRate;
  65. float total_hours;
  66. float total_overtimeHrs;
  67. float total_grossPay;
  68. float total_stateTax;
  69. float total_fedTax;
  70. float total_netPay;
  71. };
  72.  
  73. // Min/Max structure to track min/max values for all employees
  74. struct min_max {
  75. float min_wageRate;
  76. float min_hours;
  77. float min_overtimeHrs;
  78. float min_grossPay;
  79. float min_stateTax;
  80. float min_fedTax;
  81. float min_netPay;
  82. float max_wageRate;
  83. float max_hours;
  84. float max_overtimeHrs;
  85. float max_grossPay;
  86. float max_stateTax;
  87. float max_fedTax;
  88. float max_netPay;
  89. };
  90.  
  91. //*******************************************************
  92. // Function Prototypes
  93. //*******************************************************
  94.  
  95. float getHours(long int clockNumber); // Prompt user for hours worked
  96. float calcOvertimeHrs(float hours); // Calculate overtime hours
  97. float calcGrossPay(float wageRate, float hours, float overtimeHrs); // Calculate gross pay
  98. void printHeader(void); // Print formatted table header
  99. void printEmp(char firstName[], char lastName[], char taxState[], long int clockNumber,
  100. float wageRate, float hours, float overtimeHrs, float grossPay,
  101. float stateTax, float fedTax, float netPay); // Print employee info
  102. float calcStateTax(float grossPay, char taxState[]); // Determine state tax
  103. float calcFedTax(float grossPay); // Determine federal tax
  104. float calcNetPay(float grossPay, float stateTax, float fedTax); // Calculate net pay
  105. struct totals calcEmployeeTotals(float wageRate, float hours, float overtimeHrs,
  106. float grossPay, float stateTax, float fedTax,
  107. float netPay, struct totals employeeTotals); // Update totals
  108. struct min_max calcEmployeeMinMax(float wageRate, float hours, float overtimeHrs,
  109. float grossPay, float stateTax, float fedTax,
  110. float netPay, struct min_max employeeMinMax,
  111. int arrayIndex); // Update min/max values
  112. void printEmpStatistics(struct totals employeeTotals, struct min_max employeeMinMax, int theSize); // Print totals, averages, min/max
  113.  
  114. //*******************************************************
  115. // Main Function
  116. //*******************************************************
  117. int main() {
  118. int i;
  119.  
  120. // Hardcoded employee data (first/last name, tax state, clock number, wage)
  121. struct employee employeeData[SIZE] = {
  122. { {"Connie", "Cobol"}, "MA", 98401, 10.60 },
  123. { {"Mary", "Apl"}, "NH", 526488, 9.75 },
  124. { {"Frank", "Fortran"}, "VT", 765349, 10.50 },
  125. { {"Jeff", "Ada"}, "NY", 34645, 12.25 },
  126. { {"Anton", "Pascal"}, "CA", 127615, 8.35 }
  127. };
  128.  
  129. struct totals employeeTotals = {0}; // Initialize totals
  130. struct min_max employeeMinMax = {0}; // Initialize min/max
  131.  
  132. //*******************************************************
  133. // Process each employee
  134. //*******************************************************
  135. for (i = 0; i < SIZE; ++i) {
  136. // Get hours worked
  137. employeeData[i].hours = getHours(employeeData[i].clockNumber);
  138.  
  139. // Calculate overtime hours
  140. employeeData[i].overtimeHrs = calcOvertimeHrs(employeeData[i].hours);
  141.  
  142. // Calculate gross pay
  143. employeeData[i].grossPay = calcGrossPay(employeeData[i].wageRate,
  144. employeeData[i].hours,
  145. employeeData[i].overtimeHrs);
  146.  
  147. // Calculate taxes
  148. employeeData[i].stateTax = calcStateTax(employeeData[i].grossPay,
  149. employeeData[i].taxState);
  150. employeeData[i].fedTax = calcFedTax(employeeData[i].grossPay);
  151.  
  152. // Calculate net pay
  153. employeeData[i].netPay = calcNetPay(employeeData[i].grossPay,
  154. employeeData[i].stateTax,
  155. employeeData[i].fedTax);
  156.  
  157. // Update totals
  158. employeeTotals = calcEmployeeTotals(employeeData[i].wageRate,
  159. employeeData[i].hours,
  160. employeeData[i].overtimeHrs,
  161. employeeData[i].grossPay,
  162. employeeData[i].stateTax,
  163. employeeData[i].fedTax,
  164. employeeData[i].netPay,
  165. employeeTotals);
  166.  
  167. // Update min/max values
  168. employeeMinMax = calcEmployeeMinMax(employeeData[i].wageRate,
  169. employeeData[i].hours,
  170. employeeData[i].overtimeHrs,
  171. employeeData[i].grossPay,
  172. employeeData[i].stateTax,
  173. employeeData[i].fedTax,
  174. employeeData[i].netPay,
  175. employeeMinMax, i);
  176. }
  177.  
  178. //*******************************************************
  179. // Print results
  180. //*******************************************************
  181. printHeader();
  182. for (i = 0; i < SIZE; ++i) {
  183. printEmp(employeeData[i].empName.firstName,
  184. employeeData[i].empName.lastName,
  185. employeeData[i].taxState,
  186. employeeData[i].clockNumber,
  187. employeeData[i].wageRate,
  188. employeeData[i].hours,
  189. employeeData[i].overtimeHrs,
  190. employeeData[i].grossPay,
  191. employeeData[i].stateTax,
  192. employeeData[i].fedTax,
  193. employeeData[i].netPay);
  194. }
  195.  
  196. // Print totals, averages, min/max
  197. printEmpStatistics(employeeTotals, employeeMinMax, SIZE);
  198.  
  199. return 0;
  200. }
  201.  
  202. //**************************************************************
  203. // Function Definitions
  204. //**************************************************************
  205.  
  206. // Prompt user to enter hours worked
  207. float getHours(long int clockNumber) {
  208. float theHoursWorked;
  209. printf("\nEnter hours worked by emp # %06li: ", clockNumber);
  210. scanf("%f", &theHoursWorked);
  211. return theHoursWorked;
  212. }
  213.  
  214. // Print table header
  215. void printHeader(void) {
  216. printf("\n\n*** Pay Calculator ***\n");
  217. printf("\n--------------------------------------------------------------");
  218. printf("-------------------");
  219. printf("\nName Tax Clock# Wage Hours OT Gross ");
  220. printf(" State Fed Net");
  221. printf("\n State Pay ");
  222. printf(" Tax Tax Pay");
  223. printf("\n--------------------------------------------------------------");
  224. printf("-------------------");
  225. }
  226.  
  227. // Print individual employee info in formatted table
  228. void printEmp(char firstName[], char lastName[], char taxState[], long int clockNumber,
  229. float wageRate, float hours, float overtimeHrs, float grossPay,
  230. float stateTax, float fedTax, float netPay) {
  231. char name[FIRST_NAME_SIZE + LAST_NAME_SIZE + 1];
  232. strcpy(name, firstName);
  233. strcat(name, " ");
  234. strcat(name, lastName);
  235.  
  236. printf("\n%-20.20s %-2.2s %06li %5.2f %4.1f %4.1f %7.2f %6.2f %7.2f %8.2f",
  237. name, taxState, clockNumber, wageRate, hours, overtimeHrs, grossPay, stateTax, fedTax, netPay);
  238. }
  239.  
  240. // Calculate overtime hours
  241. float calcOvertimeHrs(float hours) {
  242. return (hours > STD_HOURS) ? (hours - STD_HOURS) : 0;
  243. }
  244.  
  245. // Calculate gross pay including overtime
  246. float calcGrossPay(float wageRate, float hours, float overtimeHrs) {
  247. float normalPay = wageRate * (hours - overtimeHrs);
  248. float overtimePay = overtimeHrs * (OT_RATE * wageRate);
  249. return normalPay + overtimePay;
  250. }
  251.  
  252. // Calculate state tax based on employee's tax state
  253. float calcStateTax(float grossPay, char taxState[]) {
  254. float tax;
  255. taxState[0] = toupper(taxState[0]);
  256. taxState[1] = toupper(taxState[1]);
  257. if (strcmp(taxState, "MA") == 0) tax = grossPay * MA_TAX_RATE;
  258. else if (strcmp(taxState, "NH") == 0) tax = grossPay * NH_TAX_RATE;
  259. else if (strcmp(taxState, "VT") == 0) tax = grossPay * VT_TAX_RATE;
  260. else if (strcmp(taxState, "CA") == 0) tax = grossPay * CA_TAX_RATE;
  261. else tax = grossPay * DEFAULT_TAX_RATE;
  262. return tax;
  263. }
  264.  
  265. // Calculate federal tax
  266. float calcFedTax(float grossPay) {
  267. return grossPay * FED_TAX_RATE;
  268. }
  269.  
  270. // Calculate net pay after all taxes
  271. float calcNetPay(float grossPay, float stateTax, float fedTax) {
  272. return grossPay - (stateTax + fedTax);
  273. }
  274.  
  275. // Update totals for all employees
  276. struct totals calcEmployeeTotals(float wageRate, float hours, float overtimeHrs,
  277. float grossPay, float stateTax, float fedTax,
  278. float netPay, struct totals employeeTotals) {
  279. employeeTotals.total_wageRate += wageRate;
  280. employeeTotals.total_hours += hours;
  281. employeeTotals.total_overtimeHrs += overtimeHrs;
  282. employeeTotals.total_grossPay += grossPay;
  283. employeeTotals.total_stateTax += stateTax;
  284. employeeTotals.total_fedTax += fedTax;
  285. employeeTotals.total_netPay += netPay;
  286. return employeeTotals;
  287. }
  288.  
  289. // Update min/max values for all employees
  290. struct min_max calcEmployeeMinMax(float wageRate, float hours, float overtimeHrs,
  291. float grossPay, float stateTax, float fedTax,
  292. float netPay, struct min_max employeeMinMax,
  293. int arrayIndex) {
  294. if (arrayIndex == 0) { // First employee initializes min/max
  295. employeeMinMax.min_wageRate = employeeMinMax.max_wageRate = wageRate;
  296. employeeMinMax.min_hours = employeeMinMax.max_hours = hours;
  297. employeeMinMax.min_overtimeHrs = employeeMinMax.max_overtimeHrs = overtimeHrs;
  298. employeeMinMax.min_grossPay = employeeMinMax.max_grossPay = grossPay;
  299. employeeMinMax.min_stateTax = employeeMinMax.max_stateTax = stateTax;
  300. employeeMinMax.min_fedTax = employeeMinMax.max_fedTax = fedTax;
  301. employeeMinMax.min_netPay = employeeMinMax.max_netPay = netPay;
  302. } else { // Compare and update min/max
  303. if (wageRate < employeeMinMax.min_wageRate) employeeMinMax.min_wageRate = wageRate;
  304. if (wageRate > employeeMinMax.max_wageRate) employeeMinMax.max_wageRate = wageRate;
  305. if (hours < employeeMinMax.min_hours) employeeMinMax.min_hours = hours;
  306. if (hours > employeeMinMax.max_hours) employeeMinMax.max_hours = hours;
  307. if (overtimeHrs < employeeMinMax.min_overtimeHrs) employeeMinMax.min_overtimeHrs = overtimeHrs;
  308. if (overtimeHrs > employeeMinMax.max_overtimeHrs) employeeMinMax.max_overtimeHrs = overtimeHrs;
  309. if (grossPay < employeeMinMax.min_grossPay) employeeMinMax.min_grossPay = grossPay;
  310. if (grossPay > employeeMinMax.max_grossPay) employeeMinMax.max_grossPay = grossPay;
  311. if (stateTax < employeeMinMax.min_stateTax) employeeMinMax.min_stateTax = stateTax;
  312. if (stateTax > employeeMinMax.max_stateTax) employeeMinMax.max_stateTax = stateTax;
  313. if (fedTax < employeeMinMax.min_fedTax) employeeMinMax.min_fedTax = fedTax;
  314. if (fedTax > employeeMinMax.max_fedTax) employeeMinMax.max_fedTax = fedTax;
  315. if (netPay < employeeMinMax.min_netPay) employeeMinMax.min_netPay = netPay;
  316. if (netPay > employeeMinMax.max_netPay) employeeMinMax.max_netPay = netPay;
  317. }
  318. return employeeMinMax;
  319. }
  320.  
  321. // Print totals, averages, min/max statistics
  322. void printEmpStatistics(struct totals employeeTotals, struct min_max employeeMinMax, int theSize) {
  323. printf("\n--------------------------------------------------------------");
  324. printf("-------------------");
  325. printf("\nTotals: %5.2f %5.1f %5.1f %7.2f %6.2f %7.2f %8.2f",
  326. employeeTotals.total_wageRate, employeeTotals.total_hours, employeeTotals.total_overtimeHrs,
  327. employeeTotals.total_grossPay, employeeTotals.total_stateTax, employeeTotals.total_fedTax,
  328. employeeTotals.total_netPay);
  329. if (theSize > 0)
  330. printf("\nAverages: %5.2f %5.1f %5.1f %7.2f %6.2f %7.2f %8.2f",
  331. employeeTotals.total_wageRate/theSize, employeeTotals.total_hours/theSize,
  332. employeeTotals.total_overtimeHrs/theSize, employeeTotals.total_grossPay/theSize,
  333. employeeTotals.total_stateTax/theSize, employeeTotals.total_fedTax/theSize,
  334. employeeTotals.total_netPay/theSize);
  335. printf("\nMinimum: %5.2f %5.1f %5.1f %7.2f %6.2f %7.2f %8.2f",
  336. employeeMinMax.min_wageRate, employeeMinMax.min_hours, employeeMinMax.min_overtimeHrs,
  337. employeeMinMax.min_grossPay, employeeMinMax.min_stateTax, employeeMinMax.min_fedTax,
  338. employeeMinMax.min_netPay);
  339. printf("\nMaximum: %5.2f %5.1f %5.1f %7.2f %6.2f %7.2f %8.2f",
  340. employeeMinMax.max_wageRate, employeeMinMax.max_hours, employeeMinMax.max_overtimeHrs,
  341. employeeMinMax.max_grossPay, employeeMinMax.max_stateTax, employeeMinMax.max_fedTax,
  342. employeeMinMax.max_netPay);
  343. }
  344.  
Success #stdin #stdout 0s 5316KB
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