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