fork(1) download
  1. //********************************************************
  2. //
  3. // Assignment 8 - Pointers
  4. //
  5. // Name: Jesus Castillo
  6. //
  7. // Class: C Programming, Summer, 2025
  8. //
  9. // Date: 7/23/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. // All functions are called by value
  16. //
  17. //********************************************************
  18. #include <stdio.h>
  19.  
  20. // constants
  21. #define SIZE 5
  22. #define OVERTIME_RATE 1.5f
  23. #define STD_WORK_WEEK 40.0f
  24. #define TAX_STATE_SIZE 3
  25. #define FIRST_NAME_SIZE 10
  26. #define LAST_NAME_SIZE 10
  27.  
  28. // function prototypes
  29. struct name {
  30. char firstName[FIRST_NAME_SIZE];
  31. char lastName [LAST_NAME_SIZE];
  32. };
  33. struct employee {
  34. struct name empName;
  35. char taxState [TAX_STATE_SIZE];
  36. long int clockNumber;
  37. float wageRate;
  38. float hours;
  39. float overtimeHrs;
  40. float grossPay;
  41. float stateTax;
  42. float fedTax;
  43. float netPay;
  44.  
  45. };
  46. struct totals {
  47. float total_wageRate;
  48. float total_hours;
  49. float total_overtimeHrs;
  50. float total_grossPay;
  51. float total_stateTax;
  52. float total_fedTax;
  53. float total_netPay;
  54. };
  55.  
  56. struct min_max {
  57. float min_wageRate;
  58. float min_hours;
  59. float min_overtimeHrs;
  60. float min_grossPay;
  61. float min_stateTax;
  62. float min_fedTax;
  63. float min_netPay;
  64. float max_wageRate;
  65. float max_hours;
  66. float max_overtimeHrs;
  67. float max_grossPay;
  68. float max_stateTax;
  69. float max_fedTax;
  70. float max_netPay;
  71. float stateTax;
  72. };
  73.  
  74. int main() {
  75. struct employee myEmp; // structure variable to hold employee
  76. struct employee * empPtr = &myEmp;; // pointer to structure of that type
  77. empPtr->clockNumber = 98401;
  78. empPtr->wageRate = 10.60;
  79. empPtr->hours = 51.0;
  80.  
  81. float getHours (long int clockNumber);
  82. void printHeader (void);
  83. void printEm(void);
  84. void printEmp(struct employee emp);
  85. void calcTaxes(struct employee *emp);
  86. void printSummary(struct employee empArr[], int size);
  87. float calcOvertimeHours(float hours);
  88. float calcGrossPay(float hours, float wageRate);
  89.  
  90. int i;
  91.  
  92.  
  93. // TODO: Add other function prototypes here as needed
  94.  
  95. struct employee employeeData[SIZE] = {
  96. {{"Connie", "Cobol"}, "MA", 98401, 10.60},
  97. {{"Mary", "Apl",}, "NH", 526488, 9.75},
  98. {{"Frank", "Fortran"}, "VT", 765349, 10.5},
  99. {{"Jeff", "Ada"}, "NY", 34645, 12.25},
  100. {{"Anton", "Pascal"}, "CA", 127615, 8.35}
  101.  
  102. };
  103. struct totals employeeTotals = {0,0,0,0,0,0,0};
  104. struct min_max employeeMinMax = {0,0,0,0,0,0,0,0,0,0,0,0,0,0};
  105. struct totals * emp_totals_ptr = &employeeTotals;
  106. struct min_max * emp_minMax_ptr = &employeeMinMax;
  107.  
  108.  
  109. struct employee * emp_ptr; // declare a pointer to the array of employee structures
  110. emp_ptr = employeeData; // set the pointer to point to the array of employees
  111.  
  112. /* Variable Declarations */
  113.  
  114.  
  115. // process each employee
  116. for (i = 0; i < SIZE; ++i) {
  117. // Read in hours for employee
  118. employeeData[i].hours = getHours(employeeData[i].clockNumber);
  119.  
  120.  
  121. // TODO: Function call to calculate gross pay
  122. employeeData[i].grossPay = calcGrossPay(employeeData[i].hours, employeeData[i].wageRate);
  123.  
  124.  
  125. // TODO: Function call to calculate overtime hours
  126. employeeData[i].overtimeHrs = calcOvertimeHours(employeeData[i].hours);
  127. calcTaxes(&employeeData[i]);
  128.  
  129. };
  130.  
  131. // print the header info
  132. printHeader();
  133.  
  134. // print out each employee
  135. for (i = 0; i < SIZE; ++i) {
  136. // Print all the employees - call by value
  137. printEmp(employeeData[i]);
  138. // for
  139. }; // main
  140. printSummary(employeeData, SIZE);
  141. return (0);
  142.  
  143. };
  144.  
  145. //**************************************************************
  146. // Function: getHours
  147. //
  148. // Purpose: Obtains input from user, the number of hours worked
  149. // per employee and stores the result in a local variable
  150. // that is passed back to the calling function.
  151. //
  152. // Parameters: clockNumber - The unique employee ID
  153. //
  154. // Returns: hoursWorked - hours worked in a given week
  155. //
  156. //**************************************************************
  157.  
  158. float getHours (long int clockNumber) {
  159. float hoursWorked; // hours worked in a given week
  160. // Read in hours for employee
  161. if (clockNumber == 98401)
  162. hoursWorked = 51.0;
  163. else if (clockNumber == 526488)
  164. hoursWorked = 42.5;
  165. else if (clockNumber == 765349)
  166. hoursWorked = 37.0;
  167. else if (clockNumber == 34645)
  168. hoursWorked = 45.0;
  169. else if (clockNumber == 127615)
  170. hoursWorked = 40.0;
  171. else
  172. hoursWorked = 0.0;
  173.  
  174. printf("Enter hours worked by emp #%06ld: %.2f\n", clockNumber, hoursWorked);
  175.  
  176. return hoursWorked;
  177.  
  178. } // getHours
  179.  
  180. float calcOvertimeHours(float hours) {
  181. return (hours > STD_WORK_WEEK) ? (hours - STD_WORK_WEEK) : 0.0f;
  182.  
  183.  
  184. } // calcOvertimeHours
  185.  
  186. float calcGrossPay(float hours, float wageRate) {
  187. float overtime = calcOvertimeHours(hours);
  188. float regularHours = (hours > STD_WORK_WEEK) ? STD_WORK_WEEK : hours;
  189. return (regularHours * wageRate) + (overtime * wageRate * OVERTIME_RATE);
  190. } // calcGrossPay
  191.  
  192.  
  193.  
  194. void calcTaxes(struct employee *emp) {
  195. if (strcmp(emp->taxState, "MA") == 0)
  196. emp->stateTax = emp->grossPay * 0.05f;
  197.  
  198. else if (strcmp(emp->taxState, "NH") == 0)
  199. emp->stateTax = 0.0f;
  200.  
  201. else if (strcmp(emp->taxState, "VT") == 0)
  202. emp->stateTax = emp->grossPay * 0.06f;
  203.  
  204. else if (strcmp(emp->taxState, "NY") == 0)
  205. emp->stateTax = emp->grossPay * 0.08f;
  206.  
  207. else if (strcmp(emp->taxState, "CA") == 0)
  208. emp->stateTax = emp->grossPay * 0.07f;
  209.  
  210. else
  211. emp->stateTax = 0.8f;
  212.  
  213. emp->fedTax = emp->grossPay * 0.25f;
  214. emp->netPay = emp->grossPay - (emp->stateTax + emp->fedTax);
  215. }; // Calc Taxes
  216.  
  217.  
  218.  
  219.  
  220. //**************************************************************
  221. // Function: printHeader
  222. //
  223. // Purpose: Prints the initial table header information.
  224. //
  225. // Parameters: none
  226. //
  227. // Returns: void
  228. //
  229. //**************************************************************
  230.  
  231. void printHeader (void)
  232. {
  233. printf("\n-------------------------------------------------------------------------------------------------");
  234. printf ("\n\n*** Pay Calculator ***\n");
  235.  
  236. // print the table header
  237. printf("\nName St Clock# Wage Hours OT Gross StTax FedTax NetPay \n");
  238. printf("-------------------------------------------------------------------------------------------------\n");
  239.  
  240.  
  241.  
  242.  
  243.  
  244. } // printHeader
  245.  
  246. //*************************************************************
  247. // Function: printEmp
  248. //
  249. // Purpose: Prints out all the information for an employee
  250. // in a nice and orderly table format.
  251. //
  252. // Parameters:
  253. //
  254. // clockNumber - unique employee ID
  255. // wageRate - hourly wage rate
  256. // hours - Hours worked for the week
  257. // overtimeHrs - overtime hours worked in a week
  258. // grossPay - gross pay for the week
  259. //
  260. // Returns: void
  261. //
  262. //**************************************************************
  263.  
  264. void printEmp(struct employee emp) {
  265.  
  266. printf("%-8s %-8s %-8s %06ld %6.2f %6.2f %6.2f %8.2f %7.2f %6.2f %8.2f\n",
  267. emp.empName.firstName, emp.empName.lastName, emp.taxState,
  268. emp.clockNumber, emp.wageRate, emp.hours,
  269. emp.overtimeHrs, emp.grossPay, emp.stateTax,
  270. emp.fedTax, emp.netPay);
  271. };
  272.  
  273. // TODO: Add other functions here as needed
  274. // ... remember your comment block headers for each function
  275.  
  276.  
  277.  
  278. void printSummary(struct employee empArr[], int size) {
  279. float totalWage = 0.0f;
  280. float totalHours = 0.0f;
  281. float totalOT = 0.0f;
  282. float totalGross = 0.0f;
  283. float totalStateTax = 0.0f;
  284. float totalFedTax = 0.0f;
  285. float totalNet = 0.0f;
  286. // Total Wage, Hours, OT, Groos, State tax, Fed Tax, Net
  287.  
  288. float minWage = empArr[0].wageRate;
  289. float maxWage = empArr[0].wageRate;
  290. float minHours = empArr[0].hours;
  291. float maxHours = empArr[0].hours;
  292. float minOT = empArr[0].overtimeHrs;
  293. float maxOT = empArr[0].overtimeHrs;
  294. float minGross = empArr[0].grossPay;
  295. float maxGross = empArr[0].grossPay;
  296. float minState = empArr[0].stateTax;
  297. float maxState = empArr[0].stateTax;
  298. float minFed = empArr[0].fedTax;
  299. float maxFed = empArr[0].fedTax;
  300. float minNet = empArr[0].netPay;
  301. float maxNet = empArr[0].netPay;
  302. //Min and Max of Wage, Hours, OT, Groos, State tax, Fed Tax, Net
  303.  
  304. for (int i = 0; i < size; i++) {
  305. totalWage += empArr[i].wageRate;
  306. totalHours += empArr[i].hours;
  307. totalOT += empArr[i].overtimeHrs;
  308. totalGross += empArr[i].grossPay;
  309. totalStateTax += empArr[i].stateTax;
  310. totalFedTax += empArr[i].fedTax;
  311. totalNet += empArr[i].netPay;
  312. // Calc total of Taxes of Wage, Hours, OT, Groos, State tax, Fed Tax, Net
  313.  
  314. if (empArr[i].wageRate < minWage)
  315. minWage = empArr[i].wageRate;
  316. if (empArr[i].wageRate > maxWage)
  317. maxWage = empArr[i].wageRate;
  318.  
  319. if (empArr[i].hours < minHours)
  320. minHours = empArr[i].hours;
  321. if (empArr[i].hours > maxHours)
  322. maxHours = empArr[i].hours;
  323.  
  324. if (empArr[i].overtimeHrs < minOT)
  325. minOT = empArr[i].overtimeHrs;
  326. if (empArr[i].overtimeHrs > maxOT)
  327. maxOT = empArr[i].overtimeHrs;
  328.  
  329. if (empArr[i].grossPay < minGross)
  330. minGross = empArr[i].grossPay;
  331. if (empArr[i].grossPay > maxGross)
  332. maxGross = empArr[i].grossPay;
  333.  
  334. if (empArr[i].stateTax < minState)
  335. minState = empArr[i].stateTax;
  336. if (empArr[i].stateTax > maxState)
  337. maxState = empArr[i].stateTax;
  338.  
  339. if (empArr[i].fedTax < minFed)
  340. minNet = empArr[i].netPay;
  341. if (empArr[i].fedTax < minFed)
  342. minFed = empArr[i].fedTax;
  343.  
  344. if (empArr[i].netPay < minNet)
  345. minNet = empArr[i].netPay;
  346. if (empArr[i].netPay > maxNet)
  347. maxNet = empArr[i].netPay;
  348. // Calc min and max Net Wage, Hours, OT, Groos, State tax, Fed Tax, Net
  349.  
  350.  
  351. };
  352.  
  353. printf("\n-------------------------------------------------------------------------------------------------\n");
  354. printf("Total: %6.2f %6.2f %6.2f %6.2f %6.2f %6.2f %6.2f\n",
  355. totalWage, totalHours, totalOT, totalGross, totalStateTax, totalFedTax, totalNet);
  356.  
  357. printf("Averages: %6.2f %6.2f %6.2f %6.2f %6.2f %6.2f %6.2f\n",
  358. totalWage / size, totalHours / size, totalOT / size, totalGross / size,
  359. totalStateTax / size, totalFedTax / size, totalNet / size);
  360.  
  361. printf("Minimum: %6.2f %6.2f %6.2f %6.2f %6.2f %6.2f %6.2f\n",
  362. minWage, minHours, minOT, minGross, minState, minFed, minNet);
  363. printf("Maximum: %6.2f %6.2f %6.2f %6.2f %6.2f %6.2f %6.2f\n",
  364. maxWage, maxHours, maxOT, maxGross, maxState, maxFed, maxNet);
  365.  
  366. printf("\n-------------------------------------------------------------------------------------------------\n");
  367. }
  368.  
  369.  
Success #stdin #stdout 0.01s 5288KB
stdin
Standard input is empty
stdout
Enter hours worked by emp #098401: 51.00
Enter hours worked by emp #526488: 42.50
Enter hours worked by emp #765349: 37.00
Enter hours worked by emp #034645: 45.00
Enter hours worked by emp #127615: 40.00

-------------------------------------------------------------------------------------------------

*** Pay Calculator ***

Name 	          St	   Clock#   Wage    Hours    OT      Gross     StTax  FedTax    NetPay   
-------------------------------------------------------------------------------------------------
Connie   Cobol    MA       098401   10.60   51.00   11.00    598.90    29.95  149.73    419.23
Mary     Apl      NH       526488    9.75   42.50    2.50    426.56     0.00  106.64    319.92
Frank    Fortran  VT       765349   10.50   37.00    0.00    388.50    23.31   97.12    268.07
Jeff     Ada      NY       034645   12.25   45.00    5.00    581.88    46.55  145.47    389.86
Anton    Pascal   CA       127615    8.35   40.00    0.00    334.00    23.38   83.50    227.12

-------------------------------------------------------------------------------------------------
Total:                  		    51.45  215.50   18.50   2329.84   123.18  582.46   1624.19
Averages:               		    10.29   43.10    3.70    465.97    24.64  116.49    324.84
Minimum:                		     8.35   37.00    0.00    334.00     0.00   83.50    227.12
Maximum:                      	    12.25   51.00   11.00    598.90    46.55  149.73    419.23

-------------------------------------------------------------------------------------------------