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;
  55. float totalHours;
  56. float totalOvertimeHrs;
  57. float totalGrossPay;
  58. float totalStateTax;
  59. float totalFedTax;
  60. float totalNetPay;
  61.  
  62. float avgWage;
  63. float avgHours;
  64. float avgOvertimeHrs;
  65. float avgGrossPay;
  66. float avgStateTax;
  67. float avgFedTax;
  68. float avgNetPay;
  69.  
  70. float minWage;
  71. float minHours;
  72. float minOvertimeHrs;
  73. float minGrossPay;
  74. float minStateTax;
  75. float minFedTax;
  76. float minNetPay;
  77.  
  78. float maxWage;
  79. float maxHours;
  80. float maxOvertimeHrs;
  81. float maxGrossPay;
  82. float maxStateTax;
  83. float maxFedTax;
  84. float maxNetPay;
  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. // Compare state code and return appropriate tax rate
  126. if (strcmp(state, "MA") == 0) return 0.05; // Massachusetts: 5%
  127. if (strcmp(state, "NH") == 0) return 0.00; // New Hampshire: 0%
  128. if (strcmp(state, "VT") == 0) return 0.06; // Vermont: 6%
  129. if (strcmp(state, "CA") == 0) return 0.07; // California: 7%
  130. return 0.08; // All other states: 8%
  131. }
  132.  
  133. /*****************************************************************************
  134.  * Function: initializeEmployees
  135.  *
  136.  * Purpose: Initializes the employee array with predefined data
  137.  *
  138.  * Parameters: employees - array of employee structures to initialize
  139.  *
  140.  * Returns: void
  141.  *****************************************************************************/
  142. void initializeEmployees(struct employee employees[]) {
  143. int i; // loop counter for initializing employee array
  144.  
  145. // Employee 1: Connie Cobol
  146. strcpy(employees[0].empName.firstName, "Connie");
  147. strcpy(employees[0].empName.lastName, "Cobol");
  148. strcpy(employees[0].taxState, "MA");
  149. employees[0].clockNumber = 98401;
  150. employees[0].wageRate = 10.60;
  151.  
  152. // Employee 2: Mary Apl
  153. strcpy(employees[1].empName.firstName, "Mary");
  154. strcpy(employees[1].empName.lastName, "Apl");
  155. strcpy(employees[1].taxState, "NH");
  156. employees[1].clockNumber = 526488;
  157. employees[1].wageRate = 9.75;
  158.  
  159. // Employee 3: Frank Fortran
  160. strcpy(employees[2].empName.firstName, "Frank");
  161. strcpy(employees[2].empName.lastName, "Fortran");
  162. strcpy(employees[2].taxState, "VT");
  163. employees[2].clockNumber = 765349;
  164. employees[2].wageRate = 10.50;
  165.  
  166. // Employee 4: Jeff Ada
  167. strcpy(employees[3].empName.firstName, "Jeff");
  168. strcpy(employees[3].empName.lastName, "Ada");
  169. strcpy(employees[3].taxState, "NY");
  170. employees[3].clockNumber = 34645;
  171. employees[3].wageRate = 12.25;
  172.  
  173. // Employee 5: Anton Pascal
  174. strcpy(employees[4].empName.firstName, "Anton");
  175. strcpy(employees[4].empName.lastName, "Pascal");
  176. strcpy(employees[4].taxState, "CA");
  177. employees[4].clockNumber = 127615;
  178. employees[4].wageRate = 8.35; // Corrected from 10.00 to 8.35
  179.  
  180. // Initialize calculated fields to zero for all employees
  181. for(i = 0; i < MAX_EMPLOYEES; i++) {
  182. employees[i].hours = 0;
  183. employees[i].overtimeHrs = 0;
  184. employees[i].grossPay = 0;
  185. employees[i].stateTax = 0;
  186. employees[i].fedTax = 0;
  187. employees[i].netPay = 0;
  188. }
  189. }
  190.  
  191. /*****************************************************************************
  192.  * Function: getEmployeeHours
  193.  *
  194.  * Purpose: Reads hours worked for each employee from standard input
  195.  *
  196.  * Parameters: employees - array of employee structures
  197.  *
  198.  * Returns: void
  199.  *****************************************************************************/
  200. void getEmployeeHours(struct employee employees[]) {
  201. int i; // loop counter for reading employee hours
  202.  
  203. // Read hours for each employee from input
  204. for(i = 0; i < MAX_EMPLOYEES; i++) {
  205. scanf("%f", &employees[i].hours);
  206. }
  207. }
  208.  
  209. /*****************************************************************************
  210.  * Function: computeOvertime
  211.  *
  212.  * Purpose: Calculates overtime hours for each employee
  213.  *
  214.  * Parameters: employees - array of employee structures
  215.  *
  216.  * Returns: void
  217.  *****************************************************************************/
  218. void computeOvertime(struct employee employees[]) {
  219. int i; // loop counter for processing employees
  220.  
  221. // Calculate overtime hours for each employee
  222. for(i = 0; i < MAX_EMPLOYEES; i++) {
  223. // Check if employee worked more than standard work week
  224. if(employees[i].hours > STANDARD_WORK_WEEK) {
  225. // Calculate overtime hours (hours beyond 40)
  226. employees[i].overtimeHrs = employees[i].hours - STANDARD_WORK_WEEK;
  227. } else {
  228. // No overtime if hours are 40 or less
  229. employees[i].overtimeHrs = 0.0;
  230. }
  231. }
  232. }
  233.  
  234. /*****************************************************************************
  235.  * Function: computeGrossPay
  236.  *
  237.  * Purpose: Calculates gross pay including overtime at time-and-a-half
  238.  *
  239.  * Parameters: employees - array of employee structures
  240.  *
  241.  * Returns: void
  242.  *****************************************************************************/
  243. void computeGrossPay(struct employee employees[]) {
  244. int i; // loop counter for processing employees
  245. float regularHours; // hours up to 40, used for base pay calculation
  246. float basePay; // pay for regular hours (first 40 hours)
  247. float overtimePay; // pay for overtime hours at time-and-a-half rate
  248.  
  249. // Calculate gross pay for each employee
  250. for(i = 0; i < MAX_EMPLOYEES; i++) {
  251. // Determine regular hours (capped at 40 hours maximum)
  252. regularHours = employees[i].hours;
  253. if(regularHours > STANDARD_WORK_WEEK) {
  254. regularHours = STANDARD_WORK_WEEK;
  255. }
  256.  
  257. // Calculate base pay for regular hours
  258. basePay = regularHours * employees[i].wageRate;
  259.  
  260. // Calculate overtime pay at time-and-a-half rate
  261. overtimePay = employees[i].overtimeHrs * employees[i].wageRate * OVERTIME_RATE;
  262.  
  263. // Total gross pay is base pay plus overtime pay
  264. employees[i].grossPay = basePay + overtimePay;
  265. }
  266. }
  267.  
  268. /*****************************************************************************
  269.  * Function: computeTaxesAndNetPay
  270.  *
  271.  * Purpose: Calculates state tax, federal tax, and net pay for each employee
  272.  *
  273.  * Parameters: employees - array of employee structures
  274.  *
  275.  * Returns: void
  276.  *****************************************************************************/
  277. void computeTaxesAndNetPay(struct employee employees[]) {
  278. int i; // loop counter for processing employees
  279. float stateRate; // state-specific tax rate based on employee's work state
  280.  
  281. // Calculate taxes and net pay for each employee
  282. for(i = 0; i < MAX_EMPLOYEES; i++) {
  283. // Get state tax rate based on employee's work state
  284. stateRate = getStateTaxRate(employees[i].taxState);
  285.  
  286. // Calculate state tax amount
  287. employees[i].stateTax = employees[i].grossPay * stateRate;
  288.  
  289. // Calculate federal tax amount (flat 25% rate)
  290. employees[i].fedTax = employees[i].grossPay * FED_TAX_RATE;
  291.  
  292. // Calculate net pay (gross pay minus all taxes)
  293. employees[i].netPay = employees[i].grossPay - employees[i].stateTax - employees[i].fedTax;
  294. }
  295. }
  296.  
  297. /*****************************************************************************
  298.  * Function: calculateSummaryStats
  299.  *
  300.  * Purpose: Calculates comprehensive summary statistics for all employees
  301.  *
  302.  * Parameters: employees - array of employee structures
  303.  * stats - pointer to summaryStats structure to store results
  304.  *
  305.  * Returns: void
  306.  *****************************************************************************/
  307. void calculateSummaryStats(struct employee employees[], struct summaryStats *stats) {
  308. int i; // loop counter for processing employee data
  309.  
  310. // Initialize all totals to zero
  311. stats->totalWage = 0;
  312. stats->totalHours = 0;
  313. stats->totalOvertimeHrs = 0;
  314. stats->totalGrossPay = 0;
  315. stats->totalStateTax = 0;
  316. stats->totalFedTax = 0;
  317. stats->totalNetPay = 0;
  318.  
  319. // Initialize min values with first employee's data as starting point
  320. stats->minWage = employees[0].wageRate;
  321. stats->minHours = employees[0].hours;
  322. stats->minOvertimeHrs = employees[0].overtimeHrs;
  323. stats->minGrossPay = employees[0].grossPay;
  324. stats->minStateTax = employees[0].stateTax;
  325. stats->minFedTax = employees[0].fedTax;
  326. stats->minNetPay = employees[0].netPay;
  327.  
  328. // Initialize max values with first employee's data as starting point
  329. stats->maxWage = employees[0].wageRate;
  330. stats->maxHours = employees[0].hours;
  331. stats->maxOvertimeHrs = employees[0].overtimeHrs;
  332. stats->maxGrossPay = employees[0].grossPay;
  333. stats->maxStateTax = employees[0].stateTax;
  334. stats->maxFedTax = employees[0].fedTax;
  335. stats->maxNetPay = employees[0].netPay;
  336.  
  337. // Process all employees to calculate totals and find min/max values
  338. for(i = 0; i < MAX_EMPLOYEES; i++) {
  339. // Calculate totals by summing each employee's data
  340. stats->totalWage += employees[i].wageRate;
  341. stats->totalHours += employees[i].hours;
  342. stats->totalOvertimeHrs += employees[i].overtimeHrs;
  343. stats->totalGrossPay += employees[i].grossPay;
  344. stats->totalStateTax += employees[i].stateTax;
  345. stats->totalFedTax += employees[i].fedTax;
  346. stats->totalNetPay += employees[i].netPay;
  347.  
  348. // Find minimum values by comparing current min with each employee's data
  349. stats->minWage = findMin(stats->minWage, employees[i].wageRate);
  350. stats->minHours = findMin(stats->minHours, employees[i].hours);
  351. stats->minOvertimeHrs = findMin(stats->minOvertimeHrs, employees[i].overtimeHrs);
  352. stats->minGrossPay = findMin(stats->minGrossPay, employees[i].grossPay);
  353. stats->minStateTax = findMin(stats->minStateTax, employees[i].stateTax);
  354. stats->minFedTax = findMin(stats->minFedTax, employees[i].fedTax);
  355. stats->minNetPay = findMin(stats->minNetPay, employees[i].netPay);
  356.  
  357. // Find maximum values by comparing current max with each employee's data
  358. stats->maxWage = findMax(stats->maxWage, employees[i].wageRate);
  359. stats->maxHours = findMax(stats->maxHours, employees[i].hours);
  360. stats->maxOvertimeHrs = findMax(stats->maxOvertimeHrs, employees[i].overtimeHrs);
  361. stats->maxGrossPay = findMax(stats->maxGrossPay, employees[i].grossPay);
  362. stats->maxStateTax = findMax(stats->maxStateTax, employees[i].stateTax);
  363. stats->maxFedTax = findMax(stats->maxFedTax, employees[i].fedTax);
  364. stats->maxNetPay = findMax(stats->maxNetPay, employees[i].netPay);
  365. }
  366.  
  367. // Calculate averages by dividing totals by number of employees
  368. stats->avgWage = stats->totalWage / MAX_EMPLOYEES;
  369. stats->avgHours = stats->totalHours / MAX_EMPLOYEES;
  370. stats->avgOvertimeHrs = stats->totalOvertimeHrs / MAX_EMPLOYEES;
  371. stats->avgGrossPay = stats->totalGrossPay / MAX_EMPLOYEES;
  372. stats->avgStateTax = stats->totalStateTax / MAX_EMPLOYEES;
  373. stats->avgFedTax = stats->totalFedTax / MAX_EMPLOYEES;
  374. stats->avgNetPay = stats->totalNetPay / MAX_EMPLOYEES;
  375. }
  376.  
  377. /*****************************************************************************
  378.  * Function: displayReport
  379.  *
  380.  * Purpose: Displays the complete payroll report with summary statistics
  381.  *
  382.  * Parameters: employees - array of employee structures
  383.  *
  384.  * Returns: void
  385.  *****************************************************************************/
  386. void displayReport(struct employee employees[]) {
  387. struct summaryStats stats; // structure to hold summary statistics
  388. int i; // loop counter for displaying employees
  389. char fullName[25]; // buffer for combining first and last names
  390.  
  391. // Calculate summary statistics for the report
  392. calculateSummaryStats(employees, &stats);
  393.  
  394. // Display report header
  395. printf("\n*** Pay Calculator ***\n");
  396. printf("---------------------------------------------------------------------------------\n");
  397. printf("Name Tax Clock# Wage Hours OT Gross State Fed Net\n");
  398. printf(" State Pay Tax Tax Pay\n");
  399. printf("---------------------------------------------------------------------------------\n");
  400.  
  401. // Display each employee's data in formatted rows
  402. for(i = 0; i < MAX_EMPLOYEES; i++) {
  403. // Combine first and last name into full name for display
  404. sprintf(fullName, "%s %s", employees[i].empName.firstName, employees[i].empName.lastName);
  405.  
  406. // Print formatted employee data row
  407. printf("%-18s %-3s %06ld %5.2f %5.1f %4.1f %7.2f %5.2f %6.2f %7.2f\n",
  408. fullName,
  409. employees[i].taxState,
  410. employees[i].clockNumber,
  411. employees[i].wageRate,
  412. employees[i].hours,
  413. employees[i].overtimeHrs,
  414. employees[i].grossPay,
  415. employees[i].stateTax,
  416. employees[i].fedTax,
  417. employees[i].netPay);
  418. }
  419.  
  420. // Display summary statistics section
  421. printf("---------------------------------------------------------------------------------\n");
  422. printf("Totals: %5.2f %5.1f %4.1f %7.2f %5.2f %6.2f %7.2f\n",
  423. stats.totalWage, stats.totalHours, stats.totalOvertimeHrs,
  424. stats.totalGrossPay, stats.totalStateTax, stats.totalFedTax, stats.totalNetPay);
  425. printf("Averages: %5.2f %5.1f %4.1f %7.2f %5.2f %6.2f %7.2f\n",
  426. stats.avgWage, stats.avgHours, stats.avgOvertimeHrs,
  427. stats.avgGrossPay, stats.avgStateTax, stats.avgFedTax, stats.avgNetPay);
  428. printf("Minimum: %5.2f %5.1f %4.1f %7.2f %5.2f %6.2f %7.2f\n",
  429. stats.minWage, stats.minHours, stats.minOvertimeHrs,
  430. stats.minGrossPay, stats.minStateTax, stats.minFedTax, stats.minNetPay);
  431. printf("Maximum: %5.2f %5.1f %4.1f %7.2f %5.2f %6.2f %7.2f\n",
  432. stats.maxWage, stats.maxHours, stats.maxOvertimeHrs,
  433. stats.maxGrossPay, stats.maxStateTax, stats.maxFedTax, stats.maxNetPay);
  434. }
  435.  
  436. /*****************************************************************************
  437.  * Function: main
  438.  *
  439.  * Purpose: Program entry point - orchestrates the payroll calculation process
  440.  *
  441.  * Parameters: none
  442.  *
  443.  * Returns: int - program exit status
  444.  *****************************************************************************/
  445. int main() {
  446. struct employee employees[MAX_EMPLOYEES]; // array of employee structures
  447.  
  448. // Execute payroll processing steps in sequence
  449. initializeEmployees(employees); // Step 1: Initialize employee data
  450. getEmployeeHours(employees); // Step 2: Read hours worked
  451. computeOvertime(employees); // Step 3: Calculate overtime hours
  452. computeGrossPay(employees); // Step 4: Calculate gross pay
  453. computeTaxesAndNetPay(employees); // Step 5: Calculate taxes and net pay
  454. displayReport(employees); // Step 6: Display final report
  455.  
  456. return 0; // Return success status to operating system
  457. }
Success #stdin #stdout 0.01s 5292KB
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