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