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

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