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