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