fork download
  1. //********************************************************
  2. //
  3. // Assignment 8 - Structures and Strings and Pointers
  4. //
  5. // Name: Dominic DiTommaso
  6. //
  7. // Class: C Programming, Summer 2025
  8. //
  9. // Date: July 20, 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. void calcOvertimeHrs (struct employee * emp_ptr, int theSize);
  129. void calcGrossPay (struct employee * emp_ptr, int theSize);
  130. void calcStateTax (struct employee * emp_ptr, int theSize);
  131. void calcFedTax (struct employee * emp_ptr, int theSize);
  132. void calcNetPay (struct employee * emp_ptr, int theSize);
  133.  
  134.  
  135. void printEmpStatistics (struct totals * emp_totals_ptr,
  136. struct min_max * emp_minMax_ptr,
  137. int theSize);
  138. int main ()
  139. {
  140.  
  141. // Set up a local variable to store the employee information
  142. // Initialize the name, tax state, clock number, and wage rate
  143. struct employee employeeData[SIZE] = {
  144. { {"Connie", "Cobol"}, "MA", 98401, 10.60},
  145. { {"Mary", "Apl"}, "NH", 526488, 9.75 },
  146. { {"Frank", "Fortran"}, "VT", 765349, 10.50 },
  147. { {"Jeff", "Ada"}, "NY", 34645, 12.25 },
  148. { {"Anton", "Pascal"},"CA",127615, 8.35 }
  149. };
  150.  
  151. // declare a pointer to the array of employee structures
  152. struct employee * emp_ptr;
  153.  
  154. // set the pointer to point to the array of employees
  155. emp_ptr = employeeData;
  156.  
  157. // set up structure to store totals and initialize all to zero
  158. struct totals employeeTotals = {0,0,0,0,0,0,0};
  159.  
  160. // pointer to the employeeTotals structure
  161. struct totals * emp_totals_ptr = &employeeTotals;
  162.  
  163. // set up structure to store min and max values and initialize all to zero
  164. struct min_max employeeMinMax = {0,0,0,0,0,0,0,0,0,0,0,0,0,0};
  165.  
  166. // pointer to the employeeMinMax structure
  167. struct min_max * emp_minMax_ptr = &employeeMinMax;
  168.  
  169. // Call functions as needed to read and calculate information
  170.  
  171. // Prompt for the number of hours worked by the employee
  172. getHours (employeeData, SIZE);
  173.  
  174. // Calculate the overtime hours
  175. calcOvertimeHrs (employeeData, SIZE);
  176.  
  177. // Calculate the weekly gross pay
  178. calcGrossPay (employeeData, SIZE);
  179.  
  180. // Calculate the state tax
  181. calcStateTax (employeeData, SIZE);
  182.  
  183. // Calculate the federal tax
  184. calcFedTax (employeeData, SIZE);
  185.  
  186. // Calculate the net pay after taxes
  187. calcNetPay (employeeData, SIZE);
  188.  
  189. // Keep a running sum of the employee totals
  190. // Note the & to specify the address of the employeeTotals
  191. // structure. Needed since pointers work with addresses.
  192. calcEmployeeTotals (employeeData,
  193. &employeeTotals,
  194. SIZE);
  195.  
  196. // Keep a running update of the employee minimum and maximum values
  197. calcEmployeeMinMax (employeeData,
  198. &employeeMinMax,
  199. SIZE);
  200. // Print the column headers
  201. printHeader();
  202.  
  203. // print out final information on each employee
  204. printEmp (employeeData, SIZE);
  205.  
  206. // TODO - Transition this call to using pointers.
  207. // Hint: Pass the address of these two structures
  208. // like it is being done with calcEmployeeTotals
  209. // and calcEmployeeMinMax.
  210.  
  211. getHours(emp_ptr, SIZE);
  212. calcOvertimeHrs(emp_ptr, SIZE);
  213. calcGrossPay(emp_ptr, SIZE);
  214. calcStateTax(emp_ptr, SIZE);
  215. calcFedTax(emp_ptr, SIZE);
  216. calcNetPay(emp_ptr, SIZE);
  217.  
  218. // print the totals and averages for all float items
  219. printEmpStatistics(&employeeTotals, &employeeMinMax, SIZE);
  220.  
  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. void printEmpStatistics (struct totals * emp_totals_ptr,
  377. struct min_max * emp_MinMax_ptr,
  378. int theSize)
  379. {
  380.  
  381. // print a separator line
  382. printf("\n--------------------------------------------------------------");
  383. printf("-------------------");
  384.  
  385. // print the totals for all the floating point fields
  386. printf("\nTotals: %5.2f %5.1f %5.1f %7.2f %6.2f %7.2f %8.2f",
  387. emp_totals_ptr->total_wageRate,
  388. emp_totals_ptr->total_hours,
  389. emp_totals_ptr->total_overtimeHrs,
  390. emp_totals_ptr->total_grossPay,
  391. emp_totals_ptr->total_stateTax,
  392. emp_totals_ptr->total_fedTax,
  393. emp_totals_ptr->total_netPay);
  394.  
  395. // make sure you don't divide by zero or a negative number
  396. if (theSize > 0)
  397. {
  398. // print the averages for all the floating point fields
  399. printf("\nAverages: %5.2f %5.1f %5.1f %7.2f %6.2f %7.2f %8.2f",
  400. emp_totals_ptr->total_wageRate/theSize,
  401. emp_totals_ptr->total_hours/theSize,
  402. emp_totals_ptr->total_overtimeHrs/theSize,
  403. emp_totals_ptr->total_grossPay/theSize,
  404. emp_totals_ptr->total_stateTax/theSize,
  405. emp_totals_ptr->total_fedTax/theSize,
  406. emp_totals_ptr->total_netPay/theSize);
  407. } // if
  408.  
  409. // print the min and max values
  410.  
  411. printf("\nMinimum: %5.2f %5.1f %5.1f %7.2f %6.2f %7.2f %8.2f",
  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. emp_MinMax_ptr->max_wageRate,
  422. emp_MinMax_ptr->max_hours,
  423. emp_MinMax_ptr->max_overtimeHrs,
  424. emp_MinMax_ptr->max_grossPay,
  425. emp_MinMax_ptr->max_stateTax,
  426. emp_MinMax_ptr->max_fedTax,
  427. emp_MinMax_ptr->max_netPay);
  428.  
  429. } // printEmpStatistics
  430.  
  431. //*************************************************************
  432. // Function: calcOvertimeHrs
  433. //
  434. // Purpose: Calculates the overtime hours worked by an employee
  435. // in a given week for each employee.
  436. //
  437. // Parameters:
  438. //
  439. // employeeData - array of employees (i.e., struct employee)
  440. // theSize - the array size (i.e., number of employees)
  441. //
  442. // Returns: void (the overtime hours gets updated by reference)
  443. //
  444. //**************************************************************
  445.  
  446. // TODO - Transition this function from Array references to
  447. // Pointer references. Perform these three (3) steps:
  448. //
  449. // 1) Change the employeeData parameter to a pointer (emp_ptr)
  450. // 2) Change all array references in function body to pointer
  451. // references (use emp_ptr).
  452. // 3) Increment emp_ptr just before the end of the loop
  453. // to access the next employee
  454. //
  455. // Note: Review how it was done already in the getHours function
  456.  
  457. void calcOvertimeHrs (struct employee * emp_ptr, int theSize)
  458. {
  459.  
  460. int i; // array and loop index
  461.  
  462. // calculate overtime hours for each employee
  463. for (i = 0; i < theSize; ++i)
  464. {
  465. // Any overtime ?
  466. if (emp_ptr->hours >= STD_HOURS)
  467. {
  468. emp_ptr->overtimeHrs = emp_ptr->hours - STD_HOURS;
  469. }
  470. else // no overtime
  471. {
  472. emp_ptr->overtimeHrs = 0;
  473. }
  474.  
  475. // set pointer to next employee
  476. ++emp_ptr;
  477.  
  478. } // for
  479.  
  480. } // calcOvertimeHrs
  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. // move to next employee
  526. ++emp_ptr;
  527. }
  528. } // calcGrossPay
  529.  
  530. //*************************************************************
  531. // Function: calcStateTax
  532. //
  533. // Purpose: Calculates the State Tax owed based on gross pay
  534. // for each employee. State tax rate is based on the
  535. // the designated tax state based on where the
  536. // employee is actually performing the work. Each
  537. // state decides their tax rate.
  538. //
  539. // Parameters:
  540. //
  541. // employeeData - array of employees (i.e., struct employee)
  542. // theSize - the array size (i.e., number of employees)
  543. //
  544. // Returns: void (the state tax gets updated by reference)
  545. //
  546. //**************************************************************
  547.  
  548. // TODO - Transition this function from Array references to
  549. // Pointer references. Perform these three (3) steps:
  550. //
  551. // 1) Change the employeeData parameter to a pointer (emp_ptr)
  552. // 2) Change all array references in function body to pointer
  553. // references (use emp_ptr).
  554. // 3) Increment emp_ptr just before the end of the loop
  555. // to access the next employee
  556. //
  557. // Note: Review how it was done already in the getHours function
  558.  
  559. void calcStateTax (struct employee * emp_ptr, int theSize)
  560. {
  561.  
  562. int i; // loop and array index
  563.  
  564. // calculate state tax based on where employee works
  565. for (i=0; i < theSize; ++i)
  566. {
  567. // Make sure tax state is all uppercase
  568. if (islower(emp_ptr->taxState[0]))
  569. emp_ptr->taxState[0] = toupper(emp_ptr->taxState[0]);
  570. if (islower(emp_ptr->taxState[1]))
  571. emp_ptr->taxState[1] = toupper(emp_ptr->taxState[1]);
  572.  
  573. // calculate state tax based on where employee resides
  574. if (strcmp(emp_ptr->taxState, "MA") == 0)
  575. emp_ptr->stateTax = emp_ptr->grossPay * MA_TAX_RATE;
  576. else if (strcmp(emp_ptr->taxState, "VT") == 0)
  577. emp_ptr->stateTax = emp_ptr->grossPay * VT_TAX_RATE;
  578. else if (strcmp(emp_ptr->taxState, "NH") == 0)
  579. emp_ptr->stateTax = emp_ptr->grossPay * NH_TAX_RATE;
  580. else if (strcmp(emp_ptr->taxState, "CA") == 0)
  581. emp_ptr->stateTax = emp_ptr->grossPay * CA_TAX_RATE;
  582.  
  583. else
  584. // any other state is the default rate
  585. emp_ptr->stateTax = emp_ptr->grossPay * DEFAULT_TAX_RATE;
  586. // move to next employee
  587. ++emp_ptr;
  588. } // for
  589.  
  590. } // calcStateTax
  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.  
  629. // move to next employee
  630. ++emp_ptr;
  631.  
  632. } // for
  633.  
  634. } // calcFedTax
  635.  
  636. //*************************************************************
  637. // Function: calcNetPay
  638. //
  639. // Purpose: Calculates the net pay as the gross pay minus any
  640. // state and federal taxes owed for each employee.
  641. // Essentially, their "take home" pay.
  642. //
  643. // Parameters:
  644. //
  645. // employeeData - array of employees (i.e., struct employee)
  646. // theSize - the array size (i.e., number of employees)
  647. //
  648. // Returns: void (the net pay gets updated by reference)
  649. //
  650. //**************************************************************
  651.  
  652. // TODO - Transition this function from Array references to
  653. // Pointer references. Perform these three (3) steps:
  654. //
  655. // 1) Change the employeeData parameter to a pointer (emp_ptr)
  656. // 2) Change all array references in function body to pointer
  657. // references (use emp_ptr).
  658. // 3) Increment emp_ptr just before the end of the loop
  659. // to access the next employee
  660. //
  661. // Note: Review how it was done already in the getHours function
  662.  
  663. void calcNetPay (struct employee * emp_ptr, int theSize)
  664. {
  665. int i; // loop and array index
  666. float theTotalTaxes; // the total state and federal tax
  667.  
  668. // calculate the take home pay for each employee
  669. for (i=0; i < theSize; ++i)
  670. {
  671. // calculate the total state and federal taxes
  672. theTotalTaxes = emp_ptr->stateTax + emp_ptr->fedTax;
  673.  
  674. // calculate the net pay
  675. emp_ptr->netPay = emp_ptr->grossPay - theTotalTaxes;
  676.  
  677. // move to next employee
  678. ++emp_ptr;
  679.  
  680. } // for
  681.  
  682. } // calcNetPay
  683.  
  684. //*************************************************************
  685. // Function: calcEmployeeTotals
  686. //
  687. // Purpose: Performs a running total (sum) of each employee
  688. // floating point member in the array of structures
  689. //
  690. // Parameters:
  691. //
  692. // emp_ptr - pointer to array of employees (structure)
  693. // emp_totals_ptr - pointer to a structure containing the
  694. // running totals of all floating point
  695. // members in the array of employee structure
  696. // that is accessed and referenced by emp_ptr
  697. // theSize - the array size (i.e., number of employees)
  698. //
  699. // Returns:
  700. //
  701. // void (the employeeTotals structure gets updated by reference)
  702. //
  703. //**************************************************************
  704.  
  705. void calcEmployeeTotals (struct employee * emp_ptr,
  706. struct totals * emp_totals_ptr,
  707. int theSize)
  708. {
  709.  
  710. int i; // loop index
  711.  
  712. // total up each floating point item for all employees
  713. for (i = 0; i < theSize; ++i)
  714. {
  715. // add current employee data to our running totals
  716. emp_totals_ptr->total_wageRate += emp_ptr->wageRate;
  717. emp_totals_ptr->total_hours += emp_ptr->hours;
  718. emp_totals_ptr->total_overtimeHrs += emp_ptr->overtimeHrs;
  719. emp_totals_ptr->total_grossPay += emp_ptr->grossPay;
  720. emp_totals_ptr->total_stateTax += emp_ptr->stateTax;
  721. emp_totals_ptr->total_fedTax += emp_ptr->fedTax;
  722. emp_totals_ptr->total_netPay += emp_ptr->netPay;
  723.  
  724. // go to next employee in our array of structures
  725. // Note: We don't need to increment the emp_totals_ptr
  726. // because it is not an array
  727. ++emp_ptr;
  728.  
  729. } // for
  730.  
  731. // no need to return anything since we used pointers and have
  732. // been referring the array of employee structure and the
  733. // the total structure from its calling function ... this
  734. // is the power of Call by Reference.
  735.  
  736. } // calcEmployeeTotals
  737.  
  738. //*************************************************************
  739. // Function: calcEmployeeMinMax
  740. //
  741. // Purpose: Accepts various floating point values from an
  742. // employee and adds to a running update of min
  743. // and max values
  744. //
  745. // Parameters:
  746. //
  747. // employeeData - array of employees (i.e., struct employee)
  748. // employeeTotals - structure containing a running totals
  749. // of all fields above
  750. // theSize - the array size (i.e., number of employees)
  751. //
  752. // Returns:
  753. //
  754. // employeeMinMax - updated employeeMinMax structure
  755. //
  756. //**************************************************************
  757.  
  758. void calcEmployeeMinMax (struct employee * emp_ptr,
  759. struct min_max * emp_minMax_ptr,
  760. int theSize)
  761. {
  762.  
  763. int i; // loop index
  764.  
  765. // At this point, emp_ptr is pointing to the first
  766. // employee which is located in the first element
  767. // of our employee array of structures (employeeData).
  768.  
  769. // As this is the first employee, set each min
  770. // min and max value using our emp_minMax_ptr
  771. // to the associated member fields below. They
  772. // will become the initial baseline that we
  773. // can check and update if needed against the
  774. // remaining employees.
  775.  
  776. // set the min to the first employee members
  777. emp_minMax_ptr->min_wageRate = emp_ptr->wageRate;
  778. emp_minMax_ptr->min_hours = emp_ptr->hours;
  779. emp_minMax_ptr->min_overtimeHrs = emp_ptr->overtimeHrs;
  780. emp_minMax_ptr->min_grossPay = emp_ptr->grossPay;
  781. emp_minMax_ptr->min_stateTax = emp_ptr->stateTax;
  782. emp_minMax_ptr->min_fedTax = emp_ptr->fedTax;
  783. emp_minMax_ptr->min_netPay = emp_ptr->netPay;
  784.  
  785. // set the max to the first employee members
  786. emp_minMax_ptr->max_wageRate = emp_ptr->wageRate;
  787. emp_minMax_ptr->max_hours = emp_ptr->hours;
  788. emp_minMax_ptr->max_overtimeHrs = emp_ptr->overtimeHrs;
  789. emp_minMax_ptr->max_grossPay = emp_ptr->grossPay;
  790. emp_minMax_ptr->max_stateTax = emp_ptr->stateTax;
  791. emp_minMax_ptr->max_fedTax = emp_ptr->fedTax;
  792. emp_minMax_ptr->max_netPay = emp_ptr->netPay;
  793.  
  794. // compare the rest of the employees to each other for min and max
  795. for (i = 1; i < theSize; ++i)
  796. {
  797.  
  798. // go to next employee in our array of structures
  799. // Note: We don't need to increment the emp_totals_ptr
  800. // because it is not an array
  801. ++emp_ptr;
  802.  
  803. // check if current Wage Rate is the new min and/or max
  804. if (emp_ptr->wageRate < emp_minMax_ptr->min_wageRate)
  805. {
  806. emp_minMax_ptr->min_wageRate = emp_ptr->wageRate;
  807. }
  808.  
  809. if (emp_ptr->wageRate > emp_minMax_ptr->max_wageRate)
  810. {
  811. emp_minMax_ptr->max_wageRate = emp_ptr->wageRate;
  812. }
  813.  
  814. // check is current Hours is the new min and/or max
  815. if (emp_ptr->hours < emp_minMax_ptr->min_hours)
  816. {
  817. emp_minMax_ptr->min_hours = emp_ptr->hours;
  818. }
  819.  
  820. if (emp_ptr->hours > emp_minMax_ptr->max_hours)
  821. {
  822. emp_minMax_ptr->max_hours = emp_ptr->hours;
  823. }
  824.  
  825. // check is current Overtime Hours is the new min and/or max
  826. if (emp_ptr->overtimeHrs < emp_minMax_ptr->min_overtimeHrs)
  827. {
  828. emp_minMax_ptr->min_overtimeHrs = emp_ptr->overtimeHrs;
  829. }
  830.  
  831. if (emp_ptr->overtimeHrs > emp_minMax_ptr->max_overtimeHrs)
  832. {
  833. emp_minMax_ptr->max_overtimeHrs = emp_ptr->overtimeHrs;
  834. }
  835.  
  836. // check is current Gross Pay is the new min and/or max
  837. if (emp_ptr->grossPay < emp_minMax_ptr->min_grossPay)
  838. {
  839. emp_minMax_ptr->min_grossPay = emp_ptr->grossPay;
  840. }
  841.  
  842. if (emp_ptr->grossPay > emp_minMax_ptr->max_grossPay)
  843. {
  844. emp_minMax_ptr->max_grossPay = emp_ptr->grossPay;
  845. }
  846.  
  847. // check is current State Tax is the new min and/or max
  848. if (emp_ptr->stateTax < emp_minMax_ptr->min_stateTax)
  849. {
  850. emp_minMax_ptr->min_stateTax = emp_ptr->stateTax;
  851. }
  852.  
  853. if (emp_ptr->stateTax > emp_minMax_ptr->max_stateTax)
  854. {
  855. emp_minMax_ptr->max_stateTax = emp_ptr->stateTax;
  856. }
  857.  
  858. // check is current Federal Tax is the new min and/or max
  859. if (emp_ptr->fedTax < emp_minMax_ptr->min_fedTax)
  860. {
  861. emp_minMax_ptr->min_fedTax = emp_ptr->fedTax;
  862. }
  863.  
  864. if (emp_ptr->fedTax > emp_minMax_ptr->max_fedTax)
  865. {
  866. emp_minMax_ptr->max_fedTax = emp_ptr->fedTax;
  867. }
  868.  
  869. // check is current Net Pay is the new min and/or max
  870. if (emp_ptr->netPay < emp_minMax_ptr->min_netPay)
  871. {
  872. emp_minMax_ptr->min_netPay = emp_ptr->netPay;
  873. }
  874.  
  875. if (emp_ptr->netPay > emp_minMax_ptr->max_netPay)
  876. {
  877. emp_minMax_ptr->max_netPay = emp_ptr->netPay;
  878. }
  879.  
  880. } // else if
  881.  
  882. // no need to return anything since we used pointers and have
  883. // been referencing the employeeData structure and the
  884. // the employeeMinMax structure from its calling function ...
  885. // this is the power of Call by Reference.
  886.  
  887. } // calcEmployeeMinMax
Success #stdin #stdout 0s 5316KB
stdin
51.0
42.5
37.0
45.0
40.0
stdout
Enter hours worked by emp # 098401: 
Enter hours worked by emp # 526488: 
Enter hours worked by emp # 765349: 
Enter hours worked by emp # 034645: 
Enter hours worked by emp # 127615: 

*** Pay Calculator ***

---------------------------------------------------------------------------------
Name                Tax  Clock# Wage   Hours  OT   Gross   State  Fed      Net
                   State                           Pay     Tax    Tax      Pay
---------------------------------------------------------------------------------
Connie Cobol         MA  098401 10.60  51.0  11.0  598.90  29.95  149.73   419.23
Mary Apl             NH  526488  9.75  42.5   2.5  426.56   0.00  106.64   319.92
Frank Fortran        VT  765349 10.50  37.0   0.0  388.50  23.31   97.12   268.07
Jeff Ada             NY  034645 12.25  45.0   5.0  581.88  46.55  145.47   389.86
Anton Pascal         CA  127615  8.35  40.0   0.0  334.00  23.38   83.50   227.12
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: 
---------------------------------------------------------------------------------
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