fork 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 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. // of a name: Middle, Nickname, Prefix, Suffix, etc.
  49. struct name
  50. {
  51. char firstName[FIRST_NAME_SIZE]; // first name of employee
  52. char lastName [LAST_NAME_SIZE]; // last name of employee
  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; // holds first and last name
  62. char taxState [TAX_STATE_SIZE]; // state where work is performed
  63. long int clockNumber; // unique employee id number
  64. float wageRate; // hourly pay rate
  65. float hours; // total hours worked this week
  66. float overtimeHrs; // hours worked over 40
  67. float grossPay; // total pay before taxes
  68. float stateTax; // state tax amount owed
  69. float fedTax; // federal tax amount owed
  70. float netPay; // take home pay after taxes
  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; // sum of all employee wage rates
  78. float total_hours; // sum of all hours worked
  79. float total_overtimeHrs; // sum of all overtime hours
  80. float total_grossPay; // sum of all gross pay amounts
  81. float total_stateTax; // sum of all state taxes
  82. float total_fedTax; // sum of all federal taxes
  83. float total_netPay; // sum of all net pay amounts
  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; // lowest wage rate found
  91. float min_hours; // fewest hours worked
  92. float min_overtimeHrs; // least overtime hours
  93. float min_grossPay; // lowest gross pay
  94. float min_stateTax; // lowest state tax
  95. float min_fedTax; // lowest federal tax
  96. float min_netPay; // lowest net pay
  97. float max_wageRate; // highest wage rate found
  98. float max_hours; // most hours worked
  99. float max_overtimeHrs; // most overtime hours
  100. float max_grossPay; // highest gross pay
  101. float max_stateTax; // highest state tax
  102. float max_fedTax; // highest federal tax
  103. float max_netPay; // highest net pay
  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. // transitioned from array parameter to pointer parameter
  124. void calcOvertimeHrs (struct employee * emp_ptr, int theSize);
  125. void calcGrossPay (struct employee * emp_ptr, int theSize);
  126. void calcStateTax (struct employee * emp_ptr, int theSize);
  127. void calcFedTax (struct employee * emp_ptr, int theSize);
  128. void calcNetPay (struct employee * emp_ptr, int theSize);
  129.  
  130. // transitioned both structure parameters to pointers
  131. void printEmpStatistics (struct totals * emp_totals_ptr,
  132. struct min_max * emp_MinMax_ptr,
  133. int theSize);
  134.  
  135. int main ()
  136. {
  137.  
  138. // Set up a local variable to store the employee information
  139. // Initialize the name, tax state, clock number, and wage rate
  140. struct employee employeeData[SIZE] = {
  141. { {"Connie", "Cobol"}, "MA", 98401, 10.60},
  142. { {"Mary", "Apl"}, "NH", 526488, 9.75 },
  143. { {"Frank", "Fortran"}, "VT", 765349, 10.50 },
  144. { {"Jeff", "Ada"}, "NY", 34645, 12.25 },
  145. { {"Anton", "Pascal"},"CA",127615, 8.35 }
  146. };
  147.  
  148. // declare a pointer to the array of employee structures
  149. struct employee * emp_ptr;
  150.  
  151. // set the pointer to point to the array of employees
  152. emp_ptr = employeeData;
  153.  
  154. // set up structure to store totals and initialize all to zero
  155. struct totals employeeTotals = {0,0,0,0,0,0,0};
  156.  
  157. // pointer to the employeeTotals structure
  158. struct totals * emp_totals_ptr = &employeeTotals;
  159.  
  160. // set up structure to store min and max values and initialize all to zero
  161. struct min_max employeeMinMax = {0,0,0,0,0,0,0,0,0,0,0,0,0,0};
  162.  
  163. // pointer to the employeeMinMax structure
  164. struct min_max * emp_minMax_ptr = &employeeMinMax;
  165.  
  166. // Call functions as needed to read and calculate information
  167.  
  168. // Prompt for the number of hours worked by the employee
  169. getHours (emp_ptr, SIZE);
  170.  
  171. // Calculate the overtime hours
  172. calcOvertimeHrs (emp_ptr, SIZE);
  173.  
  174. // Calculate the weekly gross pay
  175. calcGrossPay (emp_ptr, SIZE);
  176.  
  177. // Calculate the state tax
  178. calcStateTax (emp_ptr, SIZE);
  179.  
  180. // Calculate the federal tax
  181. calcFedTax (emp_ptr, SIZE);
  182.  
  183. // Calculate the net pay after taxes
  184. calcNetPay (emp_ptr, SIZE);
  185.  
  186. // Keep a running sum of the employee totals
  187. // Note the & to specify the address of the employeeTotals
  188. // structure. Needed since pointers work with addresses.
  189. calcEmployeeTotals (emp_ptr,
  190. emp_totals_ptr,
  191. SIZE);
  192.  
  193. // Keep a running update of the employee minimum and maximum values
  194. calcEmployeeMinMax (emp_ptr,
  195. emp_minMax_ptr,
  196. SIZE);
  197.  
  198. // Print the column headers
  199. printHeader();
  200.  
  201. // print out final information on each employee
  202. printEmp (emp_ptr, SIZE);
  203.  
  204. // passing pointers to both summary structures
  205. // similar to how calcEmployeeTotals and calcEmployeeMinMax are called above
  206.  
  207. // print the totals and averages for all float items
  208. printEmpStatistics (emp_totals_ptr,
  209. emp_minMax_ptr,
  210. SIZE);
  211.  
  212. return (0); // success
  213.  
  214. } // main
  215.  
  216. //**************************************************************
  217. // Function: getHours
  218. //
  219. // Purpose: Obtains input from user, the number of hours worked
  220. // per employee and updates it in the array of structures
  221. // for each employee.
  222. //
  223. // Parameters:
  224. //
  225. // emp_ptr - pointer to array of employees (i.e., struct employee)
  226. // theSize - the array size (i.e., number of employees)
  227. //
  228. // Returns: void (the employee hours gets updated by reference)
  229. //
  230. //**************************************************************
  231.  
  232. void getHours (struct employee * emp_ptr, int theSize)
  233. {
  234.  
  235. int i; // loop index
  236.  
  237. // read in hours for each employee
  238. for (i = 0; i < theSize; ++i)
  239. {
  240. // Read in hours for employee
  241. printf("\nEnter hours worked by emp # %06li: ", emp_ptr->clockNumber);
  242. scanf ("%f", &emp_ptr->hours);
  243.  
  244. // set pointer to next employee
  245. ++emp_ptr;
  246. }
  247.  
  248. } // getHours
  249.  
  250. //**************************************************************
  251. // Function: printHeader
  252. //
  253. // Purpose: Prints the initial table header information.
  254. //
  255. // Parameters: none
  256. //
  257. // Returns: void
  258. //
  259. //**************************************************************
  260.  
  261. void printHeader (void)
  262. {
  263.  
  264. printf ("\n\n*** Pay Calculator ***\n");
  265.  
  266. // print the table header
  267. printf("\n--------------------------------------------------------------");
  268. printf("-------------------");
  269. printf("\nName Tax Clock# Wage Hours OT Gross ");
  270. printf(" State Fed Net");
  271. printf("\n State Pay ");
  272. printf(" Tax Tax Pay");
  273.  
  274. printf("\n--------------------------------------------------------------");
  275. printf("-------------------");
  276.  
  277. } // printHeader
  278.  
  279. //*************************************************************
  280. // Function: printEmp
  281. //
  282. // Purpose: Prints out all the information for each employee
  283. // in a nice and orderly table format.
  284. //
  285. // Parameters:
  286. //
  287. // emp_ptr - pointer to array of struct employee
  288. // theSize - the array size (i.e., number of employees)
  289. //
  290. // Returns: void
  291. //
  292. //**************************************************************
  293.  
  294. void printEmp (struct employee * emp_ptr, int theSize)
  295. {
  296.  
  297. int i; // loop index
  298.  
  299. // used to build the full name string for printing
  300. char name [FIRST_NAME_SIZE + LAST_NAME_SIZE + 1];
  301.  
  302. // print each employee one row at a time
  303. for (i = 0; i < theSize; ++i)
  304. {
  305. // While you could just print the first and last name in the printf
  306. // statement that follows, you could also use various C string library
  307. // functions to format the name exactly the way you want it. Breaking
  308. // the name into first and last members additionally gives you some
  309. // flexibility in printing. This also becomes more useful if we decide
  310. // later to store other parts of a person's name.
  311. strcpy (name, emp_ptr->empName.firstName);
  312. strcat (name, " "); // add a space between first and last names
  313. strcat (name, emp_ptr->empName.lastName);
  314.  
  315. // Print out a single employee
  316. printf("\n%-20.20s %-2.2s %06li %5.2f %4.1f %4.1f %7.2f %6.2f %7.2f %8.2f",
  317. name, emp_ptr->taxState, emp_ptr->clockNumber,
  318. emp_ptr->wageRate, emp_ptr->hours,
  319. emp_ptr->overtimeHrs, emp_ptr->grossPay,
  320. emp_ptr->stateTax, emp_ptr->fedTax,
  321. emp_ptr->netPay);
  322.  
  323. // set pointer to next employee
  324. ++emp_ptr;
  325.  
  326. } // for
  327.  
  328. } // printEmp
  329.  
  330. //*************************************************************
  331. // Function: printEmpStatistics
  332. //
  333. // Purpose: Prints out the summary totals and averages of all
  334. // floating point value items for all employees
  335. // that have been processed. It also prints
  336. // out the min and max values.
  337. //
  338. // Parameters:
  339. //
  340. // emp_totals_ptr - pointer to structure with running totals
  341. // of all employee floating point items
  342. // emp_MinMax_ptr - pointer to structure with the minimum
  343. // and maximum values of all employee
  344. // floating point items
  345. // theSize - the total number of employees processed,
  346. // used to check for zero or negative divide
  347. //
  348. // Returns: void
  349. //
  350. //**************************************************************
  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 values
  386. printf("\nMinimum: %5.2f %5.1f %5.1f %7.2f %6.2f %7.2f %8.2f",
  387. emp_MinMax_ptr->min_wageRate,
  388. emp_MinMax_ptr->min_hours,
  389. emp_MinMax_ptr->min_overtimeHrs,
  390. emp_MinMax_ptr->min_grossPay,
  391. emp_MinMax_ptr->min_stateTax,
  392. emp_MinMax_ptr->min_fedTax,
  393. emp_MinMax_ptr->min_netPay);
  394.  
  395. // print the max values
  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. void calcOvertimeHrs (struct employee * emp_ptr, int theSize)
  423. {
  424.  
  425. int i; // loop index
  426.  
  427. // calculate overtime hours for each employee
  428. for (i = 0; i < theSize; ++i)
  429. {
  430. // check if the employee worked more than the standard hours
  431. if (emp_ptr->hours >= STD_HOURS)
  432. {
  433. // overtime is everything over 40 hours
  434. emp_ptr->overtimeHrs = emp_ptr->hours - STD_HOURS;
  435. }
  436. else // no overtime this week
  437. {
  438. emp_ptr->overtimeHrs = 0;
  439. }
  440.  
  441. // move to the next employee
  442. ++emp_ptr;
  443.  
  444. } // for
  445.  
  446. } // calcOvertimeHrs
  447.  
  448. //*************************************************************
  449. // Function: calcGrossPay
  450. //
  451. // Purpose: Calculates the gross pay based on the the normal pay
  452. // and any overtime pay for a given week for each
  453. // employee.
  454. //
  455. // Parameters:
  456. //
  457. // emp_ptr - pointer to array of employees (i.e., struct employee)
  458. // theSize - the array size (i.e., number of employees)
  459. //
  460. // Returns: void (the gross pay gets updated by reference)
  461. //
  462. //**************************************************************
  463.  
  464. void calcGrossPay (struct employee * emp_ptr, int theSize)
  465. {
  466.  
  467. int i; // loop index
  468. float theNormalPay; // pay for non-overtime hours only
  469. float theOvertimePay; // extra pay earned for overtime hours
  470.  
  471. // calculate grossPay for each employee
  472. for (i = 0; i < theSize; ++i)
  473. {
  474. // normal pay is wage times hours worked minus any overtime hours
  475. theNormalPay = emp_ptr->wageRate *
  476. (emp_ptr->hours - emp_ptr->overtimeHrs);
  477.  
  478. // overtime pay is overtime hours times wage rate times OT multiplier
  479. theOvertimePay = emp_ptr->overtimeHrs *
  480. (OT_RATE * emp_ptr->wageRate);
  481.  
  482. // gross pay is normal pay plus any overtime pay earned
  483. emp_ptr->grossPay = theNormalPay + theOvertimePay;
  484.  
  485. // move to the 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. void calcStateTax (struct employee * emp_ptr, int theSize)
  511. {
  512.  
  513. int i; // loop index
  514.  
  515. // calculate state tax based on where employee works
  516. for (i = 0; i < theSize; ++i)
  517. {
  518. // make sure tax state letters are uppercase before comparing
  519. if (islower(emp_ptr->taxState[0]))
  520. emp_ptr->taxState[0] = toupper(emp_ptr->taxState[0]);
  521. if (islower(emp_ptr->taxState[1]))
  522. emp_ptr->taxState[1] = toupper(emp_ptr->taxState[1]);
  523.  
  524. // calculate state tax based on where employee resides
  525. if (strcmp(emp_ptr->taxState, "MA") == 0)
  526. emp_ptr->stateTax = emp_ptr->grossPay * MA_TAX_RATE;
  527. else if (strcmp(emp_ptr->taxState, "VT") == 0)
  528. emp_ptr->stateTax = emp_ptr->grossPay * VT_TAX_RATE;
  529. else if (strcmp(emp_ptr->taxState, "NH") == 0)
  530. emp_ptr->stateTax = emp_ptr->grossPay * NH_TAX_RATE;
  531. else if (strcmp(emp_ptr->taxState, "CA") == 0)
  532. emp_ptr->stateTax = emp_ptr->grossPay * CA_TAX_RATE;
  533. else
  534. // any other state uses the default rate
  535. emp_ptr->stateTax = emp_ptr->grossPay * DEFAULT_TAX_RATE;
  536.  
  537. // move to the next employee
  538. ++emp_ptr;
  539.  
  540. } // for
  541.  
  542. } // calcStateTax
  543.  
  544. //*************************************************************
  545. // Function: calcFedTax
  546. //
  547. // Purpose: Calculates the Federal Tax owed based on the gross
  548. // pay for each employee
  549. //
  550. // Parameters:
  551. //
  552. // emp_ptr - pointer to array of employees (i.e., struct employee)
  553. // theSize - the array size (i.e., number of employees)
  554. //
  555. // Returns: void (the federal tax gets updated by reference)
  556. //
  557. //**************************************************************
  558.  
  559. void calcFedTax (struct employee * emp_ptr, int theSize)
  560. {
  561.  
  562. int i; // loop index
  563.  
  564. // calculate the federal tax for each employee
  565. for (i = 0; i < theSize; ++i)
  566. {
  567. // federal tax rate is the same for everyone regardless of state
  568. emp_ptr->fedTax = emp_ptr->grossPay * FED_TAX_RATE;
  569.  
  570. // move to the next employee
  571. ++emp_ptr;
  572.  
  573. } // for
  574.  
  575. } // calcFedTax
  576.  
  577. //*************************************************************
  578. // Function: calcNetPay
  579. //
  580. // Purpose: Calculates the net pay as the gross pay minus any
  581. // state and federal taxes owed for each employee.
  582. // Essentially, their "take home" pay.
  583. //
  584. // Parameters:
  585. //
  586. // emp_ptr - pointer to array of employees (i.e., struct employee)
  587. // theSize - the array size (i.e., number of employees)
  588. //
  589. // Returns: void (the net pay gets updated by reference)
  590. //
  591. //**************************************************************
  592.  
  593. void calcNetPay (struct employee * emp_ptr, int theSize)
  594. {
  595.  
  596. int i; // loop index
  597. float theTotalTaxes; // combined state and federal tax for one employee
  598.  
  599. // calculate the take home pay for each employee
  600. for (i = 0; i < theSize; ++i)
  601. {
  602. // add up both taxes together first
  603. theTotalTaxes = emp_ptr->stateTax + emp_ptr->fedTax;
  604.  
  605. // net pay is what is left over after taxes are taken out
  606. emp_ptr->netPay = emp_ptr->grossPay - theTotalTaxes;
  607.  
  608. // move to the next employee
  609. ++emp_ptr;
  610.  
  611. } // for
  612.  
  613. } // calcNetPay
  614.  
  615. //*************************************************************
  616. // Function: calcEmployeeTotals
  617. //
  618. // Purpose: Performs a running total (sum) of each employee
  619. // floating point member in the array of structures
  620. //
  621. // Parameters:
  622. //
  623. // emp_ptr - pointer to array of employees (structure)
  624. // emp_totals_ptr - pointer to a structure containing the
  625. // running totals of all floating point
  626. // members in the array of employee structure
  627. // that is accessed and referenced by emp_ptr
  628. // theSize - the array size (i.e., number of employees)
  629. //
  630. // Returns:
  631. //
  632. // void (the employeeTotals structure gets updated by reference)
  633. //
  634. //**************************************************************
  635.  
  636. void calcEmployeeTotals (struct employee * emp_ptr,
  637. struct totals * emp_totals_ptr,
  638. int theSize)
  639. {
  640.  
  641. int i; // loop index
  642.  
  643. // total up each floating point item for all employees
  644. for (i = 0; i < theSize; ++i)
  645. {
  646. // add current employee data to our running totals
  647. emp_totals_ptr->total_wageRate += emp_ptr->wageRate;
  648. emp_totals_ptr->total_hours += emp_ptr->hours;
  649. emp_totals_ptr->total_overtimeHrs += emp_ptr->overtimeHrs;
  650. emp_totals_ptr->total_grossPay += emp_ptr->grossPay;
  651. emp_totals_ptr->total_stateTax += emp_ptr->stateTax;
  652. emp_totals_ptr->total_fedTax += emp_ptr->fedTax;
  653. emp_totals_ptr->total_netPay += emp_ptr->netPay;
  654.  
  655. // go to next employee in our array of structures
  656. // Note: We don't need to increment the emp_totals_ptr
  657. // because it is not an array
  658. ++emp_ptr;
  659.  
  660. } // for
  661.  
  662. // no need to return anything since we used pointers and have
  663. // been referring the array of employee structure and the
  664. // the total structure from its calling function ... this
  665. // is the power of Call by Reference.
  666.  
  667. } // calcEmployeeTotals
  668.  
  669. //*************************************************************
  670. // Function: calcEmployeeMinMax
  671. //
  672. // Purpose: Accepts various floating point values from an
  673. // employee and adds to a running update of min
  674. // and max values
  675. //
  676. // Parameters:
  677. //
  678. // emp_ptr - pointer to array of employees (struct employee)
  679. // emp_minMax_ptr - pointer to structure containing the minimum
  680. // and maximum values of all employee floating
  681. // point items
  682. // theSize - the array size (i.e., number of employees)
  683. //
  684. // Returns:
  685. //
  686. // void (the employeeMinMax structure gets updated by reference)
  687. //
  688. //**************************************************************
  689.  
  690. void calcEmployeeMinMax (struct employee * emp_ptr,
  691. struct min_max * emp_minMax_ptr,
  692. int theSize)
  693. {
  694.  
  695. int i; // loop index - starts at 1 since first employee seeds the min/max
  696.  
  697. // At this point, emp_ptr is pointing to the first
  698. // employee which is located in the first element
  699. // of our employee array of structures (employeeData).
  700.  
  701. // As this is the first employee, set each min
  702. // and max value using our emp_minMax_ptr
  703. // to the associated member fields below. They
  704. // will become the initial baseline that we
  705. // can check and update if needed against the
  706. // remaining employees.
  707.  
  708. // set the min to the first employee members
  709. emp_minMax_ptr->min_wageRate = emp_ptr->wageRate;
  710. emp_minMax_ptr->min_hours = emp_ptr->hours;
  711. emp_minMax_ptr->min_overtimeHrs = emp_ptr->overtimeHrs;
  712. emp_minMax_ptr->min_grossPay = emp_ptr->grossPay;
  713. emp_minMax_ptr->min_stateTax = emp_ptr->stateTax;
  714. emp_minMax_ptr->min_fedTax = emp_ptr->fedTax;
  715. emp_minMax_ptr->min_netPay = emp_ptr->netPay;
  716.  
  717. // set the max to the first employee members
  718. emp_minMax_ptr->max_wageRate = emp_ptr->wageRate;
  719. emp_minMax_ptr->max_hours = emp_ptr->hours;
  720. emp_minMax_ptr->max_overtimeHrs = emp_ptr->overtimeHrs;
  721. emp_minMax_ptr->max_grossPay = emp_ptr->grossPay;
  722. emp_minMax_ptr->max_stateTax = emp_ptr->stateTax;
  723. emp_minMax_ptr->max_fedTax = emp_ptr->fedTax;
  724. emp_minMax_ptr->max_netPay = emp_ptr->netPay;
  725.  
  726. // compare the rest of the employees to each other for min and max
  727. for (i = 1; i < theSize; ++i)
  728. {
  729. // go to next employee in our array of structures
  730. // Note: We don't need to increment the emp_totals_ptr
  731. // because it is not an array
  732. ++emp_ptr;
  733.  
  734. // check if current Wage Rate is the new min and/or max
  735. if (emp_ptr->wageRate < emp_minMax_ptr->min_wageRate)
  736. {
  737. emp_minMax_ptr->min_wageRate = emp_ptr->wageRate;
  738. }
  739.  
  740. if (emp_ptr->wageRate > emp_minMax_ptr->max_wageRate)
  741. {
  742. emp_minMax_ptr->max_wageRate = emp_ptr->wageRate;
  743. }
  744.  
  745. // check is current Hours is the new min and/or max
  746. if (emp_ptr->hours < emp_minMax_ptr->min_hours)
  747. {
  748. emp_minMax_ptr->min_hours = emp_ptr->hours;
  749. }
  750.  
  751. if (emp_ptr->hours > emp_minMax_ptr->max_hours)
  752. {
  753. emp_minMax_ptr->max_hours = emp_ptr->hours;
  754. }
  755.  
  756. // check is current Overtime Hours is the new min and/or max
  757. if (emp_ptr->overtimeHrs < emp_minMax_ptr->min_overtimeHrs)
  758. {
  759. emp_minMax_ptr->min_overtimeHrs = emp_ptr->overtimeHrs;
  760. }
  761.  
  762. if (emp_ptr->overtimeHrs > emp_minMax_ptr->max_overtimeHrs)
  763. {
  764. emp_minMax_ptr->max_overtimeHrs = emp_ptr->overtimeHrs;
  765. }
  766.  
  767. // check is current Gross Pay is the new min and/or max
  768. if (emp_ptr->grossPay < emp_minMax_ptr->min_grossPay)
  769. {
  770. emp_minMax_ptr->min_grossPay = emp_ptr->grossPay;
  771. }
  772.  
  773. if (emp_ptr->grossPay > emp_minMax_ptr->max_grossPay)
  774. {
  775. emp_minMax_ptr->max_grossPay = emp_ptr->grossPay;
  776. }
  777.  
  778. // check is current State Tax is the new min and/or max
  779. if (emp_ptr->stateTax < emp_minMax_ptr->min_stateTax)
  780. {
  781. emp_minMax_ptr->min_stateTax = emp_ptr->stateTax;
  782. }
  783.  
  784. if (emp_ptr->stateTax > emp_minMax_ptr->max_stateTax)
  785. {
  786. emp_minMax_ptr->max_stateTax = emp_ptr->stateTax;
  787. }
  788.  
  789. // check is current Federal Tax is the new min and/or max
  790. if (emp_ptr->fedTax < emp_minMax_ptr->min_fedTax)
  791. {
  792. emp_minMax_ptr->min_fedTax = emp_ptr->fedTax;
  793. }
  794.  
  795. if (emp_ptr->fedTax > emp_minMax_ptr->max_fedTax)
  796. {
  797. emp_minMax_ptr->max_fedTax = emp_ptr->fedTax;
  798. }
  799.  
  800. // check is current Net Pay is the new min and/or max
  801. if (emp_ptr->netPay < emp_minMax_ptr->min_netPay)
  802. {
  803. emp_minMax_ptr->min_netPay = emp_ptr->netPay;
  804. }
  805.  
  806. if (emp_ptr->netPay > emp_minMax_ptr->max_netPay)
  807. {
  808. emp_minMax_ptr->max_netPay = emp_ptr->netPay;
  809. }
  810.  
  811. } // else if
  812.  
  813. // no need to return anything since we used pointers and have
  814. // been referencing the employeeData structure and the
  815. // the employeeMinMax structure from its calling function ...
  816. // this is the power of Call by Reference.
  817.  
  818. } // calcEmployeeMinMax
Success #stdin #stdout 0s 5316KB
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