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