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