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. 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. // Employee 1: Connie Cobol
  143. strcpy(employees[0].empName.firstName, "Connie");
  144. strcpy(employees[0].empName.lastName, "Cobol");
  145. strcpy(employees[0].taxState, "MA");
  146. employees[0].clockNumber = 98401;
  147. employees[0].wageRate = 10.60;
  148.  
  149. // Employee 2: Mary Apl
  150. strcpy(employees[1].empName.firstName, "Mary");
  151. strcpy(employees[1].empName.lastName, "Apl");
  152. strcpy(employees[1].taxState, "NH");
  153. employees[1].clockNumber = 526488;
  154. employees[1].wageRate = 9.75;
  155.  
  156. // Employee 3: Frank Fortran
  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. // Employee 4: Jeff Ada
  164. strcpy(employees[3].empName.firstName, "Jeff");
  165. strcpy(employees[3].empName.lastName, "Ada");
  166. strcpy(employees[3].taxState, "NY");
  167. employees[3].clockNumber = 34645;
  168. employees[3].wageRate = 12.25;
  169.  
  170. // Employee 5: Anton Pascal
  171. strcpy(employees[4].empName.firstName, "Anton");
  172. strcpy(employees[4].empName.lastName, "Pascal");
  173. strcpy(employees[4].taxState, "CA");
  174. employees[4].clockNumber = 127615;
  175. employees[4].wageRate = 8.35; // Corrected from 10.00 to 8.35
  176.  
  177. // Initialize calculated fields to zero
  178. for(int i = 0; i < MAX_EMPLOYEES; i++) {
  179. employees[i].hours = 0;
  180. employees[i].overtimeHrs = 0;
  181. employees[i].grossPay = 0;
  182. employees[i].stateTax = 0;
  183. employees[i].fedTax = 0;
  184. employees[i].netPay = 0;
  185. }
  186. }
  187.  
  188. /*****************************************************************************
  189.  * Function: getEmployeeHours
  190.  *
  191.  * Purpose: Reads hours worked for each employee from standard input
  192.  *
  193.  * Parameters: employees - array of employee structures
  194.  *
  195.  * Returns: void
  196.  *****************************************************************************/
  197. void getEmployeeHours(struct employee employees[]) {
  198. for(int i = 0; i < MAX_EMPLOYEES; i++) {
  199. scanf("%f", &employees[i].hours);
  200. }
  201. }
  202.  
  203. /*****************************************************************************
  204.  * Function: computeOvertime
  205.  *
  206.  * Purpose: Calculates overtime hours for each employee
  207.  *
  208.  * Parameters: employees - array of employee structures
  209.  *
  210.  * Returns: void
  211.  *****************************************************************************/
  212. void computeOvertime(struct employee employees[]) {
  213. for(int i = 0; i < MAX_EMPLOYEES; i++) {
  214. if(employees[i].hours > STANDARD_WORK_WEEK) {
  215. employees[i].overtimeHrs = employees[i].hours - STANDARD_WORK_WEEK;
  216. } else {
  217. employees[i].overtimeHrs = 0.0;
  218. }
  219. }
  220. }
  221.  
  222. /*****************************************************************************
  223.  * Function: computeGrossPay
  224.  *
  225.  * Purpose: Calculates gross pay including overtime at time-and-a-half
  226.  *
  227.  * Parameters: employees - array of employee structures
  228.  *
  229.  * Returns: void
  230.  *****************************************************************************/
  231. void computeGrossPay(struct employee employees[]) {
  232. for(int i = 0; i < MAX_EMPLOYEES; i++) {
  233. float regularHours = employees[i].hours;
  234. if(regularHours > STANDARD_WORK_WEEK) {
  235. regularHours = STANDARD_WORK_WEEK;
  236. }
  237. employees[i].grossPay = (regularHours * employees[i].wageRate) +
  238. (employees[i].overtimeHrs * employees[i].wageRate * OVERTIME_RATE);
  239. }
  240. }
  241.  
  242. /*****************************************************************************
  243.  * Function: computeTaxesAndNetPay
  244.  *
  245.  * Purpose: Calculates state tax, federal tax, and net pay for each employee
  246.  *
  247.  * Parameters: employees - array of employee structures
  248.  *
  249.  * Returns: void
  250.  *****************************************************************************/
  251. void computeTaxesAndNetPay(struct employee employees[]) {
  252. for(int i = 0; i < MAX_EMPLOYEES; i++) {
  253. float stateRate = getStateTaxRate(employees[i].taxState);
  254. employees[i].stateTax = employees[i].grossPay * stateRate;
  255. employees[i].fedTax = employees[i].grossPay * FED_TAX_RATE;
  256. employees[i].netPay = employees[i].grossPay - employees[i].stateTax - employees[i].fedTax;
  257. }
  258. }
  259.  
  260. /*****************************************************************************
  261.  * Function: calculateSummaryStats
  262.  *
  263.  * Purpose: Calculates comprehensive summary statistics for all employees
  264.  *
  265.  * Parameters: employees - array of employee structures
  266.  * stats - pointer to summaryStats structure to store results
  267.  *
  268.  * Returns: void
  269.  *****************************************************************************/
  270. void calculateSummaryStats(struct employee employees[], struct summaryStats *stats) {
  271. // Initialize totals to zero
  272. stats->totalWage = 0;
  273. stats->totalHours = 0;
  274. stats->totalOvertimeHrs = 0;
  275. stats->totalGrossPay = 0;
  276. stats->totalStateTax = 0;
  277. stats->totalFedTax = 0;
  278. stats->totalNetPay = 0;
  279.  
  280. // Initialize min values with first employee's data
  281. stats->minWage = employees[0].wageRate;
  282. stats->minHours = employees[0].hours;
  283. stats->minOvertimeHrs = employees[0].overtimeHrs;
  284. stats->minGrossPay = employees[0].grossPay;
  285. stats->minStateTax = employees[0].stateTax;
  286. stats->minFedTax = employees[0].fedTax;
  287. stats->minNetPay = employees[0].netPay;
  288.  
  289. // Initialize max values with first employee's data
  290. stats->maxWage = employees[0].wageRate;
  291. stats->maxHours = employees[0].hours;
  292. stats->maxOvertimeHrs = employees[0].overtimeHrs;
  293. stats->maxGrossPay = employees[0].grossPay;
  294. stats->maxStateTax = employees[0].stateTax;
  295. stats->maxFedTax = employees[0].fedTax;
  296. stats->maxNetPay = employees[0].netPay;
  297.  
  298. // Calculate totals and find min/max values
  299. for(int i = 0; i < MAX_EMPLOYEES; i++) {
  300. // Calculate totals
  301. stats->totalWage += employees[i].wageRate;
  302. stats->totalHours += employees[i].hours;
  303. stats->totalOvertimeHrs += employees[i].overtimeHrs;
  304. stats->totalGrossPay += employees[i].grossPay;
  305. stats->totalStateTax += employees[i].stateTax;
  306. stats->totalFedTax += employees[i].fedTax;
  307. stats->totalNetPay += employees[i].netPay;
  308.  
  309. // Find minimum values
  310. stats->minWage = findMin(stats->minWage, employees[i].wageRate);
  311. stats->minHours = findMin(stats->minHours, employees[i].hours);
  312. stats->minOvertimeHrs = findMin(stats->minOvertimeHrs, employees[i].overtimeHrs);
  313. stats->minGrossPay = findMin(stats->minGrossPay, employees[i].grossPay);
  314. stats->minStateTax = findMin(stats->minStateTax, employees[i].stateTax);
  315. stats->minFedTax = findMin(stats->minFedTax, employees[i].fedTax);
  316. stats->minNetPay = findMin(stats->minNetPay, employees[i].netPay);
  317.  
  318. // Find maximum values
  319. stats->maxWage = findMax(stats->maxWage, employees[i].wageRate);
  320. stats->maxHours = findMax(stats->maxHours, employees[i].hours);
  321. stats->maxOvertimeHrs = findMax(stats->maxOvertimeHrs, employees[i].overtimeHrs);
  322. stats->maxGrossPay = findMax(stats->maxGrossPay, employees[i].grossPay);
  323. stats->maxStateTax = findMax(stats->maxStateTax, employees[i].stateTax);
  324. stats->maxFedTax = findMax(stats->maxFedTax, employees[i].fedTax);
  325. stats->maxNetPay = findMax(stats->maxNetPay, employees[i].netPay);
  326. }
  327.  
  328. // Calculate averages
  329. stats->avgWage = stats->totalWage / MAX_EMPLOYEES;
  330. stats->avgHours = stats->totalHours / MAX_EMPLOYEES;
  331. stats->avgOvertimeHrs = stats->totalOvertimeHrs / MAX_EMPLOYEES;
  332. stats->avgGrossPay = stats->totalGrossPay / MAX_EMPLOYEES;
  333. stats->avgStateTax = stats->totalStateTax / MAX_EMPLOYEES;
  334. stats->avgFedTax = stats->totalFedTax / MAX_EMPLOYEES;
  335. stats->avgNetPay = stats->totalNetPay / MAX_EMPLOYEES;
  336. }
  337.  
  338. /*****************************************************************************
  339.  * Function: displayReport
  340.  *
  341.  * Purpose: Displays the complete payroll report with summary statistics
  342.  *
  343.  * Parameters: employees - array of employee structures
  344.  *
  345.  * Returns: void
  346.  *****************************************************************************/
  347. void displayReport(struct employee employees[]) {
  348. struct summaryStats stats;
  349. calculateSummaryStats(employees, &stats);
  350.  
  351. printf("\n*** Pay Calculator ***\n");
  352. printf("---------------------------------------------------------------------------------\n");
  353. printf("Name Tax Clock# Wage Hours OT Gross State Fed Net\n");
  354. printf(" State Pay Tax Tax Pay\n");
  355. printf("---------------------------------------------------------------------------------\n");
  356.  
  357. for(int i = 0; i < MAX_EMPLOYEES; i++) {
  358. char fullName[25]; // Buffer for full name display
  359. sprintf(fullName, "%s %s", employees[i].empName.firstName, employees[i].empName.lastName);
  360.  
  361. printf("%-18s %-3s %06ld %5.2f %5.1f %4.1f %7.2f %5.2f %6.2f %7.2f\n",
  362. fullName,
  363. employees[i].taxState,
  364. employees[i].clockNumber,
  365. employees[i].wageRate,
  366. employees[i].hours,
  367. employees[i].overtimeHrs,
  368. employees[i].grossPay,
  369. employees[i].stateTax,
  370. employees[i].fedTax,
  371. employees[i].netPay);
  372. }
  373.  
  374. printf("---------------------------------------------------------------------------------\n");
  375. printf("Totals: %5.2f %5.1f %4.1f %7.2f %5.2f %6.2f %7.2f\n",
  376. stats.totalWage, stats.totalHours, stats.totalOvertimeHrs,
  377. stats.totalGrossPay, stats.totalStateTax, stats.totalFedTax, stats.totalNetPay);
  378. printf("Averages: %5.2f %5.1f %4.1f %7.2f %5.2f %6.2f %7.2f\n",
  379. stats.avgWage, stats.avgHours, stats.avgOvertimeHrs,
  380. stats.avgGrossPay, stats.avgStateTax, stats.avgFedTax, stats.avgNetPay);
  381. printf("Minimum: %5.2f %5.1f %4.1f %7.2f %5.2f %6.2f %7.2f\n",
  382. stats.minWage, stats.minHours, stats.minOvertimeHrs,
  383. stats.minGrossPay, stats.minStateTax, stats.minFedTax, stats.minNetPay);
  384. printf("Maximum: %5.2f %5.1f %4.1f %7.2f %5.2f %6.2f %7.2f\n",
  385. stats.maxWage, stats.maxHours, stats.maxOvertimeHrs,
  386. stats.maxGrossPay, stats.maxStateTax, stats.maxFedTax, stats.maxNetPay);
  387. }
  388.  
  389. /*****************************************************************************
  390.  * Function: main
  391.  *
  392.  * Purpose: Program entry point - orchestrates the payroll calculation process
  393.  *
  394.  * Parameters: none
  395.  *
  396.  * Returns: int - program exit status
  397.  *****************************************************************************/
  398. int main() {
  399. struct employee employees[MAX_EMPLOYEES]; // Array of employee structures
  400.  
  401. // Execute payroll processing steps
  402. initializeEmployees(employees);
  403. getEmployeeHours(employees);
  404. computeOvertime(employees);
  405. computeGrossPay(employees);
  406. computeTaxesAndNetPay(employees);
  407. displayReport(employees);
  408.  
  409. return 0;
  410. }
Success #stdin #stdout 0s 5316KB
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