fork download
  1. //********************************************************
  2. //
  3. // Assignment 8 - Structures and Strings and Pointers
  4. //
  5. // Name: John McKenna
  6. //
  7. // Class: C Programming, Spring 2024
  8. //
  9. // Date: 3/29/2024
  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. #include <stdio.h>
  27. #include <string.h>
  28. #include <stdlib.h>
  29.  
  30. #define SIZE 5
  31. #define STD_HOURS 40.0
  32. #define OT_RATE 1.5
  33. #define MA_TAX_RATE 0.05
  34. #define NH_TAX_RATE 0.0
  35. #define VT_TAX_RATE 0.06
  36. #define CA_TAX_RATE 0.07
  37. #define DEFAULT_TAX_RATE 0.08
  38. #define NAME_SIZE 20
  39. #define TAX_STATE_SIZE 3
  40. #define FED_TAX_RATE 0.25
  41. #define FIRST_NAME_SIZE 10
  42. #define LAST_NAME_SIZE 10
  43.  
  44.  
  45. struct employee {
  46. char name[NAME_SIZE];
  47. char tax_state[TAX_STATE_SIZE];
  48. int clock_num;
  49. float wage;
  50. float regular; // Added regular hours worked
  51. float overtime; // Added overtime hours worked
  52. float gross_pay; // Added gross pay
  53. float state_tax; // Added state tax
  54. float fed_tax; // Added federal tax
  55. float net_pay; // Added net pay
  56. };
  57.  
  58.  
  59. struct totals {
  60. float hours;
  61. float regular;
  62. float overtime;
  63. float gross_pay;
  64. float state_tax;
  65. float fed_tax;
  66. float net_pay;
  67. };
  68.  
  69.  
  70. struct min_max {
  71. float min_wage;
  72. float max_wage;
  73. float min_hours;
  74. float max_hours;
  75. float min_ot;
  76. float max_ot;
  77. float min_gross;
  78. float max_gross;
  79. float min_state;
  80. float max_state;
  81. float min_fed;
  82. float max_fed;
  83. float min_net;
  84. float max_net;
  85. };
  86.  
  87.  
  88. void getHours(struct employee *emp_ptr, int theSize);
  89. void printEmp(struct employee *emp_ptr, int theSize);
  90. void calcEmployeeTotals(struct employee *emp_ptr, struct totals *emp_totals_ptr, int theSize);
  91. void calcEmployeeMinMax(struct employee *emp_ptr, struct min_max *emp_MinMax_ptr, int theSize);
  92. void calcOvertimeHrs(struct employee employeeData[], int theSize);
  93. void calcGrossPay(struct employee employeeData[], int theSize);
  94. void calcStateTax(struct employee employeeData[], int theSize);
  95. void calcFedTax(struct employee employeeData[], int theSize);
  96. void calcNetPay(struct employee employeeData[], int theSize);
  97. void printEmpStatistics(struct totals employeeTotals, struct min_max employeeMinMax, int theSize);
  98.  
  99. int main() {
  100.  
  101. struct employee employeeData[SIZE] = {
  102. { "Connie Cobol", "MA", 98401, 10.60},
  103. { "Mary Apl", "NH", 526488, 9.75 },
  104. { "Frank Fortran", "VT", 765349, 10.50 },
  105. { "Jeff Ada", "NY", 34645, 12.25 },
  106. { "Anton Pascal","CA",127615, 8.35 }
  107. };
  108.  
  109.  
  110. struct totals employeeTotals = {0,0,0,0,0,0,0};
  111.  
  112.  
  113. struct totals *emp_totals_ptr = &employeeTotals;
  114.  
  115.  
  116. struct min_max employeeMinMax = {0,0,0,0,0,0,0,0,0,0,0,0,0,0};
  117.  
  118.  
  119. struct min_max *emp_min_max_ptr = &employeeMinMax;
  120.  
  121.  
  122. getHours(employeeData, SIZE);
  123. calcOvertimeHrs(employeeData, SIZE);
  124. calcGrossPay(employeeData, SIZE);
  125. calcStateTax(employeeData, SIZE);
  126. calcFedTax(employeeData, SIZE);
  127. calcNetPay(employeeData, SIZE);
  128. calcEmployeeTotals(employeeData, emp_totals_ptr, SIZE);
  129. calcEmployeeMinMax(employeeData, emp_min_max_ptr, SIZE);
  130. printEmp(employeeData, SIZE);
  131. printEmpStatistics(employeeTotals, employeeMinMax, SIZE);
  132.  
  133. return 0;
  134. }
  135.  
  136. //**************************************************************
  137. // Function: getHours
  138. //
  139. // Purpose: Obtains input from user, the number of hours worked
  140. // per employee and updates it in the array of structures
  141. // for each employee.
  142. //
  143. // Parameters:
  144. //
  145. // emp_ptr - pointer to array of employees (i.e., struct employee)
  146. // theSize - the array size (i.e., number of employees)
  147. //
  148. // Returns: void (the employee hours gets updated by reference)
  149. //
  150. //**************************************************************
  151. void getHours(struct employee *emp_ptr, int theSize) {
  152. printf("Please enter the hours worked for each employee:\n");
  153. for (int i = 0; i < theSize; ++i) {
  154. printf("%s %s: ", emp_ptr[i].name, emp_ptr[i].tax_state);
  155. scanf("%f", &emp_ptr[i].wage);
  156. }
  157. }
  158.  
  159. //*************************************************************
  160. // Function: printEmp
  161. //
  162. // Purpose: Prints out all the information for each employee
  163. // in a nice and orderly table format.
  164. //
  165. // Parameters:
  166. //
  167. // emp_ptr - pointer to array of struct employee
  168. // theSize - the array size (i.e., number of employees)
  169. //
  170. // Returns: void
  171. //
  172. //**************************************************************
  173. void printEmp(struct employee *emp_ptr, int theSize) {
  174. printf("*** Employee Information ***\n");
  175. printf("-------------------------------------------------------\n");
  176. printf("Name Tax Clock# Wage Regular Overtime Gross Pay State Tax Fed Tax Net Pay\n");
  177. printf(" State\n");
  178. printf("-------------------------------------------------------\n");
  179. for (int i = 0; i < theSize; ++i) {
  180. printf("%-20s %-3s %06d %5.2f %5.2f %5.2f %5.2f %5.2f %5.2f %5.2f\n",
  181. emp_ptr[i].name, emp_ptr[i].tax_state, emp_ptr[i].clock_num, emp_ptr[i].wage,
  182. emp_ptr[i].regular, emp_ptr[i].overtime, emp_ptr[i].gross_pay,
  183. emp_ptr[i].state_tax, emp_ptr[i].fed_tax, emp_ptr[i].net_pay);
  184. }
  185. printf("-------------------------------------------------------\n");
  186. }
  187.  
  188. //*************************************************************
  189. // Function: calcEmployeeTotals
  190. //
  191. // Purpose: Performs a running total (sum) of each employee
  192. // floating point member in the array of structures
  193. //
  194. // Parameters:
  195. //
  196. // emp_ptr - pointer to array of employees (structure)
  197. // emp_totals_ptr - pointer to a structure containing the
  198. // running totals of all floating point
  199. // members in the array of employee structure
  200. // that is accessed and referenced by emp_ptr
  201. // theSize - the array size (i.e., number of employees)
  202. //
  203. // Returns:
  204. //
  205. // void (the employeeTotals structure gets updated by reference)
  206. //
  207. //**************************************************************
  208. void calcEmployeeTotals(struct employee *emp_ptr, struct totals *emp_totals_ptr, int theSize) {
  209. for (int i = 0; i < theSize; ++i) {
  210. emp_totals_ptr->gross_pay += emp_ptr[i].gross_pay;
  211. emp_totals_ptr->state_tax += emp_ptr[i].state_tax;
  212. emp_totals_ptr->fed_tax += emp_ptr[i].fed_tax;
  213. emp_totals_ptr->net_pay += emp_ptr[i].net_pay;
  214. }
  215. }
  216.  
  217. //*************************************************************
  218. // Function: calcEmployeeMinMax
  219. //
  220. // Purpose: Accepts various floating point values from an
  221. // employee and adds to a running update of min
  222. // and max values
  223. //
  224. // Parameters:
  225. //
  226. // employeeData - array of employees (i.e., struct employee)
  227. // employeeTotals - structure containing a running totals
  228. // of all fields above
  229. // theSize - the array size (i.e., number of employees)
  230. //
  231. // Returns:
  232. //
  233. // employeeMinMax - updated employeeMinMax structure
  234. //
  235. //**************************************************************
  236. void calcEmployeeMinMax(struct employee *emp_ptr, struct min_max *emp_MinMax_ptr, int theSize) {
  237. emp_MinMax_ptr->min_wage = emp_ptr[0].wage;
  238. emp_MinMax_ptr->max_wage = emp_ptr[0].wage;
  239. emp_MinMax_ptr->min_hours = emp_ptr[0].regular;
  240. emp_MinMax_ptr->max_hours = emp_ptr[0].regular;
  241. emp_MinMax_ptr->min_ot = emp_ptr[0].overtime;
  242. emp_MinMax_ptr->max_ot = emp_ptr[0].overtime;
  243. emp_MinMax_ptr->min_gross = emp_ptr[0].gross_pay;
  244. emp_MinMax_ptr->max_gross = emp_ptr[0].gross_pay;
  245. emp_MinMax_ptr->min_state = emp_ptr[0].state_tax;
  246. emp_MinMax_ptr->max_state = emp_ptr[0].state_tax;
  247. emp_MinMax_ptr->min_fed = emp_ptr[0].fed_tax;
  248. emp_MinMax_ptr->max_fed = emp_ptr[0].fed_tax;
  249. emp_MinMax_ptr->min_net = emp_ptr[0].net_pay;
  250. emp_MinMax_ptr->max_net = emp_ptr[0].net_pay;
  251.  
  252. for (int i = 1; i < theSize; ++i) {
  253. if (emp_ptr[i].wage < emp_MinMax_ptr->min_wage)
  254. emp_MinMax_ptr->min_wage = emp_ptr[i].wage;
  255. if (emp_ptr[i].wage > emp_MinMax_ptr->max_wage)
  256. emp_MinMax_ptr->max_wage = emp_ptr[i].wage;
  257. if (emp_ptr[i].regular < emp_MinMax_ptr->min_hours)
  258. emp_MinMax_ptr->min_hours = emp_ptr[i].regular;
  259. if (emp_ptr[i].regular > emp_MinMax_ptr->max_hours)
  260. emp_MinMax_ptr->max_hours = emp_ptr[i].regular;
  261. if (emp_ptr[i].overtime < emp_MinMax_ptr->min_ot)
  262. emp_MinMax_ptr->min_ot = emp_ptr[i].overtime;
  263. if (emp_ptr[i].overtime > emp_MinMax_ptr->max_ot)
  264. emp_MinMax_ptr->max_ot = emp_ptr[i].overtime;
  265. if (emp_ptr[i].gross_pay < emp_MinMax_ptr->min_gross)
  266. emp_MinMax_ptr->min_gross = emp_ptr[i].gross_pay;
  267. if (emp_ptr[i].gross_pay > emp_MinMax_ptr->max_gross)
  268. emp_MinMax_ptr->max_gross = emp_ptr[i].gross_pay;
  269. if (emp_ptr[i].state_tax < emp_MinMax_ptr->min_state)
  270. emp_MinMax_ptr->min_state = emp_ptr[i].state_tax;
  271. if (emp_ptr[i].state_tax > emp_MinMax_ptr->max_state)
  272. emp_MinMax_ptr->max_state = emp_ptr[i].state_tax;
  273. if (emp_ptr[i].fed_tax < emp_MinMax_ptr->min_fed)
  274. emp_MinMax_ptr->min_fed = emp_ptr[i].fed_tax;
  275. if (emp_ptr[i].fed_tax > emp_MinMax_ptr->max_fed)
  276. emp_MinMax_ptr->max_fed = emp_ptr[i].fed_tax;
  277. if (emp_ptr[i].net_pay < emp_MinMax_ptr->min_net)
  278. emp_MinMax_ptr->min_net = emp_ptr[i].net_pay;
  279. if (emp_ptr[i].net_pay > emp_MinMax_ptr->max_net)
  280. emp_MinMax_ptr->max_net = emp_ptr[i].net_pay;
  281. }
  282. }
  283.  
  284. //*************************************************************
  285. // Function: calcOvertimeHrs
  286. //
  287. // Purpose: Calculates the overtime hours worked by an employee
  288. // in a given week for each employee.
  289. //
  290. // Parameters:
  291. //
  292. // employeeData - array of employees (i.e., struct employee)
  293. // theSize - the array size (i.e., number of employees)
  294. //
  295. // Returns: void (the overtime hours gets updated by reference)
  296. //
  297. //**************************************************************
  298. void calcOvertimeHrs(struct employee employeeData[], int theSize) {
  299. for (int i = 0; i < theSize; ++i) {
  300. if (employeeData[i].wage > STD_HOURS) {
  301. employeeData[i].regular = STD_HOURS;
  302. employeeData[i].overtime = employeeData[i].wage - STD_HOURS;
  303. } else {
  304. employeeData[i].regular = employeeData[i].wage;
  305. employeeData[i].overtime = 0;
  306. }
  307. }
  308. }
  309.  
  310. //*************************************************************
  311. // Function: calcGrossPay
  312. //
  313. // Purpose: Calculates the gross pay based on the the normal pay
  314. // and any overtime pay for a given week for each
  315. // employee.
  316. //
  317. // Parameters:
  318. //
  319. // employeeData - array of employees (i.e., struct employee)
  320. // theSize - the array size (i.e., number of employees)
  321. //
  322. // Returns: void (the gross pay gets updated by reference)
  323. //
  324. //**************************************************************
  325. void calcGrossPay(struct employee employeeData[], int theSize) {
  326. for (int i = 0; i < theSize; ++i) {
  327. employeeData[i].gross_pay = (employeeData[i].regular * employeeData[i].wage) + (employeeData[i].overtime * (employeeData[i].wage * OT_RATE));
  328. }
  329. }
  330.  
  331. //*************************************************************
  332. // Function: calcStateTax
  333. //
  334. // Purpose: Calculates the State Tax owed based on gross pay
  335. // for each employee. State tax rate is based on the
  336. // the designated tax state based on where the
  337. // employee is actually performing the work. Each
  338. // state decides their tax rate.
  339. //
  340. // Parameters:
  341. //
  342. // employeeData - array of employees (i.e., struct employee)
  343. // theSize - the array size (i.e., number of employees)
  344. //
  345. // Returns: void (the state tax gets updated by reference)
  346. //
  347. //**************************************************************
  348. void calcStateTax(struct employee employeeData[], int theSize) {
  349. for (int i = 0; i < theSize; ++i) {
  350. if (strcmp(employeeData[i].tax_state, "MA") == 0) {
  351. employeeData[i].state_tax = employeeData[i].gross_pay * MA_TAX_RATE;
  352. } else if (strcmp(employeeData[i].tax_state, "NH") == 0) {
  353. employeeData[i].state_tax = employeeData[i].gross_pay * NH_TAX_RATE;
  354. } else if (strcmp(employeeData[i].tax_state, "VT") == 0) {
  355. employeeData[i].state_tax = employeeData[i].gross_pay * VT_TAX_RATE;
  356. } else if (strcmp(employeeData[i].tax_state, "CA") == 0) {
  357. employeeData[i].state_tax = employeeData[i].gross_pay * CA_TAX_RATE;
  358. } else {
  359. employeeData[i].state_tax = employeeData[i].gross_pay * DEFAULT_TAX_RATE;
  360. }
  361. }
  362. }
  363.  
  364. //*************************************************************
  365. // Function: calcFedTax
  366. //
  367. // Purpose: Calculates the Federal Tax owed based on the gross
  368. // pay for each employee
  369. //
  370. // Parameters:
  371. //
  372. // employeeData - array of employees (i.e., struct employee)
  373. // theSize - the array size (i.e., number of employees)
  374. //
  375. // Returns: void (the federal tax gets updated by reference)
  376. //
  377. //**************************************************************
  378. void calcFedTax(struct employee employeeData[], int theSize) {
  379. for (int i = 0; i < theSize; ++i) {
  380. employeeData[i].fed_tax = employeeData[i].gross_pay * FED_TAX_RATE;
  381. }
  382. }
  383.  
  384. //*************************************************************
  385. // Function: calcNetPay
  386. //
  387. // Purpose: Calculates the net pay as the gross pay minus any
  388. // state and federal taxes owed for each employee.
  389. // Essentially, their "take home" pay.
  390. //
  391. // Parameters:
  392. //
  393. // employeeData - array of employees (i.e., struct employee)
  394. // theSize - the array size (i.e., number of employees)
  395. //
  396. // Returns: void (the net pay gets updated by reference)
  397. //
  398. //**************************************************************
  399. void calcNetPay(struct employee employeeData[], int theSize) {
  400. for (int i = 0; i < theSize; ++i) {
  401. employeeData[i].net_pay = employeeData[i].gross_pay - (employeeData[i].state_tax + employeeData[i].fed_tax);
  402. }
  403. }
  404.  
  405. //*************************************************************
  406. // Function: printEmpStatistics
  407. //
  408. // Purpose: Prints out the summary totals and averages of all
  409. // floating point value items for all employees
  410. // that have been processed. It also prints
  411. // out the min and max values.
  412. //
  413. // Parameters:
  414. //
  415. // employeeTotals - a structure containing a running total
  416. // of all employee floating point items
  417. // employeeMinMax - a structure containing all the minimum
  418. // and maximum values of all employee
  419. // floating point items
  420. // theSize - the total number of employees processed, used
  421. // to check for zero or negative divide condition.
  422. //
  423. // Returns: void
  424. //
  425. //**************************************************************
  426. void printEmpStatistics(struct totals employeeTotals, struct min_max employeeMinMax, int theSize) {
  427. printf("\n\n*** Totals & Averages ***\n");
  428. printf("-------------------------------------------------------\n");
  429. printf(" Totals Minimum Maximum\n");
  430. printf("-------------------------------------------------------\n");
  431. printf("Hours %.2f %.2f %.2f\n",
  432. employeeTotals.hours, employeeMinMax.min_hours, employeeMinMax.max_hours);
  433. printf("Regular %.2f %.2f %.2f\n",
  434. employeeTotals.regular, employeeMinMax.min_wage, employeeMinMax.max_wage);
  435. printf("Overtime %.2f %.2f %.2f\n",
  436. employeeTotals.overtime, employeeMinMax.min_ot, employeeMinMax.max_ot);
  437. printf("Gross Pay %.2f %.2f %.2f\n",
  438. employeeTotals.gross_pay, employeeMinMax.min_gross, employeeMinMax.max_gross);
  439. printf("State Tax %.2f %.2f %.2f\n",
  440. employeeTotals.state_tax, employeeMinMax.min_state, employeeMinMax.max_state);
  441. printf("Federal Tax %.2f %.2f %.2f\n",
  442. employeeTotals.fed_tax, employeeMinMax.min_fed, employeeMinMax.max_fed);
  443. printf("Net Pay %.2f %.2f %.2f\n",
  444. employeeTotals.net_pay, employeeMinMax.min_net, employeeMinMax.max_net);
  445. printf("-------------------------------------------------------\n");
  446. }
  447.  
Success #stdin #stdout 0.01s 5308KB
stdin
51.0
42.5
37.0
45.0
40.0
stdout
Please enter the hours worked for each employee:
Connie Cobol MA: Mary Apl NH: Frank Fortran VT: Jeff Ada NY: Anton Pascal CA: *** Employee Information ***
-------------------------------------------------------
Name                Tax  Clock#  Wage  Regular  Overtime  Gross Pay  State Tax  Fed Tax  Net Pay
                   State
-------------------------------------------------------
Connie Cobol         MA  098401  51.00  40.00  11.00  2881.50  144.07  720.38  2017.05
Mary Apl             NH  526488  42.50  40.00   2.50  1859.38   0.00  464.84  1394.53
Frank Fortran        VT  765349  37.00  37.00   0.00  1369.00  82.14  342.25  944.61
Jeff Ada             NY  034645  45.00  40.00   5.00  2137.50  171.00  534.38  1432.12
Anton Pascal         CA  127615  40.00  40.00   0.00  1600.00  112.00  400.00  1088.00
-------------------------------------------------------


*** Totals & Averages ***
-------------------------------------------------------
           Totals         Minimum         Maximum
-------------------------------------------------------
Hours      0.00            37.00            40.00
Regular    0.00            37.00            51.00
Overtime   0.00            0.00            11.00
Gross Pay  9847.38          1369.00          2881.50
State Tax  509.21          0.00          171.00
Federal Tax 2461.84          342.25          720.38
Net Pay    6876.32          944.61          2017.05
-------------------------------------------------------