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