fork download
  1. //***************************************************************
  2. // Name: John Semenuk
  3. // Course: Spring 2026 C Programming
  4. // Date: 04/05/2026
  5. // Assignment: 8 - Employee Payroll Calculator (Pointer Version)
  6. //
  7. //***************************************************************
  8. //
  9. // Description:
  10. // This program calculates employee payroll including gross pay,
  11. // state and federal taxes, and net pay. It also computes totals,
  12. // averages, minimum, and maximum values for all employees.
  13. //
  14. //***************************************************************
  15.  
  16. #include <stdio.h>
  17. #include <string.h>
  18. #include <math.h> // Needed for rounding
  19.  
  20. #define NUM_EMPL 5
  21.  
  22. //********************************************************
  23. // Structure Definitions
  24. //********************************************************
  25. struct name {
  26. char first[20]; // First name of employee
  27. char last[20]; // Last name of employee
  28. };
  29.  
  30. struct employee {
  31. struct name empName; // Employee name
  32. char state[3]; // Tax state
  33. int clockNum; // Employee clock number
  34. float wage; // Hourly wage
  35. float hours; // Hours worked
  36. float overtime; // Overtime hours
  37. float grossPay; // Gross pay
  38. float stateTax; // State tax
  39. float fedTax; // Federal tax
  40. float netPay; // Net pay
  41. };
  42.  
  43. struct totals {
  44. float totalWage;
  45. float totalHours;
  46. float totalOT;
  47. float totalGross;
  48. float totalStateTax;
  49. float totalFedTax;
  50. float totalNetPay;
  51. };
  52.  
  53. struct min_max {
  54. float minWage;
  55. float maxWage;
  56. float minHours;
  57. float maxHours;
  58. float minOT;
  59. float maxOT;
  60. float minGross;
  61. float maxGross;
  62. float minStateTax;
  63. float maxStateTax;
  64. float minFedTax;
  65. float maxFedTax;
  66. float minNet;
  67. float maxNet;
  68. };
  69.  
  70. //********************************************************
  71. // Function Prototypes (using pointers)
  72. //********************************************************
  73. void getHours(struct employee *emp_ptr);
  74. float getStateTax(char *state, float gross);
  75. void calcEmployeeTotals(struct employee *emp_ptr, struct totals *totals_ptr);
  76. void calcEmployeeMinMax(struct employee *emp_ptr, struct min_max *minMax_ptr);
  77. void printHeader(void);
  78. void printEmp(struct employee *emp_ptr);
  79. void printSummary(struct totals *totals_ptr, struct min_max *minMax_ptr);
  80.  
  81. //********************************************************
  82. // Main Function
  83. //********************************************************
  84. int main() {
  85. // Initialize employee data
  86. struct employee employeeData[NUM_EMPL] = {
  87. { {"Connie", "Cobol"}, "MA", 98401, 10.60 },
  88. { {"Mary", "Apl"}, "NH", 526488, 9.75 },
  89. { {"Frank", "Fortran"}, "VT", 765349, 10.50 },
  90. { {"Jeff", "Ada"}, "NY", 34645, 12.25 },
  91. { {"Anton", "Pascal"}, "CA", 127615, 8.35 }
  92. };
  93.  
  94. // Pointer to the array of employee structures
  95. struct employee *emp_ptr = employeeData;
  96.  
  97. // Set up structure to store totals and initialize all to zero
  98. struct totals employeeTotals = {0,0,0,0,0,0,0};
  99. struct totals *totals_ptr = &employeeTotals;
  100.  
  101. // Set up structure to store min and max values and initialize all to zero
  102. struct min_max employeeMinMax = {0,0,0,0,0,0,0,0,0,0,0,0,0,0};
  103. struct min_max *minMax_ptr = &employeeMinMax;
  104.  
  105. //********************************************************
  106. // Input hours for each employee
  107. //********************************************************
  108. for (int i = 0; i < NUM_EMPL; i++) {
  109. getHours(emp_ptr + i);
  110. }
  111.  
  112. //********************************************************
  113. // Calculate totals and min/max
  114. //********************************************************
  115. calcEmployeeTotals(emp_ptr, totals_ptr);
  116. calcEmployeeMinMax(emp_ptr, minMax_ptr);
  117.  
  118. //********************************************************
  119. // Print Reports
  120. //********************************************************
  121. printf("\nEmployee Payroll Report\n");
  122. printf("----------------------------------------------------------\n");
  123. for (int i = 0; i < NUM_EMPL; i++) {
  124. printf("%s %s | Net Pay: %.2f\n",
  125. (emp_ptr + i)->empName.first,
  126. (emp_ptr + i)->empName.last,
  127. (emp_ptr + i)->netPay);
  128. }
  129.  
  130. printf("\nTotal Net Pay: %.2f\n", totals_ptr->totalNetPay);
  131. printf("Minimum Net Pay: %.2f\n", minMax_ptr->minNet);
  132. printf("Maximum Net Pay: %.2f\n", minMax_ptr->maxNet);
  133.  
  134. printf("\n*** Pay Calculator ***\n\n");
  135.  
  136. printHeader();
  137.  
  138. for (int i = 0; i < NUM_EMPL; i++) {
  139. printEmp(emp_ptr + i);
  140. }
  141.  
  142. printSummary(totals_ptr, minMax_ptr);
  143.  
  144. return 0;
  145. }
  146.  
  147. //********************************************************
  148. // Function Definitions
  149. //********************************************************
  150.  
  151. // Function to get hours worked for an employee
  152. void getHours(struct employee *emp_ptr) {
  153. printf("Enter hours worked for employee #%06d: ", emp_ptr->clockNum);
  154. scanf("%f", &emp_ptr->hours);
  155.  
  156. // Calculate overtime if hours > 40
  157. if (emp_ptr->hours > 40) {
  158. emp_ptr->overtime = emp_ptr->hours - 40;
  159. } else {
  160. emp_ptr->overtime = 0;
  161. }
  162.  
  163. // Calculate gross pay
  164. emp_ptr->grossPay = (emp_ptr->hours - emp_ptr->overtime) * emp_ptr->wage
  165. + emp_ptr->overtime * emp_ptr->wage * 1.5;
  166.  
  167. // Calculate taxes and net pay
  168. emp_ptr->stateTax = getStateTax(emp_ptr->state, emp_ptr->grossPay);
  169. emp_ptr->fedTax = 0.25 * emp_ptr->grossPay;
  170. emp_ptr->netPay = emp_ptr->grossPay - emp_ptr->stateTax - emp_ptr->fedTax;
  171. }
  172.  
  173. // Function to get state tax based on state abbreviation
  174. float getStateTax(char *state, float gross) {
  175. if (strcmp(state, "NH") == 0) return 0.0;
  176. if (strcmp(state, "MA") == 0) return gross * 0.05;
  177. if (strcmp(state, "VT") == 0) return gross * 0.06;
  178. if (strcmp(state, "NY") == 0) return gross * 0.08;
  179. if (strcmp(state, "CA") == 0) return gross * 0.07;
  180. return 0.0; // Default
  181. }
  182.  
  183. // Function to calculate totals for all employees
  184. void calcEmployeeTotals(struct employee *emp_ptr, struct totals *totals_ptr) {
  185. for (int i = 0; i < NUM_EMPL; i++) {
  186. totals_ptr->totalWage += (emp_ptr + i)->wage;
  187. totals_ptr->totalHours += (emp_ptr + i)->hours;
  188. totals_ptr->totalOT += (emp_ptr + i)->overtime;
  189. totals_ptr->totalGross += (emp_ptr + i)->grossPay;
  190. totals_ptr->totalStateTax += (emp_ptr + i)->stateTax;
  191. totals_ptr->totalFedTax += (emp_ptr + i)->fedTax;
  192. totals_ptr->totalNetPay += (emp_ptr + i)->netPay;
  193. }
  194. }
  195.  
  196. // Function to calculate min/max values for all employees
  197. void calcEmployeeMinMax(struct employee *emp_ptr, struct min_max *minMax_ptr) {
  198. minMax_ptr->minWage = minMax_ptr->maxWage = emp_ptr->wage;
  199. minMax_ptr->minHours = minMax_ptr->maxHours = emp_ptr->hours;
  200. minMax_ptr->minOT = minMax_ptr->maxOT = emp_ptr->overtime;
  201. minMax_ptr->minGross = minMax_ptr->maxGross = emp_ptr->grossPay;
  202. minMax_ptr->minStateTax = minMax_ptr->maxStateTax = emp_ptr->stateTax;
  203. minMax_ptr->minFedTax = minMax_ptr->maxFedTax = emp_ptr->fedTax;
  204. minMax_ptr->minNet = minMax_ptr->maxNet = emp_ptr->netPay;
  205.  
  206. for (int i = 1; i < NUM_EMPL; i++) {
  207. struct employee *e = emp_ptr + i;
  208.  
  209. if (e->wage < minMax_ptr->minWage) minMax_ptr->minWage = e->wage;
  210. if (e->wage > minMax_ptr->maxWage) minMax_ptr->maxWage = e->wage;
  211.  
  212. if (e->hours < minMax_ptr->minHours) minMax_ptr->minHours = e->hours;
  213. if (e->hours > minMax_ptr->maxHours) minMax_ptr->maxHours = e->hours;
  214.  
  215. if (e->overtime < minMax_ptr->minOT) minMax_ptr->minOT = e->overtime;
  216. if (e->overtime > minMax_ptr->maxOT) minMax_ptr->maxOT = e->overtime;
  217.  
  218. if (e->grossPay < minMax_ptr->minGross) minMax_ptr->minGross = e->grossPay;
  219. if (e->grossPay > minMax_ptr->maxGross) minMax_ptr->maxGross = e->grossPay;
  220.  
  221. if (e->stateTax < minMax_ptr->minStateTax) minMax_ptr->minStateTax = e->stateTax;
  222. if (e->stateTax > minMax_ptr->maxStateTax) minMax_ptr->maxStateTax = e->stateTax;
  223.  
  224. if (e->fedTax < minMax_ptr->minFedTax) minMax_ptr->minFedTax = e->fedTax;
  225. if (e->fedTax > minMax_ptr->maxFedTax) minMax_ptr->maxFedTax = e->fedTax;
  226.  
  227. if (e->netPay < minMax_ptr->minNet) minMax_ptr->minNet = e->netPay;
  228. if (e->netPay > minMax_ptr->maxNet) minMax_ptr->maxNet = e->netPay;
  229. }
  230. }
  231.  
  232. // Function to print table header
  233. void printHeader(void) {
  234. printf("---------------------------------------------------------------------------------\n");
  235. printf("Name Tax Clock# Wage Hours OT Gross State Fed Net\n");
  236. printf(" State Pay Tax Tax Pay\n");
  237. printf("---------------------------------------------------------------------------------\n");
  238. }
  239.  
  240. // Function to print individual employee
  241. void printEmp(struct employee *emp_ptr) {
  242. printf("%-15s %-15s %2s %06d %5.2f %5.1f %5.1f %7.2f %6.2f %6.2f %7.2f\n",
  243. emp_ptr->empName.first,
  244. emp_ptr->empName.last,
  245. emp_ptr->state,
  246. emp_ptr->clockNum,
  247. emp_ptr->wage,
  248. emp_ptr->hours,
  249. emp_ptr->overtime,
  250. emp_ptr->grossPay,
  251. emp_ptr->stateTax,
  252. emp_ptr->fedTax,
  253. emp_ptr->netPay);
  254. }
  255.  
  256. // Function to print totals, averages, min/max
  257. void printSummary(struct totals *totals_ptr, struct min_max *minMax_ptr) {
  258. printf("---------------------------------------------------------------------------------\n");
  259. printf("Totals: %.2f %5.1f %5.1f %7.2f %6.2f %6.2f %7.2f\n",
  260. totals_ptr->totalWage,
  261. totals_ptr->totalHours,
  262. totals_ptr->totalOT,
  263. totals_ptr->totalGross,
  264. totals_ptr->totalStateTax,
  265. totals_ptr->totalFedTax,
  266. totals_ptr->totalNetPay);
  267.  
  268. printf("Averages: %.2f %5.1f %5.1f %7.2f %6.2f %6.2f %7.2f\n",
  269. totals_ptr->totalWage / NUM_EMPL,
  270. totals_ptr->totalHours / NUM_EMPL,
  271. totals_ptr->totalOT / NUM_EMPL,
  272. totals_ptr->totalGross / NUM_EMPL,
  273. totals_ptr->totalStateTax / NUM_EMPL,
  274. totals_ptr->totalFedTax / NUM_EMPL,
  275. totals_ptr->totalNetPay / NUM_EMPL);
  276.  
  277. printf("Minimum: %.2f %5.1f %5.1f %7.2f %6.2f %6.2f %7.2f\n",
  278. minMax_ptr->minWage,
  279. minMax_ptr->minHours,
  280. minMax_ptr->minOT,
  281. minMax_ptr->minGross,
  282. minMax_ptr->minStateTax,
  283. minMax_ptr->minFedTax,
  284. minMax_ptr->minNet);
  285.  
  286. printf("Maximum: %.2f %5.1f %5.1f %7.2f %6.2f %6.2f %7.2f\n",
  287. minMax_ptr->maxWage,
  288. minMax_ptr->maxHours,
  289. minMax_ptr->maxOT,
  290. minMax_ptr->maxGross,
  291. minMax_ptr->maxStateTax,
  292. minMax_ptr->maxFedTax,
  293. minMax_ptr->maxNet);
  294. }
  295.  
Success #stdin #stdout 0.01s 5320KB
stdin
51.0   
42.5
37.0
45.0
40.0
stdout
Enter hours worked for employee #098401: Enter hours worked for employee #526488: Enter hours worked for employee #765349: Enter hours worked for employee #034645: Enter hours worked for employee #127615: 
Employee Payroll Report
----------------------------------------------------------
Connie Cobol | Net Pay: 419.23
Mary Apl | Net Pay: 319.92
Frank Fortran | Net Pay: 268.07
Jeff Ada | Net Pay: 389.86
Anton Pascal | Net Pay: 227.12

Total Net Pay: 1624.19
Minimum Net Pay: 227.12
Maximum Net Pay: 419.23

*** 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