fork download
  1.  
  2. //********************************************************
  3. // Assignment 8 - Structures and Strings and Pointers
  4. //
  5. // Name: Neftalia Cruz
  6. // Class: C Programming, Fall 2026
  7. // Date: November 5, 2026
  8. //
  9. // Description: Program which determines overtime and
  10. // gross pay for a set of employees.
  11. // Also calculates state tax, federal tax, net pay,
  12. // totals, averages, minimum and maximum values.
  13. //
  14. // Call by Reference design (using pointers)
  15. //********************************************************
  16.  
  17. #include <stdio.h>
  18. #include <string.h>
  19. #include <ctype.h>
  20.  
  21. // Define Constants
  22. #define SIZE 5
  23. #define STD_HOURS 40.0
  24. #define OT_RATE 1.5
  25. #define MA_TAX_RATE 0.05
  26. #define NH_TAX_RATE 0.0
  27. #define VT_TAX_RATE 0.06
  28. #define CA_TAX_RATE 0.07
  29. #define DEFAULT_TAX_RATE 0.08
  30. #define NAME_SIZE 20
  31. #define TAX_STATE_SIZE 3
  32. #define FED_TAX_RATE 0.25
  33. #define FIRST_NAME_SIZE 10
  34. #define LAST_NAME_SIZE 10
  35.  
  36. // Define employee name structure
  37. struct name
  38. {
  39. char firstName[FIRST_NAME_SIZE];
  40. char lastName[LAST_NAME_SIZE];
  41. };
  42.  
  43. // Define employee structure
  44. struct employee
  45. {
  46. struct name empName;
  47. char taxState[TAX_STATE_SIZE];
  48. long int clockNumber;
  49. float wageRate;
  50. float hours;
  51. float overtimeHrs;
  52. float grossPay;
  53. float stateTax;
  54. float fedTax;
  55. float netPay;
  56. };
  57.  
  58. // Define totals structure
  59. struct totals
  60. {
  61. float total_wageRate;
  62. float total_hours;
  63. float total_overtimeHrs;
  64. float total_grossPay;
  65. float total_stateTax;
  66. float total_fedTax;
  67. float total_netPay;
  68. };
  69.  
  70. // Define minimum and maximum structure
  71. struct min_max
  72. {
  73. float min_wageRate;
  74. float min_hours;
  75. float min_overtimeHrs;
  76. float min_grossPay;
  77. float min_stateTax;
  78. float min_fedTax;
  79. float min_netPay;
  80.  
  81. float max_wageRate;
  82. float max_hours;
  83. float max_overtimeHrs;
  84. float max_grossPay;
  85. float max_stateTax;
  86. float max_fedTax;
  87. float max_netPay;
  88. };
  89.  
  90. // Function prototypes
  91. void getHours(struct employee * emp_ptr, int theSize);
  92. void printEmp(struct employee * emp_ptr, int theSize);
  93.  
  94. void calcEmployeeTotals(struct employee * emp_ptr,
  95. struct totals * emp_totals_ptr,
  96. int theSize);
  97.  
  98. void calcEmployeeMinMax(struct employee * emp_ptr,
  99. struct min_max * emp_minMax_ptr,
  100. int theSize);
  101.  
  102. void printHeader(void);
  103.  
  104. // TODO 1 - Completed pointer prototypes
  105. void calcOvertimeHrs(struct employee * emp_ptr, int theSize);
  106. void calcGrossPay(struct employee * emp_ptr, int theSize);
  107. void calcStateTax(struct employee * emp_ptr, int theSize);
  108. void calcFedTax(struct employee * emp_ptr, int theSize);
  109. void calcNetPay(struct employee * emp_ptr, int theSize);
  110.  
  111. void printEmpStatistics(struct totals * emp_totals_ptr,
  112. struct min_max * emp_MinMax_ptr,
  113. int theSize);
  114.  
  115. //********************************************************
  116. // MAIN
  117. //********************************************************
  118.  
  119. int main()
  120. {
  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. // Declare employee pointer
  130. struct employee * emp_ptr;
  131.  
  132. emp_ptr = employeeData;
  133.  
  134. // Initialize totals
  135. struct totals employeeTotals = {0,0,0,0,0,0,0};
  136.  
  137. struct totals * emp_totals_ptr = &employeeTotals;
  138.  
  139. // Initialize minimum and maximum
  140. struct min_max employeeMinMax =
  141. {0,0,0,0,0,0,0,0,0,0,0,0,0,0};
  142.  
  143. struct min_max * emp_minMax_ptr = &employeeMinMax;
  144.  
  145. // Read employee hours
  146. getHours(employeeData, SIZE);
  147.  
  148. // Calculate overtime
  149. calcOvertimeHrs(employeeData, SIZE);
  150.  
  151. // Calculate gross pay
  152. calcGrossPay(employeeData, SIZE);
  153.  
  154. // Calculate state tax
  155. calcStateTax(employeeData, SIZE);
  156.  
  157. // Calculate federal tax
  158. calcFedTax(employeeData, SIZE);
  159.  
  160. // Calculate net pay
  161. calcNetPay(employeeData, SIZE);
  162.  
  163. // Calculate totals
  164. calcEmployeeTotals(employeeData,
  165. &employeeTotals,
  166. SIZE);
  167.  
  168. // Calculate minimum and maximum
  169. calcEmployeeMinMax(employeeData,
  170. &employeeMinMax,
  171. SIZE);
  172.  
  173. // Print report
  174. printHeader();
  175.  
  176. printEmp(employeeData, SIZE);
  177.  
  178. // TODO 2 - Pass addresses of structures
  179. printEmpStatistics(&employeeTotals,
  180. &employeeMinMax,
  181. SIZE);
  182.  
  183. return 0;
  184. }
  185.  
  186. //********************************************************
  187. // Function: getHours
  188. // Purpose: Read hours for each employee
  189. //********************************************************
  190.  
  191. void getHours(struct employee * emp_ptr, int theSize)
  192. {
  193. int i;
  194.  
  195. for (i = 0; i < theSize; ++i)
  196. {
  197. printf("\nEnter hours worked by emp # %06li: ",
  198. emp_ptr->clockNumber);
  199.  
  200. scanf("%f", &emp_ptr->hours);
  201.  
  202. ++emp_ptr;
  203. }
  204. }
  205.  
  206. //********************************************************
  207. // Function: printHeader
  208. // Purpose: Print payroll report headings
  209. //********************************************************
  210.  
  211. void printHeader(void)
  212. {
  213. printf("\n\n*** Pay Calculator ***\n");
  214.  
  215. printf("\n--------------------------------------------------------------");
  216. printf("-------------------");
  217.  
  218. printf("\nName Tax Clock# Wage Hours OT Gross ");
  219. printf(" State Fed Net");
  220.  
  221. printf("\n State Pay ");
  222. printf(" Tax Tax Pay");
  223.  
  224. printf("\n--------------------------------------------------------------");
  225. printf("-------------------");
  226. }
  227.  
  228. //********************************************************
  229. // Function: printEmp
  230. // Purpose: Print each employee's payroll information
  231. //********************************************************
  232.  
  233. void printEmp(struct employee * emp_ptr, int theSize)
  234. {
  235. int i;
  236.  
  237. char name[FIRST_NAME_SIZE + LAST_NAME_SIZE + 1];
  238.  
  239. for (i = 0; i < theSize; ++i)
  240. {
  241. strcpy(name, emp_ptr->empName.firstName);
  242. strcat(name, " ");
  243. strcat(name, emp_ptr->empName.lastName);
  244.  
  245. printf("\n%-20.20s %-2.2s %06li %5.2f %4.1f %4.1f %7.2f %6.2f %7.2f %8.2f",
  246. name,
  247. emp_ptr->taxState,
  248. emp_ptr->clockNumber,
  249. emp_ptr->wageRate,
  250. emp_ptr->hours,
  251. emp_ptr->overtimeHrs,
  252. emp_ptr->grossPay,
  253. emp_ptr->stateTax,
  254. emp_ptr->fedTax,
  255. emp_ptr->netPay);
  256.  
  257. ++emp_ptr;
  258. }
  259. }
  260.  
  261. //********************************************************
  262. // Function: printEmpStatistics
  263. // Purpose: Print totals, averages, minimum and maximum
  264. //
  265. // TODO 3 - Completed using pointer references
  266. //********************************************************
  267.  
  268. void printEmpStatistics(struct totals * emp_totals_ptr,
  269. struct min_max * emp_MinMax_ptr,
  270. int theSize)
  271. {
  272. printf("\n--------------------------------------------------------------");
  273. printf("-------------------");
  274.  
  275. // Print totals
  276. printf("\nTotals: %5.2f %5.1f %5.1f %7.2f %6.2f %7.2f %8.2f",
  277. emp_totals_ptr->total_wageRate,
  278. emp_totals_ptr->total_hours,
  279. emp_totals_ptr->total_overtimeHrs,
  280. emp_totals_ptr->total_grossPay,
  281. emp_totals_ptr->total_stateTax,
  282. emp_totals_ptr->total_fedTax,
  283. emp_totals_ptr->total_netPay);
  284.  
  285. // Print averages
  286. if (theSize > 0)
  287. {
  288. printf("\nAverages: %5.2f %5.1f %5.1f %7.2f %6.2f %7.2f %8.2f",
  289. emp_totals_ptr->total_wageRate / theSize,
  290. emp_totals_ptr->total_hours / theSize,
  291. emp_totals_ptr->total_overtimeHrs / theSize,
  292. emp_totals_ptr->total_grossPay / theSize,
  293. emp_totals_ptr->total_stateTax / theSize,
  294. emp_totals_ptr->total_fedTax / theSize,
  295. emp_totals_ptr->total_netPay / theSize);
  296. }
  297.  
  298. // Print minimum values
  299. printf("\nMinimum: %5.2f %5.1f %5.1f %7.2f %6.2f %7.2f %8.2f",
  300. emp_MinMax_ptr->min_wageRate,
  301. emp_MinMax_ptr->min_hours,
  302. emp_MinMax_ptr->min_overtimeHrs,
  303. emp_MinMax_ptr->min_grossPay,
  304. emp_MinMax_ptr->min_stateTax,
  305. emp_MinMax_ptr->min_fedTax,
  306. emp_MinMax_ptr->min_netPay);
  307.  
  308. // Print maximum values
  309. printf("\nMaximum: %5.2f %5.1f %5.1f %7.2f %6.2f %7.2f %8.2f",
  310. emp_MinMax_ptr->max_wageRate,
  311. emp_MinMax_ptr->max_hours,
  312. emp_MinMax_ptr->max_overtimeHrs,
  313. emp_MinMax_ptr->max_grossPay,
  314. emp_MinMax_ptr->max_stateTax,
  315. emp_MinMax_ptr->max_fedTax,
  316. emp_MinMax_ptr->max_netPay);
  317. }
  318.  
  319. //********************************************************
  320. // Function: calcOvertimeHrs
  321. // Purpose: Calculate overtime hours
  322. //
  323. // TODO 4 - Completed using pointers
  324. //********************************************************
  325.  
  326. void calcOvertimeHrs(struct employee * emp_ptr, int theSize)
  327. {
  328. int i;
  329.  
  330. for (i = 0; i < theSize; ++i)
  331. {
  332. if (emp_ptr->hours >= STD_HOURS)
  333. {
  334. emp_ptr->overtimeHrs =
  335. emp_ptr->hours - STD_HOURS;
  336. }
  337. else
  338. {
  339. emp_ptr->overtimeHrs = 0;
  340. }
  341.  
  342. ++emp_ptr;
  343. }
  344. }
  345.  
  346. //********************************************************
  347. // Function: calcGrossPay
  348. // Purpose: Calculate gross pay
  349. //
  350. // TODO 5 - Completed using pointers
  351. //********************************************************
  352.  
  353. void calcGrossPay(struct employee * emp_ptr, int theSize)
  354. {
  355. int i;
  356. float theNormalPay;
  357. float theOvertimePay;
  358.  
  359. for (i = 0; i < theSize; ++i)
  360. {
  361. theNormalPay =
  362. emp_ptr->wageRate *
  363. (emp_ptr->hours - emp_ptr->overtimeHrs);
  364.  
  365. theOvertimePay =
  366. emp_ptr->overtimeHrs *
  367. (OT_RATE * emp_ptr->wageRate);
  368.  
  369. emp_ptr->grossPay =
  370. theNormalPay + theOvertimePay;
  371.  
  372. ++emp_ptr;
  373. }
  374. }
  375.  
  376. //********************************************************
  377. // Function: calcStateTax
  378. // Purpose: Calculate state tax
  379. //
  380. // TODO 6 - Completed using pointers
  381. //********************************************************
  382.  
  383. void calcStateTax(struct employee * emp_ptr, int theSize)
  384. {
  385. int i;
  386.  
  387. for (i = 0; i < theSize; ++i)
  388. {
  389. // Make sure tax state is uppercase
  390. if (islower(emp_ptr->taxState[0]))
  391. emp_ptr->taxState[0] =
  392. toupper(emp_ptr->taxState[0]);
  393.  
  394. if (islower(emp_ptr->taxState[1]))
  395. emp_ptr->taxState[1] =
  396. toupper(emp_ptr->taxState[1]);
  397.  
  398. // Calculate state tax
  399. if (strcmp(emp_ptr->taxState, "MA") == 0)
  400. {
  401. emp_ptr->stateTax =
  402. emp_ptr->grossPay * MA_TAX_RATE;
  403. }
  404. else if (strcmp(emp_ptr->taxState, "VT") == 0)
  405. {
  406. emp_ptr->stateTax =
  407. emp_ptr->grossPay * VT_TAX_RATE;
  408. }
  409. else if (strcmp(emp_ptr->taxState, "NH") == 0)
  410. {
  411. emp_ptr->stateTax =
  412. emp_ptr->grossPay * NH_TAX_RATE;
  413. }
  414. else if (strcmp(emp_ptr->taxState, "CA") == 0)
  415. {
  416. emp_ptr->stateTax =
  417. emp_ptr->grossPay * CA_TAX_RATE;
  418. }
  419. else
  420. {
  421. emp_ptr->stateTax =
  422. emp_ptr->grossPay * DEFAULT_TAX_RATE;
  423. }
  424.  
  425. ++emp_ptr;
  426. }
  427. }
  428.  
  429. //********************************************************
  430. // Function: calcFedTax
  431. // Purpose: Calculate federal tax
  432. //
  433. // TODO 7 - Completed using pointers
  434. //********************************************************
  435.  
  436. void calcFedTax(struct employee * emp_ptr, int theSize)
  437. {
  438. int i;
  439.  
  440. for (i = 0; i < theSize; ++i)
  441. {
  442. emp_ptr->fedTax =
  443. emp_ptr->grossPay * FED_TAX_RATE;
  444.  
  445. ++emp_ptr;
  446. }
  447. }
  448.  
  449. //********************************************************
  450. // Function: calcNetPay
  451. // Purpose: Calculate net pay
  452. //
  453. // TODO 8 - Completed using pointers
  454. //********************************************************
  455.  
  456. void calcNetPay(struct employee * emp_ptr, int theSize)
  457. {
  458. int i;
  459. float theTotalTaxes;
  460.  
  461. for (i = 0; i < theSize; ++i)
  462. {
  463. theTotalTaxes =
  464. emp_ptr->stateTax + emp_ptr->fedTax;
  465.  
  466. emp_ptr->netPay =
  467. emp_ptr->grossPay - theTotalTaxes;
  468.  
  469. ++emp_ptr;
  470. }
  471. }
  472.  
  473. //********************************************************
  474. // Function: calcEmployeeTotals
  475. // Purpose: Calculate running totals
  476. // Already completed in professor's template
  477. //********************************************************
  478.  
  479. void calcEmployeeTotals(struct employee * emp_ptr,
  480. struct totals * emp_totals_ptr,
  481. int theSize)
  482. {
  483. int i;
  484.  
  485. for (i = 0; i < theSize; ++i)
  486. {
  487. emp_totals_ptr->total_wageRate +=
  488. emp_ptr->wageRate;
  489.  
  490. emp_totals_ptr->total_hours +=
  491. emp_ptr->hours;
  492.  
  493. emp_totals_ptr->total_overtimeHrs +=
  494. emp_ptr->overtimeHrs;
  495.  
  496. emp_totals_ptr->total_grossPay +=
  497. emp_ptr->grossPay;
  498.  
  499. emp_totals_ptr->total_stateTax +=
  500. emp_ptr->stateTax;
  501.  
  502. emp_totals_ptr->total_fedTax +=
  503. emp_ptr->fedTax;
  504.  
  505. emp_totals_ptr->total_netPay +=
  506. emp_ptr->netPay;
  507.  
  508. ++emp_ptr;
  509. }
  510. }
  511.  
  512. //********************************************************
  513. // Function: calcEmployeeMinMax
  514. // Purpose: Calculate minimum and maximum values
  515. // Already completed in professor's template
  516. //********************************************************
  517.  
  518. void calcEmployeeMinMax(struct employee * emp_ptr,
  519. struct min_max * emp_minMax_ptr,
  520. int theSize)
  521. {
  522. int i;
  523.  
  524. // Set minimum values using first employee
  525. emp_minMax_ptr->min_wageRate = emp_ptr->wageRate;
  526. emp_minMax_ptr->min_hours = emp_ptr->hours;
  527. emp_minMax_ptr->min_overtimeHrs = emp_ptr->overtimeHrs;
  528. emp_minMax_ptr->min_grossPay = emp_ptr->grossPay;
  529. emp_minMax_ptr->min_stateTax = emp_ptr->stateTax;
  530. emp_minMax_ptr->min_fedTax = emp_ptr->fedTax;
  531. emp_minMax_ptr->min_netPay = emp_ptr->netPay;
  532.  
  533. // Set maximum values using first employee
  534. emp_minMax_ptr->max_wageRate = emp_ptr->wageRate;
  535. emp_minMax_ptr->max_hours = emp_ptr->hours;
  536. emp_minMax_ptr->max_overtimeHrs = emp_ptr->overtimeHrs;
  537. emp_minMax_ptr->max_grossPay = emp_ptr->grossPay;
  538. emp_minMax_ptr->max_stateTax = emp_ptr->stateTax;
  539. emp_minMax_ptr->max_fedTax = emp_ptr->fedTax;
  540. emp_minMax_ptr->max_netPay = emp_ptr->netPay;
  541.  
  542. // Compare remaining employees
  543. for (i = 1; i < theSize; ++i)
  544. {
  545. ++emp_ptr;
  546.  
  547. // Wage rate minimum and maximum
  548. if (emp_ptr->wageRate < emp_minMax_ptr->min_wageRate)
  549. {
  550. emp_minMax_ptr->min_wageRate = emp_ptr->wageRate;
  551. }
  552.  
  553. if (emp_ptr->wageRate > emp_minMax_ptr->max_wageRate)
  554. {
  555. emp_minMax_ptr->max_wageRate = emp_ptr->wageRate;
  556. }
  557.  
  558. // Hours minimum and maximum
  559. if (emp_ptr->hours < emp_minMax_ptr->min_hours)
  560. {
  561. emp_minMax_ptr->min_hours = emp_ptr->hours;
  562. }
  563.  
  564. if (emp_ptr->hours > emp_minMax_ptr->max_hours)
  565. {
  566. emp_minMax_ptr->max_hours = emp_ptr->hours;
  567. }
  568.  
  569. // Overtime hours minimum and maximum
  570. if (emp_ptr->overtimeHrs < emp_minMax_ptr->min_overtimeHrs)
  571. {
  572. emp_minMax_ptr->min_overtimeHrs = emp_ptr->overtimeHrs;
  573. }
  574.  
  575. if (emp_ptr->overtimeHrs > emp_minMax_ptr->max_overtimeHrs)
  576. {
  577. emp_minMax_ptr->max_overtimeHrs = emp_ptr->overtimeHrs;
  578. }
  579.  
  580. // Gross pay minimum and maximum
  581. if (emp_ptr->grossPay < emp_minMax_ptr->min_grossPay)
  582. {
  583. emp_minMax_ptr->min_grossPay = emp_ptr->grossPay;
  584. }
  585.  
  586. if (emp_ptr->grossPay > emp_minMax_ptr->max_grossPay)
  587. {
  588. emp_minMax_ptr->max_grossPay = emp_ptr->grossPay;
  589. }
  590.  
  591. // State tax minimum and maximum
  592. if (emp_ptr->stateTax < emp_minMax_ptr->min_stateTax)
  593. {
  594. emp_minMax_ptr->min_stateTax = emp_ptr->stateTax;
  595. }
  596.  
  597. if (emp_ptr->stateTax > emp_minMax_ptr->max_stateTax)
  598. {
  599. emp_minMax_ptr->max_stateTax = emp_ptr->stateTax;
  600. }
  601.  
  602. // Federal tax minimum and maximum
  603. if (emp_ptr->fedTax < emp_minMax_ptr->min_fedTax)
  604. {
  605. emp_minMax_ptr->min_fedTax = emp_ptr->fedTax;
  606. }
  607.  
  608. if (emp_ptr->fedTax > emp_minMax_ptr->max_fedTax)
  609. {
  610. emp_minMax_ptr->max_fedTax = emp_ptr->fedTax;
  611. }
  612.  
  613. // Net pay minimum and maximum
  614. if (emp_ptr->netPay < emp_minMax_ptr->min_netPay)
  615. {
  616. emp_minMax_ptr->min_netPay = emp_ptr->netPay;
  617. }
  618.  
  619. if (emp_ptr->netPay > emp_minMax_ptr->max_netPay)
  620. {
  621. emp_minMax_ptr->max_netPay = emp_ptr->netPay;
  622. }
  623. }
  624. }
  625.  
Success #stdin #stdout 0s 5336KB
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