fork download
  1. //********************************************************
  2. //
  3. // Assignment 7 - Employee Pay Calculator with Character Strings
  4. //
  5. // Name: Felix Henriquez
  6. //
  7. // Class: C Programming, Fall 2025
  8. //
  9. // Date: November 2, 2025
  10. //
  11. // Description: Extended employee pay calculator using structures,
  12. // character strings, and tax calculations to determine
  13. // state tax, federal tax, and net pay with summary stats.
  14. //
  15. //********************************************************
  16.  
  17. #include <stdio.h>
  18. #include <string.h>
  19.  
  20. // Constants for array sizes and business rules
  21. #define MAX_EMPLOYEES 5
  22. #define FIRST_NAME_SIZE 11 // 10 characters + null terminator
  23. #define LAST_NAME_SIZE 11 // 10 characters + null terminator
  24. #define TAX_STATE_SIZE 3 // 2 characters + null terminator
  25. #define STANDARD_WORK_WEEK 40.0
  26. #define OVERTIME_RATE 1.5
  27. #define FED_TAX_RATE 0.25
  28.  
  29. // Structure for employee name
  30. struct name
  31. {
  32. char firstName[FIRST_NAME_SIZE];
  33. char lastName[LAST_NAME_SIZE];
  34. };
  35.  
  36. // Structure for employee data
  37. struct employee
  38. {
  39. struct name empName;
  40. char taxState[TAX_STATE_SIZE];
  41. long int clockNumber;
  42. float wageRate;
  43. float hours;
  44. float overtimeHrs;
  45. float grossPay;
  46. float stateTax;
  47. float fedTax;
  48. float netPay;
  49. };
  50.  
  51. // Structure for summary statistics
  52. struct summaryStats
  53. {
  54. float totalWage; // Sum of all wage rates
  55. float totalHours; // Sum of all hours worked
  56. float totalOvertimeHrs; // Sum of all overtime hours
  57. float totalGrossPay; // Sum of all gross pay amounts
  58. float totalStateTax; // Sum of all state tax amounts
  59. float totalFedTax; // Sum of all federal tax amounts
  60. float totalNetPay; // Sum of all net pay amounts
  61.  
  62. float avgWage; // Average wage rate across employees
  63. float avgHours; // Average hours worked
  64. float avgOvertimeHrs; // Average overtime hours
  65. float avgGrossPay; // Average gross pay
  66. float avgStateTax; // Average state tax
  67. float avgFedTax; // Average federal tax
  68. float avgNetPay; // Average net pay
  69.  
  70. float minWage; // Minimum wage rate
  71. float minHours; // Minimum hours worked
  72. float minOvertimeHrs; // Minimum overtime hours
  73. float minGrossPay; // Minimum gross pay
  74. float minStateTax; // Minimum state tax
  75. float minFedTax; // Minimum federal tax
  76. float minNetPay; // Minimum net pay
  77.  
  78. float maxWage; // Maximum wage rate
  79. float maxHours; // Maximum hours worked
  80. float maxOvertimeHrs; // Maximum overtime hours
  81. float maxGrossPay; // Maximum gross pay
  82. float maxStateTax; // Maximum state tax
  83. float maxFedTax; // Maximum federal tax
  84. float maxNetPay; // Maximum net pay
  85. };
  86.  
  87. /*****************************************************************************
  88.  * Function: findMin
  89.  *
  90.  * Purpose: Returns the minimum of two float values
  91.  *
  92.  * Parameters: a - first value to compare
  93.  * b - second value to compare
  94.  *
  95.  * Returns: The smaller of the two values
  96.  *****************************************************************************/
  97. float findMin(float a, float b) {
  98. return (a < b) ? a : b;
  99. }
  100.  
  101. /*****************************************************************************
  102.  * Function: findMax
  103.  *
  104.  * Purpose: Returns the maximum of two float values
  105.  *
  106.  * Parameters: a - first value to compare
  107.  * b - second value to compare
  108.  *
  109.  * Returns: The larger of the two values
  110.  *****************************************************************************/
  111. float findMax(float a, float b) {
  112. return (a > b) ? a : b;
  113. }
  114.  
  115. /*****************************************************************************
  116.  * Function: getStateTaxRate
  117.  *
  118.  * Purpose: Determines the state tax rate based on the state code
  119.  *
  120.  * Parameters: state - two-character state code string
  121.  *
  122.  * Returns: State tax rate as a float percentage
  123.  *****************************************************************************/
  124. float getStateTaxRate(const char state[]) {
  125. if (strcmp(state, "MA") == 0) return 0.05; // Massachusetts: 5%
  126. if (strcmp(state, "NH") == 0) return 0.00; // New Hampshire: 0%
  127. if (strcmp(state, "VT") == 0) return 0.06; // Vermont: 6%
  128. if (strcmp(state, "CA") == 0) return 0.07; // California: 7%
  129. return 0.08; // All other states: 8%
  130. }
  131.  
  132. /*****************************************************************************
  133.  * Function: initializeEmployees
  134.  *
  135.  * Purpose: Initializes the employee array with predefined data
  136.  *
  137.  * Parameters: employees - array of employee structures to initialize
  138.  *
  139.  * Returns: void
  140.  *****************************************************************************/
  141. void initializeEmployees(struct employee employees[]) {
  142. int i; // loop counter
  143.  
  144. // Initialize employee data with predefined values
  145. strcpy(employees[0].empName.firstName, "Connie");
  146. strcpy(employees[0].empName.lastName, "Cobol");
  147. strcpy(employees[0].taxState, "MA");
  148. employees[0].clockNumber = 98401;
  149. employees[0].wageRate = 10.60;
  150.  
  151. strcpy(employees[1].empName.firstName, "Mary");
  152. strcpy(employees[1].empName.lastName, "Apl");
  153. strcpy(employees[1].taxState, "NH");
  154. employees[1].clockNumber = 526488;
  155. employees[1].wageRate = 9.75;
  156.  
  157. strcpy(employees[2].empName.firstName, "Frank");
  158. strcpy(employees[2].empName.lastName, "Fortran");
  159. strcpy(employees[2].taxState, "VT");
  160. employees[2].clockNumber = 765349;
  161. employees[2].wageRate = 10.50;
  162.  
  163. strcpy(employees[3].empName.firstName, "Jeff");
  164. strcpy(employees[3].empName.lastName, "Ada");
  165. strcpy(employees[3].taxState, "NY");
  166. employees[3].clockNumber = 34645;
  167. employees[3].wageRate = 12.25;
  168.  
  169. strcpy(employees[4].empName.firstName, "Anton");
  170. strcpy(employees[4].empName.lastName, "Pascal");
  171. strcpy(employees[4].taxState, "CA");
  172. employees[4].clockNumber = 127615;
  173. employees[4].wageRate = 8.35; // Corrected from 10.00 to 8.35
  174.  
  175. // Initialize calculated fields to zero
  176. for(i = 0; i < MAX_EMPLOYEES; i++) {
  177. employees[i].hours = 0;
  178. employees[i].overtimeHrs = 0;
  179. employees[i].grossPay = 0;
  180. employees[i].stateTax = 0;
  181. employees[i].fedTax = 0;
  182. employees[i].netPay = 0;
  183. }
  184. }
  185.  
  186. /*****************************************************************************
  187.  * Function: getEmployeeHours
  188.  *
  189.  * Purpose: Reads hours worked for each employee from standard input
  190.  *
  191.  * Parameters: employees - array of employee structures
  192.  *
  193.  * Returns: void
  194.  *****************************************************************************/
  195. void getEmployeeHours(struct employee employees[]) {
  196. int i; // loop counter
  197.  
  198. // Read hours for each employee
  199. for(i = 0; i < MAX_EMPLOYEES; i++) {
  200. scanf("%f", &employees[i].hours);
  201. }
  202. }
  203.  
  204. /*****************************************************************************
  205.  * Function: computeOvertime
  206.  *
  207.  * Purpose: Calculates overtime hours for each employee
  208.  *
  209.  * Parameters: employees - array of employee structures
  210.  *
  211.  * Returns: void
  212.  *****************************************************************************/
  213. void computeOvertime(struct employee employees[]) {
  214. int i; // loop counter
  215.  
  216. for(i = 0; i < MAX_EMPLOYEES; i++) {
  217. if(employees[i].hours > STANDARD_WORK_WEEK) {
  218. employees[i].overtimeHrs = employees[i].hours - STANDARD_WORK_WEEK; // Calculate overtime
  219. } else {
  220. employees[i].overtimeHrs = 0.0; // No overtime
  221. }
  222. }
  223. }
  224.  
  225. /*****************************************************************************
  226.  * Function: computeGrossPay
  227.  *
  228.  * Purpose: Calculates gross pay including overtime at time-and-a-half
  229.  *
  230.  * Parameters: employees - array of employee structures
  231.  *
  232.  * Returns: void
  233.  *****************************************************************************/
  234. void computeGrossPay(struct employee employees[]) {
  235. int i; // loop counter
  236. float regularHours; // hours up to 40
  237. float basePay; // pay for regular hours
  238. float overtimePay; // pay for overtime hours
  239.  
  240. for(i = 0; i < MAX_EMPLOYEES; i++) {
  241. regularHours = employees[i].hours;
  242. if(regularHours > STANDARD_WORK_WEEK) {
  243. regularHours = STANDARD_WORK_WEEK; // Cap at 40 hours
  244. }
  245.  
  246. basePay = regularHours * employees[i].wageRate; // Regular pay
  247. overtimePay = employees[i].overtimeHrs * employees[i].wageRate * OVERTIME_RATE; // Overtime pay
  248. employees[i].grossPay = basePay + overtimePay; // Total gross pay
  249. }
  250. }
  251.  
  252. /*****************************************************************************
  253.  * Function: computeTaxesAndNetPay
  254.  *
  255.  * Purpose: Calculates state tax, federal tax, and net pay for each employee
  256.  *
  257.  * Parameters: employees - array of employee structures
  258.  *
  259.  * Returns: void
  260.  *****************************************************************************/
  261. void computeTaxesAndNetPay(struct employee employees[]) {
  262. int i; // loop counter
  263. float stateRate; // state-specific tax rate
  264.  
  265. for(i = 0; i < MAX_EMPLOYEES; i++) {
  266. stateRate = getStateTaxRate(employees[i].taxState); // Get state rate
  267. employees[i].stateTax = employees[i].grossPay * stateRate; // Calculate state tax
  268. employees[i].fedTax = employees[i].grossPay * FED_TAX_RATE; // Calculate federal tax
  269. employees[i].netPay = employees[i].grossPay - employees[i].stateTax - employees[i].fedTax; // Calculate net pay
  270. }
  271. }
  272.  
  273. /*****************************************************************************
  274.  * Function: calculateSummaryStats
  275.  *
  276.  * Purpose: Calculates comprehensive summary statistics for all employees
  277.  *
  278.  * Parameters: employees - array of employee structures
  279.  * stats - pointer to summaryStats structure to store results
  280.  *
  281.  * Returns: void
  282.  *****************************************************************************/
  283. void calculateSummaryStats(struct employee employees[], struct summaryStats *stats) {
  284. int i; // loop counter
  285.  
  286. // Initialize totals to zero
  287. stats->totalWage = 0;
  288. stats->totalHours = 0;
  289. stats->totalOvertimeHrs = 0;
  290. stats->totalGrossPay = 0;
  291. stats->totalStateTax = 0;
  292. stats->totalFedTax = 0;
  293. stats->totalNetPay = 0;
  294.  
  295. // Initialize min/max with first employee's data
  296. stats->minWage = employees[0].wageRate;
  297. stats->minHours = employees[0].hours;
  298. stats->minOvertimeHrs = employees[0].overtimeHrs;
  299. stats->minGrossPay = employees[0].grossPay;
  300. stats->minStateTax = employees[0].stateTax;
  301. stats->minFedTax = employees[0].fedTax;
  302. stats->minNetPay = employees[0].netPay;
  303.  
  304. stats->maxWage = employees[0].wageRate;
  305. stats->maxHours = employees[0].hours;
  306. stats->maxOvertimeHrs = employees[0].overtimeHrs;
  307. stats->maxGrossPay = employees[0].grossPay;
  308. stats->maxStateTax = employees[0].stateTax;
  309. stats->maxFedTax = employees[0].fedTax;
  310. stats->maxNetPay = employees[0].netPay;
  311.  
  312. // Process all employees
  313. for(i = 0; i < MAX_EMPLOYEES; i++) {
  314. // Calculate totals
  315. stats->totalWage += employees[i].wageRate;
  316. stats->totalHours += employees[i].hours;
  317. stats->totalOvertimeHrs += employees[i].overtimeHrs;
  318. stats->totalGrossPay += employees[i].grossPay;
  319. stats->totalStateTax += employees[i].stateTax;
  320. stats->totalFedTax += employees[i].fedTax;
  321. stats->totalNetPay += employees[i].netPay;
  322.  
  323. // Find minimum values
  324. stats->minWage = findMin(stats->minWage, employees[i].wageRate);
  325. stats->minHours = findMin(stats->minHours, employees[i].hours);
  326. stats->minOvertimeHrs = findMin(stats->minOvertimeHrs, employees[i].overtimeHrs);
  327. stats->minGrossPay = findMin(stats->minGrossPay, employees[i].grossPay);
  328. stats->minStateTax = findMin(stats->minStateTax, employees[i].stateTax);
  329. stats->minFedTax = findMin(stats->minFedTax, employees[i].fedTax);
  330. stats->minNetPay = findMin(stats->minNetPay, employees[i].netPay);
  331.  
  332. // Find maximum values
  333. stats->maxWage = findMax(stats->maxWage, employees[i].wageRate);
  334. stats->maxHours = findMax(stats->maxHours, employees[i].hours);
  335. stats->maxOvertimeHrs = findMax(stats->maxOvertimeHrs, employees[i].overtimeHrs);
  336. stats->maxGrossPay = findMax(stats->maxGrossPay, employees[i].grossPay);
  337. stats->maxStateTax = findMax(stats->maxStateTax, employees[i].stateTax);
  338. stats->maxFedTax = findMax(stats->maxFedTax, employees[i].fedTax);
  339. stats->maxNetPay = findMax(stats->maxNetPay, employees[i].netPay);
  340. }
  341.  
  342. // Calculate averages
  343. stats->avgWage = stats->totalWage / MAX_EMPLOYEES;
  344. stats->avgHours = stats->totalHours / MAX_EMPLOYEES;
  345. stats->avgOvertimeHrs = stats->totalOvertimeHrs / MAX_EMPLOYEES;
  346. stats->avgGrossPay = stats->totalGrossPay / MAX_EMPLOYEES;
  347. stats->avgStateTax = stats->totalStateTax / MAX_EMPLOYEES;
  348. stats->avgFedTax = stats->totalFedTax / MAX_EMPLOYEES;
  349. stats->avgNetPay = stats->totalNetPay / MAX_EMPLOYEES;
  350. }
  351.  
  352. /*****************************************************************************
  353.  * Function: displayReport
  354.  *
  355.  * Purpose: Displays the complete payroll report with summary statistics
  356.  *
  357.  * Parameters: employees - array of employee structures
  358.  *
  359.  * Returns: void
  360.  *****************************************************************************/
  361. void displayReport(struct employee employees[]) {
  362. struct summaryStats stats; // structure for summary statistics
  363. int i; // loop counter
  364. char fullName[25]; // buffer for full name
  365.  
  366. calculateSummaryStats(employees, &stats); // Calculate statistics
  367.  
  368. // Display report header
  369. printf("\n*** Pay Calculator ***\n");
  370. printf("---------------------------------------------------------------------------------\n");
  371. printf("Name Tax Clock# Wage Hours OT Gross State Fed Net\n");
  372. printf(" State Pay Tax Tax Pay\n");
  373. printf("---------------------------------------------------------------------------------\n");
  374.  
  375. // Display employee data
  376. for(i = 0; i < MAX_EMPLOYEES; i++) {
  377. sprintf(fullName, "%s %s", employees[i].empName.firstName, employees[i].empName.lastName); // Create full name
  378.  
  379. printf("%-18s %-3s %06ld %5.2f %5.1f %4.1f %7.2f %5.2f %6.2f %7.2f\n",
  380. fullName,
  381. employees[i].taxState,
  382. employees[i].clockNumber,
  383. employees[i].wageRate,
  384. employees[i].hours,
  385. employees[i].overtimeHrs,
  386. employees[i].grossPay,
  387. employees[i].stateTax,
  388. employees[i].fedTax,
  389. employees[i].netPay);
  390. }
  391.  
  392. // Display summary statistics
  393. printf("---------------------------------------------------------------------------------\n");
  394. printf("Totals: %5.2f %5.1f %4.1f %7.2f %5.2f %6.2f %7.2f\n",
  395. stats.totalWage, stats.totalHours, stats.totalOvertimeHrs,
  396. stats.totalGrossPay, stats.totalStateTax, stats.totalFedTax, stats.totalNetPay);
  397. printf("Averages: %5.2f %5.1f %4.1f %7.2f %5.2f %6.2f %7.2f\n",
  398. stats.avgWage, stats.avgHours, stats.avgOvertimeHrs,
  399. stats.avgGrossPay, stats.avgStateTax, stats.avgFedTax, stats.avgNetPay);
  400. printf("Minimum: %5.2f %5.1f %4.1f %7.2f %5.2f %6.2f %7.2f\n",
  401. stats.minWage, stats.minHours, stats.minOvertimeHrs,
  402. stats.minGrossPay, stats.minStateTax, stats.minFedTax, stats.minNetPay);
  403. printf("Maximum: %5.2f %5.1f %4.1f %7.2f %5.2f %6.2f %7.2f\n",
  404. stats.maxWage, stats.maxHours, stats.maxOvertimeHrs,
  405. stats.maxGrossPay, stats.maxStateTax, stats.maxFedTax, stats.maxNetPay);
  406. }
  407.  
  408. /*****************************************************************************
  409.  * Function: main
  410.  *
  411.  * Purpose: Program entry point - orchestrates the payroll calculation process
  412.  *
  413.  * Parameters: none
  414.  *
  415.  * Returns: int - program exit status
  416.  *****************************************************************************/
  417. int main() {
  418. struct employee employees[MAX_EMPLOYEES]; // employee data array
  419.  
  420. // Execute payroll processing steps
  421. initializeEmployees(employees); // Initialize employee data
  422. getEmployeeHours(employees); // Read hours worked
  423. computeOvertime(employees); // Calculate overtime
  424. computeGrossPay(employees); // Calculate gross pay
  425. computeTaxesAndNetPay(employees); // Calculate taxes and net pay
  426. displayReport(employees); // Display final report
  427.  
  428. return 0; // Program success
  429. }
Success #stdin #stdout 0.01s 5272KB
stdin
51.0
42.5
37.0
45.0
40.0
stdout
*** 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