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