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