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