fork download
  1. //********************************************************
  2. // Assignment 8 - Pointers & Structures
  3. // Name: Anthony Principe
  4. // Class: C Programming Fall 2025
  5. // Date: November 9th 2025
  6. //
  7. // Description: Program which determines overtime and
  8. // gross pay for a set of employees with outputs sent
  9. // to standard output (the screen).
  10. //********************************************************
  11.  
  12. #include <stdio.h>
  13. #include <string.h>
  14. #include <ctype.h>
  15.  
  16. // constants
  17. #define SIZE 5
  18. #define STD_HOURS 40.0
  19. #define OT_RATE 1.5
  20. #define MA_TAX_RATE 0.05
  21. #define NH_TAX_RATE 0.0
  22. #define VT_TAX_RATE 0.06
  23. #define CA_TAX_RATE 0.07
  24. #define DEFAULT_TAX_RATE 0.08
  25. #define FED_TAX_RATE 0.25
  26. #define FIRST_NAME_SIZE 10
  27. #define LAST_NAME_SIZE 10
  28. #define TAX_STATE_SIZE 3
  29.  
  30. // struct for names
  31. struct name {
  32. char firstName[FIRST_NAME_SIZE];
  33. char lastName[LAST_NAME_SIZE];
  34. };
  35.  
  36. // struct with employee info
  37. struct employee {
  38. struct name empName;
  39. char taxState[TAX_STATE_SIZE];
  40. long int clockNumber;
  41. float wageRate;
  42. float hours;
  43. float overtimeHrs;
  44. float grossPay;
  45. float stateTax;
  46. float fedTax;
  47. float netPay;
  48. };
  49.  
  50. // struct to hold totals
  51. struct totals {
  52. float total_wageRate;
  53. float total_hours;
  54. float total_overtimeHrs;
  55. float total_grossPay;
  56. float total_stateTax;
  57. float total_fedTax;
  58. float total_netPay;
  59. };
  60.  
  61. // struct to hold min and max values
  62. struct min_max {
  63. float min_wageRate;
  64. float min_hours;
  65. float min_overtimeHrs;
  66. float min_grossPay;
  67. float min_stateTax;
  68. float min_fedTax;
  69. float min_netPay;
  70. float max_wageRate;
  71. float max_hours;
  72. float max_overtimeHrs;
  73. float max_grossPay;
  74. float max_stateTax;
  75. float max_fedTax;
  76. float max_netPay;
  77. };
  78.  
  79. // prototypes for pointer functions
  80. void getHours(struct employee * emp_ptr, int theSize);
  81. void calcOvertimeHrs(struct employee * emp_ptr, int theSize);
  82. void calcGrossPay(struct employee * emp_ptr, int theSize);
  83. void calcStateTax(struct employee * emp_ptr, int theSize);
  84. void calcFedTax(struct employee * emp_ptr, int theSize);
  85. void calcNetPay(struct employee * emp_ptr, int theSize);
  86. void calcEmployeeTotals(struct employee * emp_ptr, struct totals * emp_totals_ptr, int theSize);
  87. void calcEmployeeMinMax(struct employee * emp_ptr, struct min_max * emp_minMax_ptr, int theSize);
  88. void printHeader(void);
  89. void printEmp(struct employee * emp_ptr, int theSize);
  90. void printEmpStatistics(struct totals * emp_totals_ptr, struct min_max * emp_minMax_ptr, int theSize);
  91.  
  92. int main()
  93. {
  94. // starting employee data
  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.50 },
  99. { {"Jeff","Ada"}, "NY", 34645, 12.25 },
  100. { {"Anton","Pascal"},"CA",127615, 8.35 }
  101. };
  102.  
  103. // totals and min/max start at 0
  104. struct totals employeeTotals = {0,0,0,0,0,0,0};
  105. struct min_max employeeMinMax = {0,0,0,0,0,0,0,0,0,0,0,0,0};
  106.  
  107. // do all the calculations
  108. getHours(employeeData, SIZE);
  109. calcOvertimeHrs(employeeData, SIZE);
  110. calcGrossPay(employeeData, SIZE);
  111. calcStateTax(employeeData, SIZE);
  112. calcFedTax(employeeData, SIZE);
  113. calcNetPay(employeeData, SIZE);
  114.  
  115. calcEmployeeTotals(employeeData, &employeeTotals, SIZE);
  116. calcEmployeeMinMax(employeeData, &employeeMinMax, SIZE);
  117.  
  118. // show results
  119. printHeader();
  120. printEmp(employeeData, SIZE);
  121. printEmpStatistics(&employeeTotals, &employeeMinMax, SIZE);
  122.  
  123. printf("\n\n");
  124. return 0;
  125. }
  126.  
  127. //********************************************************
  128. // getHours - asks user for hours using a pointer loop
  129. //********************************************************
  130. void getHours(struct employee * emp_ptr, int theSize)
  131. {
  132. int i;
  133.  
  134. for (i = 0; i < theSize; ++i)
  135. {
  136. printf("\nEnter hours worked by emp # %06li: ", emp_ptr->clockNumber);
  137. scanf("%f", &emp_ptr->hours);
  138.  
  139. // move pointer to next employee
  140. emp_ptr++;
  141. }
  142. }
  143.  
  144. //********************************************************
  145. // calcOvertimeHrs - subtract 40 if overtime needed
  146. //********************************************************
  147. void calcOvertimeHrs(struct employee * emp_ptr, int theSize)
  148. {
  149. int i;
  150.  
  151. for (i = 0; i < theSize; ++i)
  152. {
  153. if (emp_ptr->hours > STD_HOURS)
  154. emp_ptr->overtimeHrs = emp_ptr->hours - STD_HOURS;
  155. else
  156. emp_ptr->overtimeHrs = 0;
  157.  
  158. emp_ptr++;
  159. }
  160. }
  161.  
  162. //********************************************************
  163. // calcGrossPay - normal + overtime rate if needed
  164. //********************************************************
  165. void calcGrossPay(struct employee * emp_ptr, int theSize)
  166. {
  167. int i;
  168.  
  169. for (i = 0; i < theSize; ++i)
  170. {
  171. if (emp_ptr->hours <= STD_HOURS)
  172. {
  173. emp_ptr->grossPay = emp_ptr->hours * emp_ptr->wageRate;
  174. }
  175. else
  176. {
  177. emp_ptr->grossPay = (STD_HOURS * emp_ptr->wageRate) +
  178. (emp_ptr->overtimeHrs * emp_ptr->wageRate * OT_RATE);
  179. }
  180.  
  181. emp_ptr++;
  182. }
  183. }
  184.  
  185. //********************************************************
  186. // calcStateTax - tax based on state
  187. //********************************************************
  188. void calcStateTax(struct employee * emp_ptr, int theSize)
  189. {
  190. int i;
  191.  
  192. for (i = 0; i < theSize; ++i)
  193. {
  194. if (strcmp(emp_ptr->taxState, "MA") == 0)
  195. emp_ptr->stateTax = emp_ptr->grossPay * MA_TAX_RATE;
  196. else if (strcmp(emp_ptr->taxState, "NH") == 0)
  197. emp_ptr->stateTax = emp_ptr->grossPay * NH_TAX_RATE;
  198. else if (strcmp(emp_ptr->taxState, "VT") == 0)
  199. emp_ptr->stateTax = emp_ptr->grossPay * VT_TAX_RATE;
  200. else if (strcmp(emp_ptr->taxState, "CA") == 0)
  201. emp_ptr->stateTax = emp_ptr->grossPay * CA_TAX_RATE;
  202. else
  203. emp_ptr->stateTax = emp_ptr->grossPay * DEFAULT_TAX_RATE;
  204.  
  205. emp_ptr++;
  206. }
  207. }
  208.  
  209. //********************************************************
  210. // calcFedTax - Calculates federal tax to be collected
  211. //********************************************************
  212. void calcFedTax(struct employee * emp_ptr, int theSize)
  213. {
  214. int i;
  215.  
  216. for (i = 0; i < theSize; ++i)
  217. {
  218. emp_ptr->fedTax = emp_ptr->grossPay * FED_TAX_RATE;
  219. emp_ptr++;
  220. }
  221. }
  222.  
  223. //********************************************************
  224. // calcNetPay - Pay amount after tax
  225. //********************************************************
  226. void calcNetPay(struct employee * emp_ptr, int theSize)
  227. {
  228. int i;
  229.  
  230. for (i = 0; i < theSize; ++i)
  231. {
  232. emp_ptr->netPay = emp_ptr->grossPay - (emp_ptr->stateTax + emp_ptr->fedTax);
  233. emp_ptr++;
  234. }
  235. }
  236.  
  237. //********************************************************
  238. // calcEmployeeTotals - adds everything up once
  239. //********************************************************
  240. void calcEmployeeTotals(struct employee * emp_ptr, struct totals * emp_totals_ptr, int theSize)
  241. {
  242. int i;
  243.  
  244. for (i = 0; i < theSize; ++i)
  245. {
  246. emp_totals_ptr->total_wageRate += emp_ptr->wageRate;
  247. emp_totals_ptr->total_hours += emp_ptr->hours;
  248. emp_totals_ptr->total_overtimeHrs += emp_ptr->overtimeHrs;
  249. emp_totals_ptr->total_grossPay += emp_ptr->grossPay;
  250. emp_totals_ptr->total_stateTax += emp_ptr->stateTax;
  251. emp_totals_ptr->total_fedTax += emp_ptr->fedTax;
  252. emp_totals_ptr->total_netPay += emp_ptr->netPay;
  253.  
  254. emp_ptr++;
  255. }
  256. }
  257.  
  258. //********************************************************
  259. // calcEmployeeMinMax - checks each value
  260. //********************************************************
  261. void calcEmployeeMinMax(struct employee * emp_ptr, struct min_max * emp_minMax_ptr, int theSize)
  262. {
  263. int i;
  264.  
  265. // start with first employee as baseline
  266. emp_minMax_ptr->min_wageRate = emp_ptr->wageRate;
  267. emp_minMax_ptr->min_hours = emp_ptr->hours;
  268. emp_minMax_ptr->min_overtimeHrs = emp_ptr->overtimeHrs;
  269. emp_minMax_ptr->min_grossPay = emp_ptr->grossPay;
  270. emp_minMax_ptr->min_stateTax = emp_ptr->stateTax;
  271. emp_minMax_ptr->min_fedTax = emp_ptr->fedTax;
  272. emp_minMax_ptr->min_netPay = emp_ptr->netPay;
  273.  
  274. emp_minMax_ptr->max_wageRate = emp_ptr->wageRate;
  275. emp_minMax_ptr->max_hours = emp_ptr->hours;
  276. emp_minMax_ptr->max_overtimeHrs = emp_ptr->overtimeHrs;
  277. emp_minMax_ptr->max_grossPay = emp_ptr->grossPay;
  278. emp_minMax_ptr->max_stateTax = emp_ptr->stateTax;
  279. emp_minMax_ptr->max_fedTax = emp_ptr->fedTax;
  280. emp_minMax_ptr->max_netPay = emp_ptr->netPay;
  281.  
  282. // move off first employee
  283. emp_ptr++;
  284.  
  285. for (i = 1; i < theSize; ++i)
  286. {
  287. if (emp_ptr->wageRate < emp_minMax_ptr->min_wageRate)
  288. emp_minMax_ptr->min_wageRate = emp_ptr->wageRate;
  289. if (emp_ptr->wageRate > emp_minMax_ptr->max_wageRate)
  290. emp_minMax_ptr->max_wageRate = emp_ptr->wageRate;
  291.  
  292. if (emp_ptr->hours < emp_minMax_ptr->min_hours)
  293. emp_minMax_ptr->min_hours = emp_ptr->hours;
  294. if (emp_ptr->hours > emp_minMax_ptr->max_hours)
  295. emp_minMax_ptr->max_hours = emp_ptr->hours;
  296.  
  297. if (emp_ptr->overtimeHrs < emp_minMax_ptr->min_overtimeHrs)
  298. emp_minMax_ptr->min_overtimeHrs = emp_ptr->overtimeHrs;
  299. if (emp_ptr->overtimeHrs > emp_minMax_ptr->max_overtimeHrs)
  300. emp_minMax_ptr->max_overtimeHrs = emp_ptr->overtimeHrs;
  301.  
  302. if (emp_ptr->grossPay < emp_minMax_ptr->min_grossPay)
  303. emp_minMax_ptr->min_grossPay = emp_ptr->grossPay;
  304. if (emp_ptr->grossPay > emp_minMax_ptr->max_grossPay)
  305. emp_minMax_ptr->max_grossPay = emp_ptr->grossPay;
  306.  
  307. if (emp_ptr->stateTax < emp_minMax_ptr->min_stateTax)
  308. emp_minMax_ptr->min_stateTax = emp_ptr->stateTax;
  309. if (emp_ptr->stateTax > emp_minMax_ptr->max_stateTax)
  310. emp_minMax_ptr->max_stateTax = emp_ptr->stateTax;
  311.  
  312. if (emp_ptr->fedTax < emp_minMax_ptr->min_fedTax)
  313. emp_minMax_ptr->min_fedTax = emp_ptr->fedTax;
  314. if (emp_ptr->fedTax > emp_minMax_ptr->max_fedTax)
  315. emp_minMax_ptr->max_fedTax = emp_ptr->fedTax;
  316.  
  317. if (emp_ptr->netPay < emp_minMax_ptr->min_netPay)
  318. emp_minMax_ptr->min_netPay = emp_ptr->netPay;
  319. if (emp_ptr->netPay > emp_minMax_ptr->max_netPay)
  320. emp_minMax_ptr->max_netPay = emp_ptr->netPay;
  321.  
  322. emp_ptr++;
  323. }
  324. }
  325.  
  326. //********************************************************
  327. // print header labels
  328. //********************************************************
  329. void printHeader(void)
  330. {
  331. printf("\n\n*** Pay Calculator ***\n");
  332. printf("\n--------------------------------------------------------------");
  333. printf("-------------------");
  334. printf("\nName Tax Clock# Wage Hours OT Gross ");
  335. printf(" State Fed Net");
  336. printf("\n State Pay ");
  337. printf(" Tax Tax Pay");
  338. printf("\n--------------------------------------------------------------");
  339. printf("-------------------");
  340. }
  341.  
  342. //********************************************************
  343. // print one employee per line using pointer walking
  344. //********************************************************
  345. void printEmp(struct employee * emp_ptr, int theSize)
  346. {
  347. int i;
  348. char name[FIRST_NAME_SIZE + LAST_NAME_SIZE + 1]; // buffer for full name
  349.  
  350. for (i = 0; i < theSize; ++i)
  351. {
  352. // build full name string
  353. strcpy(name, emp_ptr->empName.firstName);
  354. strcat(name, " ");
  355. strcat(name, emp_ptr->empName.lastName);
  356.  
  357. // print formatted row
  358. printf("\n%-20.20s %-2.2s %06li %5.2f %4.1f %4.1f %7.2f %6.2f %7.2f %8.2f",
  359. name, emp_ptr->taxState, emp_ptr->clockNumber,
  360. emp_ptr->wageRate, emp_ptr->hours,
  361. emp_ptr->overtimeHrs, emp_ptr->grossPay,
  362. emp_ptr->stateTax, emp_ptr->fedTax, emp_ptr->netPay);
  363.  
  364. // next employee
  365. emp_ptr++;
  366. }
  367. }
  368.  
  369. //********************************************************
  370. // print totals, averages, min, and max rows
  371. //********************************************************
  372. void printEmpStatistics(struct totals * emp_totals_ptr, struct min_max * emp_minMax_ptr, int theSize)
  373. {
  374. printf("\n--------------------------------------------------------------");
  375. printf("-------------------");
  376.  
  377. printf("\nTotals: %5.2f %5.1f %5.1f %7.2f %6.2f %7.2f %8.2f",
  378. emp_totals_ptr->total_wageRate,
  379. emp_totals_ptr->total_hours,
  380. emp_totals_ptr->total_overtimeHrs,
  381. emp_totals_ptr->total_grossPay,
  382. emp_totals_ptr->total_stateTax,
  383. emp_totals_ptr->total_fedTax,
  384. emp_totals_ptr->total_netPay);
  385.  
  386. printf("\nAverages: %5.2f %5.1f %5.1f %7.2f %6.2f %7.2f %8.2f",
  387. emp_totals_ptr->total_wageRate / theSize,
  388. emp_totals_ptr->total_hours / theSize,
  389. emp_totals_ptr->total_overtimeHrs / theSize,
  390. emp_totals_ptr->total_grossPay / theSize,
  391. emp_totals_ptr->total_stateTax / theSize,
  392. emp_totals_ptr->total_fedTax / theSize,
  393. emp_totals_ptr->total_netPay / theSize);
  394.  
  395. printf("\nMinimum: %5.2f %5.1f %5.1f %7.2f %6.2f %7.2f %8.2f",
  396. emp_minMax_ptr->min_wageRate,
  397. emp_minMax_ptr->min_hours,
  398. emp_minMax_ptr->min_overtimeHrs,
  399. emp_minMax_ptr->min_grossPay,
  400. emp_minMax_ptr->min_stateTax,
  401. emp_minMax_ptr->min_fedTax,
  402. emp_minMax_ptr->min_netPay);
  403.  
  404. printf("\nMaximum: %5.2f %5.1f %5.1f %7.2f %6.2f %7.2f %8.2f\n",
  405. emp_minMax_ptr->max_wageRate,
  406. emp_minMax_ptr->max_hours,
  407. emp_minMax_ptr->max_overtimeHrs,
  408. emp_minMax_ptr->max_grossPay,
  409. emp_minMax_ptr->max_stateTax,
  410. emp_minMax_ptr->max_fedTax,
  411. emp_minMax_ptr->max_netPay);
  412. }
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