fork download
  1. //********************************************************
  2. //
  3. // Assignment 8 - Structures and Strings and Pointers
  4. //
  5. // Name: <Mario Toribio>
  6. //
  7. // Class: C Programming, <Summer 2025>
  8. //
  9. // Date: <07/16/2025>
  10. //
  11. // Description: Program which determines overtime and
  12. // gross pay for a set of employees with outputs sent
  13. // to standard output (the screen).
  14. //
  15. // This assignment also adds the employee name, their tax state,
  16. // and calculates the state tax, federal tax, and net pay. It
  17. // also calculates totals, averages, minimum, and maximum values.
  18. //
  19. // Array and Structure references are to be replaced with
  20. // pointer references to speed up the processing of this code.
  21. //
  22. // Call by Reference design (using pointers)
  23. //
  24. //********************************************************
  25.  
  26. //********************************************************
  27. //
  28. // Assignment 8 - Structures and Strings and Pointers
  29. //
  30. // Name: Natelle Lewis
  31. //
  32. // Class: C Programming, Spring 2025
  33. //
  34. // Date: 05/05/25
  35. //
  36. // Description: Program which determines overtime and
  37. // gross pay for a set of employees with outputs sent
  38. // to standard output (the screen).
  39. //
  40. // This assignment also adds the employee name, their tax state,
  41. // and calculates the state tax, federal tax, and net pay. It
  42. // also calculates totals, averages, minimum, and maximum values.
  43. //
  44. // Array and Structure references are to be replaced with
  45. // pointer references to speed up the processing of this code.
  46. //
  47. // Call by Reference design (using pointers)
  48. //
  49. //********************************************************
  50.  
  51. // necessary header files
  52. #include <stdio.h>
  53. #include <string.h>
  54. #include <ctype.h>
  55.  
  56. // define constants
  57. #define SIZE 5
  58. #define STD_HOURS 40.0
  59. #define OT_RATE 1.5
  60. #define MA_TAX_RATE 0.05
  61. #define NH_TAX_RATE 0.0
  62. #define VT_TAX_RATE 0.06
  63. #define CA_TAX_RATE 0.07
  64. #define DEFAULT_TAX_RATE 0.08
  65. #define NAME_SIZE 20
  66. #define TAX_STATE_SIZE 3
  67. #define FED_TAX_RATE 0.25
  68. #define FIRST_NAME_SIZE 10
  69. #define LAST_NAME_SIZE 10
  70.  
  71. // Define a structure type to store an employee name
  72. // ... note how one could easily extend this to other parts
  73. // parts of a name: Middle, Nickname, Prefix, Suffix, etc.
  74. struct name
  75. {
  76. char firstName[FIRST_NAME_SIZE];
  77. char lastName [LAST_NAME_SIZE];
  78. };
  79.  
  80. // Define a structure type to pass employee data between functions
  81. // Note that the structure type is global, but you don't want a variable
  82. // of that type to be global. Best to declare a variable of that type
  83. // in a function like main or another function and pass as needed.
  84. struct employee
  85. {
  86. struct name empName;
  87. char taxState [TAX_STATE_SIZE];
  88. long int clockNumber;
  89. float wageRate;
  90. float hours;
  91. float overtimeHrs;
  92. float grossPay;
  93. float stateTax;
  94. float fedTax;
  95. float netPay;
  96. };
  97.  
  98. // this structure type defines the totals of all floating point items
  99. // so they can be totaled and used also to calculate averages
  100. struct totals
  101. {
  102. float total_wageRate;
  103. float total_hours;
  104. float total_overtimeHrs;
  105. float total_grossPay;
  106. float total_stateTax;
  107. float total_fedTax;
  108. float total_netPay;
  109. };
  110.  
  111. // this structure type defines the min and max values of all floating
  112. // point items so they can be display in our final report
  113. struct min_max
  114. {
  115. float min_wageRate;
  116. float min_hours;
  117. float min_overtimeHrs;
  118. float min_grossPay;
  119. float min_stateTax;
  120. float min_fedTax;
  121. float min_netPay;
  122. float max_wageRate;
  123. float max_hours;
  124. float max_overtimeHrs;
  125. float max_grossPay;
  126. float max_stateTax;
  127. float max_fedTax;
  128. float max_netPay;
  129. };
  130.  
  131. // define prototypes here for each function except main
  132.  
  133. // These prototypes have already been transitioned to pointers
  134. void getHours (struct employee * emp_ptr, int theSize);
  135. void printEmp (struct employee * emp_ptr, int theSize);
  136.  
  137. void calcEmployeeTotals (struct employee * emp_ptr,
  138. struct totals * emp_totals_ptr,
  139. int theSize);
  140.  
  141. void calcEmployeeMinMax (struct employee * emp_ptr,
  142. struct min_max * emp_MinMax_ptr,
  143. int theSize);
  144.  
  145. // This prototype does not need to use pointers
  146. void printHeader (void);
  147.  
  148.  
  149. // TODO - Transition these prototypes from using arrays to
  150. // using pointers (use emp_ptr instead of employeeData for
  151. // the first parameter). See prototypes above for hints.
  152.  
  153. void calcOvertimeHrs (struct employee * emp_ptr, int theSize);
  154. void calcGrossPay (struct employee * emp_ptr, int theSize);
  155. void calcStateTax (struct employee * emp_ptr, int theSize);
  156. void calcFedTax (struct employee * emp_ptr, int theSize);
  157. void calcNetPay (struct employee * emp_ptr, int theSize);
  158.  
  159. void printEmpStatistics (struct totals * emp_ptr,
  160. struct min_max * emp_MinMax_ptr,
  161. int theSize);
  162.  
  163. int main ()
  164. {
  165.  
  166. // Set up a local variable to store the employee information
  167. // Initialize the name, tax state, clock number, and wage rate
  168. struct employee employeeData[SIZE] = {
  169. { {"Connie", "Cobol"}, "MA", 98401, 10.60},
  170. { {"Mary", "Apl"}, "NH", 526488, 9.75 },
  171. { {"Frank", "Fortran"}, "VT", 765349, 10.50 },
  172. { {"Jeff", "Ada"}, "NY", 34645, 12.25 },
  173. { {"Anton", "Pascal"},"CA",127615, 8.35 }
  174. };
  175.  
  176. // declare a pointer to the array of employee structures
  177. struct employee * emp_ptr;
  178.  
  179. // set the pointer to point to the array of employees
  180. emp_ptr = employeeData;
  181.  
  182. // set up structure to store totals and initialize all to zero
  183. struct totals employeeTotals = {0,0,0,0,0,0,0};
  184.  
  185. // pointer to the employeeTotals structure
  186. struct totals * emp_totals_ptr = &employeeTotals;
  187.  
  188. // set up structure to store min and max values and initialize all to zero
  189. struct min_max employeeMinMax = {0,0,0,0,0,0,0,0,0,0,0,0,0,0};
  190.  
  191. // pointer to the employeeMinMax structure
  192. struct min_max * emp_minMax_ptr = &employeeMinMax;
  193.  
  194. // Call functions as needed to read and calculate information
  195.  
  196. // Prompt for the number of hours worked by the employee
  197. getHours (emp_ptr, SIZE);
  198.  
  199. // Calculate the overtime hours
  200. calcOvertimeHrs (emp_ptr, SIZE);
  201.  
  202. // Calculate the weekly gross pay
  203. calcGrossPay (emp_ptr, SIZE);
  204.  
  205. // Calculate the state tax
  206. calcStateTax (emp_ptr, SIZE);
  207.  
  208. // Calculate the federal tax
  209. calcFedTax (emp_ptr, SIZE);
  210.  
  211. // Calculate the net pay after taxes
  212. calcNetPay (emp_ptr, SIZE);
  213.  
  214. // Keep a running sum of the employee totals
  215. // Note the & to specify the address of the employeeTotals
  216. // structure. Needed since pointers work with addresses.
  217. calcEmployeeTotals (emp_ptr,
  218. emp_totals_ptr,
  219. SIZE);
  220.  
  221. // Keep a running update of the employee minimum and maximum values
  222. calcEmployeeMinMax (emp_ptr,
  223. emp_minMax_ptr,
  224. SIZE);
  225. // Print the column headers
  226. printHeader();
  227.  
  228. // print out final information on each employee
  229. printEmp (emp_ptr, SIZE);
  230.  
  231. // TODO - Transition this call to using pointers.
  232. // Hint: Pass the address of these two structures
  233. // like it is being done with calcEmployeeTotals
  234. // and calcEmployeeMinMax.
  235.  
  236. // print the totals and averages for all float items
  237. printEmpStatistics (emp_totals_ptr,
  238. emp_minMax_ptr,
  239. SIZE);
  240.  
  241. return (0); // success
  242.  
  243. } // main
  244.  
  245. //**************************************************************
  246. // Function: getHours
  247. //
  248. // Purpose: Obtains input from user, the number of hours worked
  249. // per employee and updates it in the array of structures
  250. // for each employee.
  251. //
  252. // Parameters:
  253. //
  254. // emp_ptr - pointer to array of employees (i.e., struct employee)
  255. // theSize - the array size (i.e., number of employees)
  256. //
  257. // Returns: void (the employee hours gets updated by reference)
  258. //
  259. //**************************************************************
  260.  
  261. void getHours (struct employee * emp_ptr, int theSize)
  262. {
  263.  
  264. int i; // loop index
  265.  
  266. // read in hours for each employee
  267. for (i = 0; i < theSize; ++i)
  268. {
  269. // Read in hours for employee
  270. printf("\nEnter hours worked by emp # %06li: ", emp_ptr->clockNumber);
  271. scanf ("%f", &emp_ptr->hours);
  272.  
  273. // set pointer to next employee
  274. ++emp_ptr;
  275. }
  276.  
  277. } // getHours
  278.  
  279. //**************************************************************
  280. // Function: printHeader
  281. //
  282. // Purpose: Prints the initial table header information.
  283. //
  284. // Parameters: none
  285. //
  286. // Returns: void
  287. //
  288. //**************************************************************
  289.  
  290. void printHeader (void)
  291. {
  292.  
  293. printf ("\n\n*** Pay Calculator ***\n");
  294.  
  295. // print the table header
  296. printf("\n--------------------------------------------------------------");
  297. printf("-------------------");
  298. printf("\nName Tax Clock# Wage Hours OT Gross ");
  299. printf(" State Fed Net");
  300. printf("\n State Pay ");
  301. printf(" Tax Tax Pay");
  302.  
  303. printf("\n--------------------------------------------------------------");
  304. printf("-------------------");
  305.  
  306. } // printHeader
  307.  
  308. //*************************************************************
  309. // Function: printEmp
  310. //
  311. // Purpose: Prints out all the information for each employee
  312. // in a nice and orderly table format.
  313. //
  314. // Parameters:
  315. //
  316. // emp_ptr - pointer to array of struct employee
  317. // theSize - the array size (i.e., number of employees)
  318. //
  319. // Returns: void
  320. //
  321. //**************************************************************
  322.  
  323. void printEmp (struct employee * emp_ptr, int theSize)
  324. {
  325.  
  326. int i; // array and loop index
  327.  
  328. // Used to format the employee name
  329. char name [FIRST_NAME_SIZE + LAST_NAME_SIZE + 1];
  330.  
  331. // read in hours for each employee
  332. for (i = 0; i < theSize; ++i)
  333. {
  334. // While you could just print the first and last name in the printf
  335. // statement that follows, you could also use various C string library
  336. // functions to format the name exactly the way you want it. Breaking
  337. // the name into first and last members additionally gives you some
  338. // flexibility in printing. This also becomes more useful if we decide
  339. // later to store other parts of a person's name. I really did this just
  340. // to show you how to work with some of the common string functions.
  341. strcpy (name, emp_ptr->empName.firstName);
  342. strcat (name, " "); // add a space between first and last names
  343. strcat (name, emp_ptr->empName.lastName);
  344.  
  345. // Print out a single employee
  346. printf("\n%-20.20s %-2.2s %06li %5.2f %4.1f %4.1f %7.2f %6.2f %7.2f %8.2f",
  347. name, emp_ptr->taxState, emp_ptr->clockNumber,
  348. emp_ptr->wageRate, emp_ptr->hours,
  349. emp_ptr->overtimeHrs, emp_ptr->grossPay,
  350. emp_ptr->stateTax, emp_ptr->fedTax,
  351. emp_ptr->netPay);
  352.  
  353. // set pointer to next employee
  354. ++emp_ptr;
  355.  
  356. } // for
  357.  
  358. } // printEmp
  359.  
  360. //*************************************************************
  361. // Function: printEmpStatistics
  362. //
  363. // Purpose: Prints out the summary totals and averages of all
  364. // floating point value items for all employees
  365. // that have been processed. It also prints
  366. // out the min and max values.
  367. //
  368. // Parameters:
  369. //
  370. // employeeTotals - a structure containing a running total
  371. // of all employee floating point items
  372. // employeeMinMax - a structure containing all the minimum
  373. // and maximum values of all employee
  374. // floating point items
  375. // theSize - the total number of employees processed, used
  376. // to check for zero or negative divide condition.
  377. //
  378. // Returns: void
  379. //
  380. //**************************************************************
  381.  
  382. // TODO - Transition this function from Structure references to
  383. // Pointer references. Two steps are needed:
  384. //
  385. // 1) Change both structure parameters to pointers (use
  386. // emp_totals_ptr and emp_MinMax_ptr).
  387. //
  388. // 2) Change all structures references to pointer references
  389. // within all places inside the function body.
  390. //
  391. // For example, instead of employeeTotals.total_wageRate
  392. // ... use emp_totals_ptr->total_wageRate
  393. // and instead of employeeMinMax.min_wageRate
  394. // ... use emp_MinMax_ptr->min_wageRate
  395.  
  396. void printEmpStatistics (struct totals * emp_ptr,
  397. struct min_max * emp_MinMax_ptr,
  398. int theSize)
  399. {
  400.  
  401. // print a separator line
  402. printf("\n--------------------------------------------------------------");
  403. printf("-------------------");
  404.  
  405. // print the totals for all the floating point fields
  406. printf("\nTotals: %5.2f %5.1f %5.1f %7.2f %6.2f %7.2f %8.2f",
  407. emp_ptr->total_wageRate,
  408. emp_ptr->total_hours,
  409. emp_ptr->total_overtimeHrs,
  410. emp_ptr->total_grossPay,
  411. emp_ptr->total_stateTax,
  412. emp_ptr->total_fedTax,
  413. emp_ptr->total_netPay);
  414.  
  415. // make sure you don't divide by zero or a negative number
  416. if (theSize > 0)
  417. {
  418. // print the averages for all the floating point fields
  419. printf("\nAverages: %5.2f %5.1f %5.1f %7.2f %6.2f %7.2f %8.2f",
  420. emp_ptr->total_wageRate/theSize,
  421. emp_ptr->total_hours/theSize,
  422. emp_ptr->total_overtimeHrs/theSize,
  423. emp_ptr->total_grossPay/theSize,
  424. emp_ptr->total_stateTax/theSize,
  425. emp_ptr->total_fedTax/theSize,
  426. emp_ptr->total_netPay/theSize);
  427. } // if
  428.  
  429. // print the min and max values
  430.  
  431. printf("\nMinimum: %5.2f %5.1f %5.1f %7.2f %6.2f %7.2f %8.2f",
  432. emp_MinMax_ptr->min_wageRate,
  433. emp_MinMax_ptr->min_hours,
  434. emp_MinMax_ptr->min_overtimeHrs,
  435. emp_MinMax_ptr->min_grossPay,
  436. emp_MinMax_ptr->min_stateTax,
  437. emp_MinMax_ptr->min_fedTax,
  438. emp_MinMax_ptr->min_netPay);
  439.  
  440. printf("\nMaximum: %5.2f %5.1f %5.1f %7.2f %6.2f %7.2f %8.2f",
  441. emp_MinMax_ptr->max_wageRate,
  442. emp_MinMax_ptr->max_hours,
  443. emp_MinMax_ptr->max_overtimeHrs,
  444. emp_MinMax_ptr->max_grossPay,
  445. emp_MinMax_ptr->max_stateTax,
  446. emp_MinMax_ptr->max_fedTax,
  447. emp_MinMax_ptr->max_netPay);
  448.  
  449. } // printEmpStatistics
  450.  
  451. //*************************************************************
  452. // Function: calcOvertimeHrs
  453. //
  454. // Purpose: Calculates the overtime hours worked by an employee
  455. // in a given week for each employee.
  456. //
  457. // Parameters:
  458. //
  459. // employeeData - array of employees (i.e., struct employee)
  460. // theSize - the array size (i.e., number of employees)
  461. //
  462. // Returns: void (the overtime hours gets updated by reference)
  463. //
  464. //**************************************************************
  465.  
  466. // TODO - Transition this function from Array references to
  467. // Pointer references. Perform these three (3) steps:
  468. //
  469. // 1) Change the employeeData parameter to a pointer (emp_ptr)
  470. // 2) Change all array references in function body to pointer
  471. // references (use emp_ptr).
  472. // 3) Increment emp_ptr just before the end of the loop
  473. // to access the next employee
  474. //
  475. // Note: Review how it was done already in the getHours function
  476.  
  477. void calcOvertimeHrs (struct employee * emp_ptr, int theSize)
  478. {
  479.  
  480. int i; // array and loop index
  481.  
  482. // calculate overtime hours for each employee
  483. for (i = 0; i < theSize; ++i)
  484. {
  485. // Any overtime ?
  486. if (emp_ptr->hours >= STD_HOURS)
  487. {
  488. emp_ptr->overtimeHrs = emp_ptr->hours - STD_HOURS;
  489. }
  490. else // no overtime
  491. {
  492. emp_ptr->overtimeHrs = 0;
  493. }
  494.  
  495. emp_ptr++;
  496.  
  497. }
  498.  
  499. } // calcOvertimeHrs
  500.  
  501. //*************************************************************
  502. // Function: calcGrossPay
  503. //
  504. // Purpose: Calculates the gross pay based on the the normal pay
  505. // and any overtime pay for a given week for each
  506. // employee.
  507. //
  508. // Parameters:
  509. //
  510. // employeeData - array of employees (i.e., struct employee)
  511. // theSize - the array size (i.e., number of employees)
  512. //
  513. // Returns: void (the gross pay gets updated by reference)
  514. //
  515. //**************************************************************
  516.  
  517. // TODO - Transition this function from Array references to
  518. // Pointer references. Perform these three (3) steps:
  519. //
  520. // 1) Change the employeeData parameter to a pointer (emp_ptr)
  521. // 2) Change all array references in function body to pointer
  522. // references (use emp_ptr).
  523. // 3) Increment emp_ptr just before the end of the loop
  524. // to access the next employee
  525. //
  526. // Note: Review how it was done already in the getHours function
  527.  
  528. void calcGrossPay (struct employee * emp_ptr, int theSize)
  529. {
  530. int i; // loop and array index
  531. float theNormalPay; // normal pay without any overtime hours
  532. float theOvertimePay; // overtime pay
  533.  
  534. // calculate grossPay for each employee
  535. for (i=0; i < theSize; ++i)
  536. {
  537. // calculate normal pay and any overtime pay
  538. theNormalPay = emp_ptr->wageRate *
  539. (emp_ptr->hours - emp_ptr->overtimeHrs);
  540. theOvertimePay = emp_ptr->overtimeHrs *
  541. (OT_RATE * emp_ptr->wageRate);
  542.  
  543. // calculate gross pay for employee as normalPay + any overtime pay
  544. emp_ptr->grossPay = theNormalPay + theOvertimePay;
  545.  
  546. emp_ptr++;
  547. }
  548.  
  549. } // calcGrossPay
  550.  
  551. //*************************************************************
  552. // Function: calcStateTax
  553. //
  554. // Purpose: Calculates the State Tax owed based on gross pay
  555. // for each employee. State tax rate is based on the
  556. // the designated tax state based on where the
  557. // employee is actually performing the work. Each
  558. // state decides their tax rate.
  559. //
  560. // Parameters:
  561. //
  562. // employeeData - array of employees (i.e., struct employee)
  563. // theSize - the array size (i.e., number of employees)
  564. //
  565. // Returns: void (the state tax gets updated by reference)
  566. //
  567. //**************************************************************
  568.  
  569. // TODO - Transition this function from Array references to
  570. // Pointer references. Perform these three (3) steps:
  571. //
  572. // 1) Change the employeeData parameter to a pointer (emp_ptr)
  573. // 2) Change all array references in function body to pointer
  574. // references (use emp_ptr).
  575. // 3) Increment emp_ptr just before the end of the loop
  576. // to access the next employee
  577. //
  578. // Note: Review how it was done already in the getHours function
  579.  
  580. void calcStateTax (struct employee * emp_ptr, int theSize)
  581. {
  582.  
  583. int i; // loop and array index
  584.  
  585. // calculate state tax based on where employee works
  586. for (i=0; i < theSize; ++i)
  587. {
  588. // Make sure tax state is all uppercase
  589. if (islower(emp_ptr->taxState[0]))
  590. emp_ptr->taxState[0] = toupper(emp_ptr->taxState[0]);
  591. if (islower(emp_ptr->taxState[1]))
  592. emp_ptr->taxState[1] = toupper(emp_ptr->taxState[1]);
  593.  
  594. // calculate state tax based on where employee resides
  595. if (strcmp(emp_ptr->taxState, "MA") == 0)
  596. emp_ptr->stateTax = emp_ptr->grossPay * MA_TAX_RATE;
  597. else if (strcmp(emp_ptr->taxState, "VT") == 0)
  598. emp_ptr->stateTax = emp_ptr->grossPay * VT_TAX_RATE;
  599. else if (strcmp(emp_ptr->taxState, "NH") == 0)
  600. emp_ptr->stateTax = emp_ptr->grossPay * NH_TAX_RATE;
  601. else if (strcmp(emp_ptr->taxState, "CA") == 0)
  602. emp_ptr->stateTax = emp_ptr->grossPay * CA_TAX_RATE;
  603. else
  604. // any other state is the default rate
  605. emp_ptr->stateTax = emp_ptr->grossPay * DEFAULT_TAX_RATE;
  606.  
  607. emp_ptr++;
  608. } // for
  609.  
  610. } // calcStateTax
  611.  
  612. //*************************************************************
  613. // Function: calcFedTax
  614. //
  615. // Purpose: Calculates the Federal Tax owed based on the gross
  616. // pay for each employee
  617. //
  618. // Parameters:
  619. //
  620. // employeeData - array of employees (i.e., struct employee)
  621. // theSize - the array size (i.e., number of employees)
  622. //
  623. // Returns: void (the federal tax gets updated by reference)
  624. //
  625. //**************************************************************
  626.  
  627. // TODO - Transition this function from Array references to
  628. // Pointer references. Perform these three (3) steps:
  629. //
  630. // 1) Change the employeeData parameter to a pointer (emp_ptr)
  631. // 2) Change all array references in function body to pointer
  632. // references (use emp_ptr).
  633. // 3) Increment emp_ptr just before the end of the loop
  634. // to access the next employee
  635. //
  636. // Note: Review how it was done already in the getHours function
  637.  
  638. void calcFedTax (struct employee * emp_ptr, int theSize)
  639. {
  640.  
  641. int i; // loop and array index
  642.  
  643. // calculate the federal tax for each employee
  644. for (i=0; i < theSize; ++i)
  645. {
  646. // Fed Tax is the same for all regardless of state
  647. emp_ptr->fedTax = emp_ptr->grossPay * FED_TAX_RATE;
  648.  
  649. emp_ptr++;
  650.  
  651. } // for
  652.  
  653. } // calcFedTax
  654.  
  655. //*************************************************************
  656. // Function: calcNetPay
  657. //
  658. // Purpose: Calculates the net pay as the gross pay minus any
  659. // state and federal taxes owed for each employee.
  660. // Essentially, their "take home" pay.
  661. //
  662. // Parameters:
  663. //
  664. // employeeData - array of employees (i.e., struct employee)
  665. // theSize - the array size (i.e., number of employees)
  666. //
  667. // Returns: void (the net pay gets updated by reference)
  668. //
  669. //**************************************************************
  670.  
  671. // TODO - Transition this function from Array references to
  672. // Pointer references. Perform these three (3) steps:
  673. //
  674. // 1) Change the employeeData parameter to a pointer (emp_ptr)
  675. // 2) Change all array references in function body to pointer
  676. // references (use emp_ptr).
  677. // 3) Increment emp_ptr just before the end of the loop
  678. // to access the next employee
  679. //
  680. // Note: Review how it was done already in the getHours function
  681.  
  682. void calcNetPay (struct employee * emp_ptr, int theSize)
  683. {
  684. int i; // loop and array index
  685. float theTotalTaxes; // the total state and federal tax
  686.  
  687. // calculate the take home pay for each employee
  688. for (i=0; i < theSize; ++i)
  689. {
  690. // calculate the total state and federal taxes
  691. theTotalTaxes = emp_ptr->stateTax + emp_ptr->fedTax;
  692.  
  693. // calculate the net pay
  694. emp_ptr->netPay = emp_ptr->grossPay - theTotalTaxes;
  695.  
  696. emp_ptr++;
  697.  
  698. } // for
  699.  
  700. } // calcNetPay
  701.  
  702. //*************************************************************
  703. // Function: calcEmployeeTotals
  704. //
  705. // Purpose: Performs a running total (sum) of each employee
  706. // floating point member in the array of structures
  707. //
  708. // Parameters:
  709. //
  710. // emp_ptr - pointer to array of employees (structure)
  711. // emp_totals_ptr - pointer to a structure containing the
  712. // running totals of all floating point
  713. // members in the array of employee structure
  714. // that is accessed and referenced by emp_ptr
  715. // theSize - the array size (i.e., number of employees)
  716. //
  717. // Returns:
  718. //
  719. // void (the employeeTotals structure gets updated by reference)
  720. //
  721. //**************************************************************
  722.  
  723. void calcEmployeeTotals (struct employee * emp_ptr,
  724. struct totals * emp_totals_ptr,
  725. int theSize)
  726. {
  727.  
  728. int i; // loop index
  729.  
  730. // total up each floating point item for all employees
  731. for (i = 0; i < theSize; ++i)
  732. {
  733. // add current employee data to our running totals
  734. emp_totals_ptr->total_wageRate += emp_ptr->wageRate;
  735. emp_totals_ptr->total_hours += emp_ptr->hours;
  736. emp_totals_ptr->total_overtimeHrs += emp_ptr->overtimeHrs;
  737. emp_totals_ptr->total_grossPay += emp_ptr->grossPay;
  738. emp_totals_ptr->total_stateTax += emp_ptr->stateTax;
  739. emp_totals_ptr->total_fedTax += emp_ptr->fedTax;
  740. emp_totals_ptr->total_netPay += emp_ptr->netPay;
  741.  
  742. // go to next employee in our array of structures
  743. // Note: We don't need to increment the emp_totals_ptr
  744. // because it is not an array
  745. ++emp_ptr;
  746.  
  747. } // for
  748.  
  749. // no need to return anything since we used pointers and have
  750. // been referring the array of employee structure and the
  751. // the total structure from its calling function ... this
  752. // is the power of Call by Reference.
  753.  
  754. } // calcEmployeeTotals
  755.  
  756. //*************************************************************
  757. // Function: calcEmployeeMinMax
  758. //
  759. // Purpose: Accepts various floating point values from an
  760. // employee and adds to a running update of min
  761. // and max values
  762. //
  763. // Parameters:
  764. //
  765. // employeeData - array of employees (i.e., struct employee)
  766. // employeeTotals - structure containing a running totals
  767. // of all fields above
  768. // theSize - the array size (i.e., number of employees)
  769. //
  770. // Returns:
  771. //
  772. // employeeMinMax - updated employeeMinMax structure
  773. //
  774. //**************************************************************
  775.  
  776. void calcEmployeeMinMax (struct employee * emp_ptr,
  777. struct min_max * emp_minMax_ptr,
  778. int theSize)
  779. {
  780.  
  781. int i; // loop index
  782.  
  783. // At this point, emp_ptr is pointing to the first
  784. // employee which is located in the first element
  785. // of our employee array of structures (employeeData).
  786.  
  787. // As this is the first employee, set each min
  788. // min and max value using our emp_minMax_ptr
  789. // to the associated member fields below. They
  790. // will become the initial baseline that we
  791. // can check and update if needed against the
  792. // remaining employees.
  793.  
  794. // set the min to the first employee members
  795. emp_minMax_ptr->min_wageRate = emp_ptr->wageRate;
  796. emp_minMax_ptr->min_hours = emp_ptr->hours;
  797. emp_minMax_ptr->min_overtimeHrs = emp_ptr->overtimeHrs;
  798. emp_minMax_ptr->min_grossPay = emp_ptr->grossPay;
  799. emp_minMax_ptr->min_stateTax = emp_ptr->stateTax;
  800. emp_minMax_ptr->min_fedTax = emp_ptr->fedTax;
  801. emp_minMax_ptr->min_netPay = emp_ptr->netPay;
  802.  
  803. // set the max to the first employee members
  804. emp_minMax_ptr->max_wageRate = emp_ptr->wageRate;
  805. emp_minMax_ptr->max_hours = emp_ptr->hours;
  806. emp_minMax_ptr->max_overtimeHrs = emp_ptr->overtimeHrs;
  807. emp_minMax_ptr->max_grossPay = emp_ptr->grossPay;
  808. emp_minMax_ptr->max_stateTax = emp_ptr->stateTax;
  809. emp_minMax_ptr->max_fedTax = emp_ptr->fedTax;
  810. emp_minMax_ptr->max_netPay = emp_ptr->netPay;
  811.  
  812. // compare the rest of the employees to each other for min and max
  813. for (i = 1; i < theSize; ++i)
  814. {
  815.  
  816. // go to next employee in our array of structures
  817. // Note: We don't need to increment the emp_totals_ptr
  818. // because it is not an array
  819. ++emp_ptr;
  820.  
  821. // check if current Wage Rate is the new min and/or max
  822. if (emp_ptr->wageRate < emp_minMax_ptr->min_wageRate)
  823. {
  824. emp_minMax_ptr->min_wageRate = emp_ptr->wageRate;
  825. }
  826.  
  827. if (emp_ptr->wageRate > emp_minMax_ptr->max_wageRate)
  828. {
  829. emp_minMax_ptr->max_wageRate = emp_ptr->wageRate;
  830. }
  831.  
  832. // check is current Hours is the new min and/or max
  833. if (emp_ptr->hours < emp_minMax_ptr->min_hours)
  834. {
  835. emp_minMax_ptr->min_hours = emp_ptr->hours;
  836. }
  837.  
  838. if (emp_ptr->hours > emp_minMax_ptr->max_hours)
  839. {
  840. emp_minMax_ptr->max_hours = emp_ptr->hours;
  841. }
  842.  
  843. // check is current Overtime Hours is the new min and/or max
  844. if (emp_ptr->overtimeHrs < emp_minMax_ptr->min_overtimeHrs)
  845. {
  846. emp_minMax_ptr->min_overtimeHrs = emp_ptr->overtimeHrs;
  847. }
  848.  
  849. if (emp_ptr->overtimeHrs > emp_minMax_ptr->max_overtimeHrs)
  850. {
  851. emp_minMax_ptr->max_overtimeHrs = emp_ptr->overtimeHrs;
  852. }
  853.  
  854. // check is current Gross Pay is the new min and/or max
  855. if (emp_ptr->grossPay < emp_minMax_ptr->min_grossPay)
  856. {
  857. emp_minMax_ptr->min_grossPay = emp_ptr->grossPay;
  858. }
  859.  
  860. if (emp_ptr->grossPay > emp_minMax_ptr->max_grossPay)
  861. {
  862. emp_minMax_ptr->max_grossPay = emp_ptr->grossPay;
  863. }
  864.  
  865. // check is current State Tax is the new min and/or max
  866. if (emp_ptr->stateTax < emp_minMax_ptr->min_stateTax)
  867. {
  868. emp_minMax_ptr->min_stateTax = emp_ptr->stateTax;
  869. }
  870.  
  871. if (emp_ptr->stateTax > emp_minMax_ptr->max_stateTax)
  872. {
  873. emp_minMax_ptr->max_stateTax = emp_ptr->stateTax;
  874. }
  875.  
  876. // check is current Federal Tax is the new min and/or max
  877. if (emp_ptr->fedTax < emp_minMax_ptr->min_fedTax)
  878. {
  879. emp_minMax_ptr->min_fedTax = emp_ptr->fedTax;
  880. }
  881.  
  882. if (emp_ptr->fedTax > emp_minMax_ptr->max_fedTax)
  883. {
  884. emp_minMax_ptr->max_fedTax = emp_ptr->fedTax;
  885. }
  886.  
  887. // check is current Net Pay is the new min and/or max
  888. if (emp_ptr->netPay < emp_minMax_ptr->min_netPay)
  889. {
  890. emp_minMax_ptr->min_netPay = emp_ptr->netPay;
  891. }
  892.  
  893. if (emp_ptr->netPay > emp_minMax_ptr->max_netPay)
  894. {
  895. emp_minMax_ptr->max_netPay = emp_ptr->netPay;
  896. }
  897.  
  898. } // else if
  899.  
  900. // no need to return anything since we used pointers and have
  901. // been referencing the employeeData structure and the
  902. // the employeeMinMax structure from its calling function ...
  903. // this is the power of Call by Reference.
  904.  
  905. } // calcEmployeeMinMax
Success #stdin #stdout 0.01s 5292KB
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