fork download
  1. //********************************************************
  2. //
  3. // Assignment 8 - Structures and Strings and Pointers
  4. //
  5. // Name: <Alberto DeJesus>
  6. //
  7. // Class: C Programming, <Summer 2026>
  8. //
  9. // Date: <July 20, 2026>
  10. //
  11. // Description: Program which determines overtime and
  12. // gross pay for a set of employees with outputs sent
  13. // to standard output (the screen).
  14. //
  15. // This assignment also adds the employee name, their tax state,
  16. // and calculates the state tax, federal tax, and net pay. It
  17. // also calculates totals, averages, minimum, and maximum values.
  18. //
  19. // Array and Structure references are to be replaced with
  20. // pointer references to speed up the processing of this code.
  21. //
  22. // Call by Reference design (using pointers)
  23. //
  24. //********************************************************
  25.  
  26. // necessary header files
  27. #include <stdio.h>
  28. #include <string.h>
  29. #include <ctype.h>
  30.  
  31. // define constants
  32. #define SIZE 5
  33. #define STD_HOURS 40.0
  34. #define OT_RATE 1.5
  35. #define MA_TAX_RATE 0.05
  36. #define NH_TAX_RATE 0.0
  37. #define VT_TAX_RATE 0.06
  38. #define CA_TAX_RATE 0.07
  39. #define DEFAULT_TAX_RATE 0.08
  40. #define NAME_SIZE 20
  41. #define TAX_STATE_SIZE 3
  42. #define FED_TAX_RATE 0.25
  43. #define FIRST_NAME_SIZE 10
  44. #define LAST_NAME_SIZE 10
  45.  
  46. // Define a structure type to store an employee name
  47. // ... note how one could easily extend this to other parts
  48. // parts of a name: Middle, Nickname, Prefix, Suffix, etc.
  49. struct name
  50. {
  51. char firstName[FIRST_NAME_SIZE];
  52. char lastName [LAST_NAME_SIZE];
  53. };
  54.  
  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.  
  125.  
  126. void calcOvertimeHrs (struct employee * emp_ptr, int theSize);
  127. void calcGrossPay (struct employee * emp_ptr, int theSize);
  128. void calcStateTax (struct employee * emp_ptr, int theSize); /* tax and employee hours combined*/
  129. void calcFedTax (struct employee * emp_ptr, int theSize);
  130. void calcNetPay (struct employee * emp_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. calcGrossPay (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.  
  201. // print the totals and averages for all float items
  202. void printEmpStatistics(struct totals * emp_totals_ptr,
  203. struct min_max * emp_MinMax_ptr,
  204. int theSize);
  205.  
  206. return (0); // success
  207.  
  208. } // main
  209.  
  210. //**************************************************************
  211. // Function: getHours
  212. //
  213. // Purpose: Obtains input from user, the number of hours worked
  214. // per employee and updates it in the array of structures
  215. // for each employee.
  216. //
  217. // Parameters:
  218. //
  219. // emp_ptr - pointer to array of employees (i.e., struct employee)
  220. // theSize - the array size (i.e., number of employees)
  221. //
  222. // Returns: void (the employee hours gets updated by reference)
  223. //
  224. //**************************************************************
  225.  
  226. void getHours (struct employee * emp_ptr, int theSize)
  227. {
  228.  
  229. int i; // loop index
  230.  
  231. // read in hours for each employee
  232. for (i = 0; i < theSize; ++i)
  233. {
  234. // Read in hours for employee
  235. printf("\nEnter hours worked by emp # %06li: ", emp_ptr->clockNumber);
  236. scanf ("%f", &emp_ptr->hours);
  237.  
  238. // set pointer to next employee
  239. ++emp_ptr;
  240. }
  241.  
  242. } // getHours
  243.  
  244. //**************************************************************
  245. // Function: printHeader
  246. //
  247. // Purpose: Prints the initial table header information.
  248. //
  249. // Parameters: none
  250. //
  251. // Returns: void
  252. //
  253. //**************************************************************
  254.  
  255. void printHeader (void)
  256. {
  257.  
  258. printf ("\n\n*** Pay Calculator ***\n");
  259.  
  260. // print the table header
  261. printf("\n--------------------------------------------------------------");
  262. printf("-------------------");
  263. printf("\nName Tax Clock# Wage Hours OT Gross ");
  264. printf(" State Fed Net");
  265. printf("\n State Pay ");
  266. printf(" Tax Tax Pay");
  267.  
  268. printf("\n--------------------------------------------------------------");
  269. printf("-------------------");
  270.  
  271. } // printHeader
  272.  
  273. //*************************************************************
  274. // Function: printEmp
  275. //
  276. // Purpose: Prints out all the information for each employee
  277. // in a nice and orderly table format.
  278. //
  279. // Parameters:
  280. //
  281. // emp_ptr - pointer to array of struct employee
  282. // theSize - the array size (i.e., number of employees)
  283. //
  284. // Returns: void
  285. //
  286. //**************************************************************
  287.  
  288. void printEmp (struct employee * emp_ptr, int theSize)
  289. {
  290.  
  291. int i; // array and loop index
  292.  
  293. // Used to format the employee name
  294. char name [FIRST_NAME_SIZE + LAST_NAME_SIZE + 1];
  295.  
  296. // read in hours for each employee
  297. for (i = 0; i < theSize; ++i)
  298. {
  299. // While you could just print the first and last name in the printf
  300. // statement that follows, you could also use various C string library
  301. // functions to format the name exactly the way you want it. Breaking
  302. // the name into first and last members additionally gives you some
  303. // flexibility in printing. This also becomes more useful if we decide
  304. // later to store other parts of a person's name. I really did this just
  305. // to show you how to work with some of the common string functions.
  306. strcpy (name, emp_ptr->empName.firstName);
  307. strcat (name, " "); // add a space between first and last names
  308. strcat (name, emp_ptr->empName.lastName);
  309.  
  310. // Print out a single employee
  311. printf("\n%-20.20s %-2.2s %06li %5.2f %4.1f %4.1f %7.2f %6.2f %7.2f %8.2f",
  312. name, emp_ptr->taxState, emp_ptr->clockNumber,
  313. emp_ptr->wageRate, emp_ptr->hours,
  314. emp_ptr->overtimeHrs, emp_ptr->grossPay,
  315. emp_ptr->stateTax, emp_ptr->fedTax,
  316. emp_ptr->netPay);
  317.  
  318. // set pointer to next employee
  319. ++emp_ptr;
  320.  
  321. } // for
  322.  
  323. } // printEmp
  324.  
  325. //*************************************************************
  326. // Function: printEmpStatistics
  327. //
  328. // Purpose: Prints out the summary totals and averages of all
  329. // floating point value items for all employees
  330. // that have been processed. It also prints
  331. // out the min and max values.
  332. //
  333. // Parameters:
  334. //
  335. // employeeTotals - a structure containing a running total
  336. // of all employee floating point items
  337. // employeeMinMax - a structure containing all the minimum
  338. // and maximum values of all employee
  339. // floating point items
  340. // theSize - the total number of employees processed, used
  341. // to check for zero or negative divide condition.
  342. //
  343. // Returns: void
  344. //
  345. //**************************************************************
  346.  
  347. // TODO - Transition this function from Structure references to
  348. // Pointer references. Two steps are needed:
  349. //
  350. // 1) Change both structure parameters to pointers (use
  351. // emp_totals_ptr and emp_MinMax_ptr).
  352. //
  353. // 2) Change all structures references to pointer references
  354. // within all places inside the function body.
  355. //
  356. // For example, instead of employeeTotals.total_wageRate
  357. // ... use emp_totals_ptr->total_wageRate
  358. // and instead of employeeMinMax.min_wageRate
  359. // ... use emp_MinMax_ptr->min_wageRate
  360.  
  361. void printEmpStatistics(struct totals * emp_totals_ptr,
  362. struct min_max * emp_MinMax_ptr,
  363. 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, /*individual data of employee totals*/
  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, /*employee tax total*/
  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, /*employee minimum hours and tax*/
  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, /*employee maximum hours and tax*/
  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. } // printEmpStatistics
  415.  
  416. //*************************************************************
  417. // Function: calcOvertimeHrs
  418. //
  419. // Purpose: Calculates the overtime hours worked by an employee
  420. // in a given week for each employee.
  421. //
  422. // Parameters:
  423. //
  424. // employeeData - array of employees (i.e., struct employee)
  425. // theSize - the array size (i.e., number of employees)
  426. //
  427. // Returns: void (the overtime hours gets updated by reference)
  428. //
  429. //**************************************************************
  430.  
  431. // TODO - Transition this function from Array references to
  432. // Pointer references. Perform these three (3) steps:
  433. //
  434. // 1) Change the employeeData parameter to a pointer (emp_ptr)
  435. // 2) Change all array references in function body to pointer
  436. // references (use emp_ptr).
  437. // 3) Increment emp_ptr just before the end of the loop
  438. // to access the next employee
  439. //
  440. // Note: Review how it was done already in the getHours function
  441.  
  442. void calcOvertimeHrs(struct employee * emp_ptr, int theSize)
  443. {
  444.  
  445. int i; // array and loop index
  446.  
  447. // calculate overtime hours for each employee
  448. for (i = 0; i < theSize; ++i)
  449. {
  450. // Any overtime ?
  451. if (emp_ptr->hours >= STD_HOURS)
  452. {
  453. emp_ptr->overtimeHrs = emp_ptr->hours - STD_HOURS; /*overtime hours employees*/
  454. }
  455. else // no overtime
  456. {
  457. emp_ptr->overtimeHrs = 0;
  458. }
  459. ++emp_ptr;
  460. }
  461.  
  462. } // calcOvertimeHrs
  463.  
  464. //*************************************************************
  465. // Function: calcGrossPay
  466. //
  467. // Purpose: Calculates the gross pay based on the the normal pay
  468. // and any overtime pay for a given week for each
  469. // employee.
  470. //
  471. // Parameters:
  472. //
  473. // employeeData - array of employees (i.e., struct employee)
  474. // theSize - the array size (i.e., number of employees)
  475. //
  476. // Returns: void (the gross pay gets updated by reference)
  477. //
  478. //**************************************************************
  479.  
  480. // TODO - Transition this function from Array references to
  481. // Pointer references. Perform these three (3) steps:
  482. //
  483. // 1) Change the employeeData parameter to a pointer (emp_ptr)
  484. // 2) Change all array references in function body to pointer
  485. // references (use emp_ptr).
  486. // 3) Increment emp_ptr just before the end of the loop
  487. // to access the next employee
  488. //
  489. // Note: Review how it was done already in the getHours function
  490.  
  491. void calcGrossPay(struct employee * emp_ptr, int theSize)
  492. {
  493. int i; // loop and array index
  494. float theNormalPay; // normal pay without any overtime hours
  495. float theOvertimePay; // overtime pay
  496.  
  497. // calculate grossPay for each employee
  498. for (i=0; i < theSize; ++i)
  499. {
  500. // calculate normal pay and any overtime pay
  501. theNormalPay = emp_ptr->wageRate *
  502. (emp_ptr->hours - emp_ptr->overtimeHrs);
  503. theOvertimePay = emp_ptr->overtimeHrs *
  504. (OT_RATE * emp_ptr->wageRate);
  505.  
  506. // calculate gross pay for employee as normalPay + any overtime pay
  507. emp_ptr->grossPay = theNormalPay + theOvertimePay;
  508. }
  509. ++emp_ptr;
  510. } // calcGrossPay
  511.  
  512. //*************************************************************
  513. // Function: calcStateTax
  514. //
  515. // Purpose: Calculates the State Tax owed based on gross pay
  516. // for each employee. State tax rate is based on the
  517. // the designated tax state based on where the
  518. // employee is actually performing the work. Each
  519. // state decides their tax rate.
  520. //
  521. // Parameters:
  522. //
  523. // employeeData - array of employees (i.e., struct employee)
  524. // theSize - the array size (i.e., number of employees)
  525. //
  526. // Returns: void (the state tax gets updated by reference)
  527. //
  528. //**************************************************************
  529.  
  530. // TODO - Transition this function from Array references to
  531. // Pointer references. Perform these three (3) steps:
  532. //
  533. // 1) Change the employeeData parameter to a pointer (emp_ptr)
  534. // 2) Change all array references in function body to pointer
  535. // references (use emp_ptr).
  536. // 3) Increment emp_ptr just before the end of the loop
  537. // to access the next employee
  538. //
  539. // Note: Review how it was done already in the getHours function
  540.  
  541. void calcStateTax(struct employee * emp_ptr, int theSize)
  542. {
  543.  
  544. int i; // loop and array index
  545.  
  546. // calculate state tax based on where employee works
  547. for (i=0; i < theSize; ++i)
  548. {
  549. // Make sure tax state is all uppercase
  550. if (islower(emp_ptr->taxState[0]))
  551. emp_ptr->taxState[0] = toupper(emp_ptr->taxState[0]);
  552. if (islower(emp_ptr->taxState[1]))
  553. emp_ptr->taxState[1] = toupper(emp_ptr->taxState[1]);
  554.  
  555. // calculate state tax based on where employee resides
  556. if (strcmp(emp_ptr->taxState, "MA") == 0)
  557. emp_ptr->stateTax = emp_ptr->grossPay * MA_TAX_RATE;
  558. else if (strcmp(emp_ptr->taxState, "VT") == 0)
  559. emp_ptr->stateTax = emp_ptr->grossPay * VT_TAX_RATE;
  560. else if (strcmp(emp_ptr->taxState, "NH") == 0)
  561. emp_ptr->stateTax = emp_ptr->grossPay * NH_TAX_RATE;
  562. else if (strcmp(emp_ptr->taxState, "CA") == 0)
  563. emp_ptr->stateTax = emp_ptr->grossPay * CA_TAX_RATE;
  564. else
  565. // any other state is the default rate
  566. emp_ptr->stateTax = emp_ptr->grossPay * DEFAULT_TAX_RATE;
  567. } // for
  568. ++emp_ptr;
  569. } // calcStateTax
  570.  
  571. //*************************************************************
  572. // Function: calcFedTax
  573. //
  574. // Purpose: Calculates the Federal Tax owed based on the gross
  575. // pay for each employee
  576. //
  577. // Parameters:
  578. //
  579. // employeeData - array of employees (i.e., struct employee)
  580. // theSize - the array size (i.e., number of employees)
  581. //
  582. // Returns: void (the federal tax gets updated by reference)
  583. //
  584. //**************************************************************
  585.  
  586. // TODO - Transition this function from Array references to
  587. // Pointer references. Perform these three (3) steps:
  588. //
  589. // 1) Change the employeeData parameter to a pointer (emp_ptr)
  590. // 2) Change all array references in function body to pointer
  591. // references (use emp_ptr).
  592. // 3) Increment emp_ptr just before the end of the loop
  593. // to access the next employee
  594. //
  595. // Note: Review how it was done already in the getHours function
  596.  
  597. void calcFedTax(struct employee * emp_ptr, int theSize)
  598. {
  599.  
  600. int i; // loop and array index
  601.  
  602. // calculate the federal tax for each employee
  603. for (i=0; i < theSize; ++i)
  604. {
  605. // Fed Tax is the same for all regardless of state
  606. emp_ptr->fedTax = emp_ptr->grossPay * FED_TAX_RATE;
  607.  
  608. } // for
  609. ++emp_ptr;
  610. } // calcFedTax
  611.  
  612. //*************************************************************
  613. // Function: calcNetPay
  614. //
  615. // Purpose: Calculates the net pay as the gross pay minus any
  616. // state and federal taxes owed for each employee.
  617. // Essentially, their "take home" pay.
  618. //
  619. // Parameters:
  620. //
  621. // employeeData - array of employees (i.e., struct employee)
  622. // theSize - the array size (i.e., number of employees)
  623. //
  624. // Returns: void (the net pay gets updated by reference)
  625. //
  626. //**************************************************************
  627.  
  628. // TODO - Transition this function from Array references to
  629. // Pointer references. Perform these three (3) steps:
  630. //
  631. // 1) Change the employeeData parameter to a pointer (emp_ptr)
  632. // 2) Change all array references in function body to pointer
  633. // references (use emp_ptr).
  634. // 3) Increment emp_ptr just before the end of the loop
  635. // to access the next employee
  636. //
  637. // Note: Review how it was done already in the getHours function
  638.  
  639. void calcNetPay(struct employee * emp_ptr, int theSize)
  640. {
  641. int i; // loop and array index
  642. float theTotalTaxes; // the total state and federal tax
  643.  
  644. // calculate the take home pay for each employee
  645. for (i=0; i < theSize; ++i)
  646. {
  647. // calculate the total state and federal taxes
  648. theTotalTaxes = emp_ptr->stateTax + emp_ptr->fedTax;
  649.  
  650. // calculate the net pay
  651. emp_ptr->netPay = emp_ptr->grossPay - theTotalTaxes;
  652.  
  653. } // for
  654. ++emp_ptr;
  655. } // calcNetPay
  656.  
  657. //*************************************************************
  658. // Function: calcEmployeeTotals
  659. //
  660. // Purpose: Performs a running total (sum) of each employee
  661. // floating point member in the array of structures
  662. //
  663. // Parameters:
  664. //
  665. // emp_ptr - pointer to array of employees (structure)
  666. // emp_totals_ptr - pointer to a structure containing the
  667. // running totals of all floating point
  668. // members in the array of employee structure
  669. // that is accessed and referenced by emp_ptr
  670. // theSize - the array size (i.e., number of employees)
  671. //
  672. // Returns:
  673. //
  674. // void (the employeeTotals structure gets updated by reference)
  675. //
  676. //**************************************************************
  677.  
  678. void calcEmployeeTotals (struct employee * emp_ptr,
  679. struct totals * emp_totals_ptr,
  680. int theSize)
  681. {
  682.  
  683. int i; // loop index
  684.  
  685. // total up each floating point item for all employees
  686. for (i = 0; i < theSize; ++i)
  687. {
  688. // add current employee data to our running totals
  689. emp_totals_ptr->total_wageRate += emp_ptr->wageRate;
  690. emp_totals_ptr->total_hours += emp_ptr->hours;
  691. emp_totals_ptr->total_overtimeHrs += emp_ptr->overtimeHrs;
  692. emp_totals_ptr->total_grossPay += emp_ptr->grossPay;
  693. emp_totals_ptr->total_stateTax += emp_ptr->stateTax;
  694. emp_totals_ptr->total_fedTax += emp_ptr->fedTax;
  695. emp_totals_ptr->total_netPay += emp_ptr->netPay;
  696.  
  697. // go to next employee in our array of structures
  698. // Note: We don't need to increment the emp_totals_ptr
  699. // because it is not an array
  700. ++emp_ptr;
  701.  
  702. } // for
  703.  
  704. // no need to return anything since we used pointers and have
  705. // been referring the array of employee structure and the
  706. // the total structure from its calling function ... this
  707. // is the power of Call by Reference.
  708.  
  709. } // calcEmployeeTotals
  710.  
  711. //*************************************************************
  712. // Function: calcEmployeeMinMax
  713. //
  714. // Purpose: Accepts various floating point values from an
  715. // employee and adds to a running update of min
  716. // and max values
  717. //
  718. // Parameters:
  719. //
  720. // employeeData - array of employees (i.e., struct employee)
  721. // employeeTotals - structure containing a running totals
  722. // of all fields above
  723. // theSize - the array size (i.e., number of employees)
  724. //
  725. // Returns:
  726. //
  727. // employeeMinMax - updated employeeMinMax structure
  728. //
  729. //**************************************************************
  730.  
  731. void calcEmployeeMinMax (struct employee * emp_ptr,
  732. struct min_max * emp_minMax_ptr,
  733. int theSize)
  734. {
  735.  
  736. int i; // loop index
  737.  
  738. // At this point, emp_ptr is pointing to the first
  739. // employee which is located in the first element
  740. // of our employee array of structures (employeeData).
  741.  
  742. // As this is the first employee, set each min
  743. // min and max value using our emp_minMax_ptr
  744. // to the associated member fields below. They
  745. // will become the initial baseline that we
  746. // can check and update if needed against the
  747. // remaining employees.
  748.  
  749. // set the min to the first employee members
  750. emp_minMax_ptr->min_wageRate = emp_ptr->wageRate;
  751. emp_minMax_ptr->min_hours = emp_ptr->hours;
  752. emp_minMax_ptr->min_overtimeHrs = emp_ptr->overtimeHrs;
  753. emp_minMax_ptr->min_grossPay = emp_ptr->grossPay;
  754. emp_minMax_ptr->min_stateTax = emp_ptr->stateTax;
  755. emp_minMax_ptr->min_fedTax = emp_ptr->fedTax;
  756. emp_minMax_ptr->min_netPay = emp_ptr->netPay;
  757.  
  758. // set the max to the first employee members
  759. emp_minMax_ptr->max_wageRate = emp_ptr->wageRate;
  760. emp_minMax_ptr->max_hours = emp_ptr->hours;
  761. emp_minMax_ptr->max_overtimeHrs = emp_ptr->overtimeHrs;
  762. emp_minMax_ptr->max_grossPay = emp_ptr->grossPay;
  763. emp_minMax_ptr->max_stateTax = emp_ptr->stateTax;
  764. emp_minMax_ptr->max_fedTax = emp_ptr->fedTax;
  765. emp_minMax_ptr->max_netPay = emp_ptr->netPay;
  766.  
  767. // compare the rest of the employees to each other for min and max
  768. for (i = 1; i < theSize; ++i)
  769. {
  770.  
  771. // go to next employee in our array of structures
  772. // Note: We don't need to increment the emp_totals_ptr
  773. // because it is not an array
  774. ++emp_ptr;
  775.  
  776. // check if current Wage Rate is the new min and/or max
  777. if (emp_ptr->wageRate < emp_minMax_ptr->min_wageRate)
  778. {
  779. emp_minMax_ptr->min_wageRate = emp_ptr->wageRate;
  780. }
  781.  
  782. if (emp_ptr->wageRate > emp_minMax_ptr->max_wageRate)
  783. {
  784. emp_minMax_ptr->max_wageRate = emp_ptr->wageRate;
  785. }
  786.  
  787. // check is current Hours is the new min and/or max
  788. if (emp_ptr->hours < emp_minMax_ptr->min_hours)
  789. {
  790. emp_minMax_ptr->min_hours = emp_ptr->hours;
  791. }
  792.  
  793. if (emp_ptr->hours > emp_minMax_ptr->max_hours)
  794. {
  795. emp_minMax_ptr->max_hours = emp_ptr->hours;
  796. }
  797.  
  798. // check is current Overtime Hours is the new min and/or max
  799. if (emp_ptr->overtimeHrs < emp_minMax_ptr->min_overtimeHrs)
  800. {
  801. emp_minMax_ptr->min_overtimeHrs = emp_ptr->overtimeHrs;
  802. }
  803.  
  804. if (emp_ptr->overtimeHrs > emp_minMax_ptr->max_overtimeHrs)
  805. {
  806. emp_minMax_ptr->max_overtimeHrs = emp_ptr->overtimeHrs;
  807. }
  808.  
  809. // check is current Gross Pay is the new min and/or max
  810. if (emp_ptr->grossPay < emp_minMax_ptr->min_grossPay)
  811. {
  812. emp_minMax_ptr->min_grossPay = emp_ptr->grossPay;
  813. }
  814.  
  815. if (emp_ptr->grossPay > emp_minMax_ptr->max_grossPay)
  816. {
  817. emp_minMax_ptr->max_grossPay = emp_ptr->grossPay;
  818. }
  819.  
  820. // check is current State Tax is the new min and/or max
  821. if (emp_ptr->stateTax < emp_minMax_ptr->min_stateTax)
  822. {
  823. emp_minMax_ptr->min_stateTax = emp_ptr->stateTax;
  824. }
  825.  
  826. if (emp_ptr->stateTax > emp_minMax_ptr->max_stateTax)
  827. {
  828. emp_minMax_ptr->max_stateTax = emp_ptr->stateTax;
  829. }
  830.  
  831. // check is current Federal Tax is the new min and/or max
  832. if (emp_ptr->fedTax < emp_minMax_ptr->min_fedTax)
  833. {
  834. emp_minMax_ptr->min_fedTax = emp_ptr->fedTax;
  835. }
  836.  
  837. if (emp_ptr->fedTax > emp_minMax_ptr->max_fedTax)
  838. {
  839. emp_minMax_ptr->max_fedTax = emp_ptr->fedTax;
  840. }
  841.  
  842. // check is current Net Pay is the new min and/or max
  843. if (emp_ptr->netPay < emp_minMax_ptr->min_netPay)
  844. {
  845. emp_minMax_ptr->min_netPay = emp_ptr->netPay;
  846. }
  847.  
  848. if (emp_ptr->netPay > emp_minMax_ptr->max_netPay)
  849. {
  850. emp_minMax_ptr->max_netPay = emp_ptr->netPay;
  851. }
  852.  
  853. } // else if
  854.  
  855. // no need to return anything since we used pointers and have
  856. // been referencing the employeeData structure and the
  857. // the employeeMinMax structure from its calling function ...
  858. // this is the power of Call by Reference.
  859.  
  860. } // calcEmployeeMinMax
Success #stdin #stdout 0.01s 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    0.00   0.00    0.00     0.00
Frank Fortran        VT  765349 10.50  37.0   0.0    0.00   0.00    0.00     0.00
Jeff Ada             NY  034645 12.25  45.0   5.0    0.00   0.00    0.00     0.00
Anton Pascal         CA  127615  8.35  40.0   0.0    0.00   0.00    0.00     0.00