fork download
  1. //********************************************************
  2. //
  3. // Assignment 7 - Structures and Strings
  4. //
  5. // Name: Seth Hin
  6. //
  7. // Class: C Programming, Spring 2026
  8. //
  9. // Date: March 29, 2026
  10. //
  11. // Description: Program which determines overtime, gross pay,
  12. // state tax, federal tax, and net pay for a set of employees.
  13. // It also calculates totals, averages, minimum, and maximum
  14. // values for all floating point employee data.
  15. //
  16. //********************************************************
  17.  
  18. #include <stdio.h>
  19. #include <string.h>
  20. #include <ctype.h>
  21.  
  22. //---------------- CONSTANTS ----------------//
  23. #define SIZE 5
  24. #define STD_HOURS 40.0
  25. #define OT_RATE 1.5
  26. #define MA_TAX_RATE 0.05
  27. #define NH_TAX_RATE 0.0
  28. #define VT_TAX_RATE 0.06
  29. #define CA_TAX_RATE 0.07
  30. #define DEFAULT_TAX_RATE 0.08
  31. #define TAX_STATE_SIZE 3
  32. #define FED_TAX_RATE 0.25
  33. #define FIRST_NAME_SIZE 10
  34. #define LAST_NAME_SIZE 10
  35.  
  36. //---------------- STRUCTURES ----------------//
  37.  
  38. // structure to store employee name
  39. struct name
  40. {
  41. char firstName[FIRST_NAME_SIZE];
  42. char lastName[LAST_NAME_SIZE];
  43. };
  44.  
  45. // structure to store employee information
  46. struct employee
  47. {
  48. struct name empName;
  49. char taxState[TAX_STATE_SIZE];
  50. long int clockNumber;
  51. float wageRate;
  52. float hours;
  53. float overtimeHrs;
  54. float grossPay;
  55. float stateTax;
  56. float fedTax;
  57. float netPay;
  58. };
  59.  
  60. // structure to store totals
  61. struct totals
  62. {
  63. float total_wageRate;
  64. float total_hours;
  65. float total_overtimeHrs;
  66. float total_grossPay;
  67. float total_stateTax;
  68. float total_fedTax;
  69. float total_netPay;
  70. };
  71.  
  72. // structure to store min and max values
  73. struct min_max
  74. {
  75. float min_wageRate, min_hours, min_overtimeHrs, min_grossPay, min_stateTax, min_fedTax, min_netPay;
  76. float max_wageRate, max_hours, max_overtimeHrs, max_grossPay, max_stateTax, max_fedTax, max_netPay;
  77. };
  78.  
  79. //---------------- FUNCTION PROTOTYPES ----------------//
  80. void getHours(struct employee[], int);
  81. void calcOvertimeHrs(struct employee[], int);
  82. void calcGrossPay(struct employee[], int);
  83. void calcStateTax(struct employee[], int);
  84. void calcFedTax(struct employee[], int);
  85. void calcNetPay(struct employee[], int);
  86. void printHeader(void);
  87. void printEmp(struct employee[], int);
  88. struct totals calcEmployeeTotals(struct employee[], struct totals, int);
  89. struct min_max calcEmployeeMinMax(struct employee[], struct min_max, int);
  90. void printEmpStatistics(struct totals, struct min_max, int);
  91.  
  92. int main()
  93. {
  94. // initialize 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. // structure to hold totals
  104. struct totals employeeTotals = {0};
  105.  
  106. // structure to hold min and max values
  107. struct min_max employeeMinMax = {0};
  108.  
  109. // function calls
  110. getHours(employeeData, SIZE);
  111. calcOvertimeHrs(employeeData, SIZE);
  112. calcGrossPay(employeeData, SIZE);
  113. calcStateTax(employeeData, SIZE);
  114. calcFedTax(employeeData, SIZE);
  115. calcNetPay(employeeData, SIZE);
  116.  
  117. employeeTotals = calcEmployeeTotals(employeeData, employeeTotals, SIZE);
  118. employeeMinMax = calcEmployeeMinMax(employeeData, employeeMinMax, SIZE);
  119.  
  120. printHeader();
  121. printEmp(employeeData, SIZE);
  122. printEmpStatistics(employeeTotals, employeeMinMax, SIZE);
  123.  
  124. return 0;
  125.  
  126. } // main
  127.  
  128. //**************************************************************
  129. // Function: getHours
  130. //
  131. // Purpose: Reads the number of hours worked for each employee
  132. // from user input and stores it in the structure.
  133. //
  134. // Parameters:
  135. // employeeData - array of employee structures
  136. // theSize - number of employees
  137. //
  138. // Returns: void
  139. //**************************************************************
  140. void getHours(struct employee employeeData[], int theSize)
  141. {
  142. int i; // loop index
  143.  
  144. for (i = 0; i < theSize; i++)
  145. {
  146. printf("\nEnter hours worked by emp # %06li: ", employeeData[i].clockNumber);
  147. scanf("%f", &employeeData[i].hours);
  148. }
  149.  
  150. } // getHours
  151.  
  152. //**************************************************************
  153. // Function: calcOvertimeHrs
  154. //
  155. // Purpose: Calculates overtime hours for each employee.
  156. //
  157. // Parameters:
  158. // employeeData - array of employee structures
  159. // theSize - number of employees
  160. //
  161. // Returns: void
  162. //**************************************************************
  163. void calcOvertimeHrs(struct employee employeeData[], int theSize)
  164. {
  165. int i; // loop index
  166.  
  167. for (i = 0; i < theSize; i++)
  168. {
  169. if (employeeData[i].hours > STD_HOURS)
  170. employeeData[i].overtimeHrs = employeeData[i].hours - STD_HOURS;
  171. else
  172. employeeData[i].overtimeHrs = 0;
  173. }
  174.  
  175. } // calcOvertimeHrs
  176.  
  177. //**************************************************************
  178. // Function: calcGrossPay
  179. //
  180. // Purpose: Calculates gross pay including overtime.
  181. //
  182. // Parameters:
  183. // employeeData - array of employee structures
  184. // theSize - number of employees
  185. //
  186. // Returns: void
  187. //**************************************************************
  188. void calcGrossPay(struct employee employeeData[], int theSize)
  189. {
  190. int i; // loop index
  191. float normalPay; // regular pay
  192. float overtimePay; // overtime pay
  193.  
  194. for (i = 0; i < theSize; i++)
  195. {
  196. normalPay = (employeeData[i].hours - employeeData[i].overtimeHrs) * employeeData[i].wageRate;
  197. overtimePay = employeeData[i].overtimeHrs * OT_RATE * employeeData[i].wageRate;
  198. employeeData[i].grossPay = normalPay + overtimePay;
  199. }
  200.  
  201. } // calcGrossPay
  202.  
  203. //**************************************************************
  204. // Function: calcStateTax
  205. //
  206. // Purpose: Calculates state tax based on tax state.
  207. //
  208. // Parameters:
  209. // employeeData - array of employee structures
  210. // theSize - number of employees
  211. //
  212. // Returns: void
  213. //**************************************************************
  214. void calcStateTax(struct employee employeeData[], int theSize)
  215. {
  216. int i; // loop index
  217.  
  218. for (i = 0; i < theSize; i++)
  219. {
  220. if (strcmp(employeeData[i].taxState, "MA") == 0)
  221. employeeData[i].stateTax = employeeData[i].grossPay * MA_TAX_RATE;
  222. else if (strcmp(employeeData[i].taxState, "NH") == 0)
  223. employeeData[i].stateTax = employeeData[i].grossPay * NH_TAX_RATE;
  224. else if (strcmp(employeeData[i].taxState, "VT") == 0)
  225. employeeData[i].stateTax = employeeData[i].grossPay * VT_TAX_RATE;
  226. else if (strcmp(employeeData[i].taxState, "CA") == 0)
  227. employeeData[i].stateTax = employeeData[i].grossPay * CA_TAX_RATE;
  228. else
  229. employeeData[i].stateTax = employeeData[i].grossPay * DEFAULT_TAX_RATE;
  230. }
  231.  
  232. } // calcStateTax
  233.  
  234. //**************************************************************
  235. // Function: calcFedTax
  236. //
  237. // Purpose: Calculates federal tax for each employee.
  238. //
  239. // Parameters:
  240. // employeeData - array of employee structures
  241. // theSize - number of employees
  242. //
  243. // Returns: void
  244. //**************************************************************
  245. void calcFedTax(struct employee employeeData[], int theSize)
  246. {
  247. int i; // loop index
  248.  
  249. for (i = 0; i < theSize; i++)
  250. {
  251. employeeData[i].fedTax = employeeData[i].grossPay * FED_TAX_RATE;
  252. }
  253.  
  254. } // calcFedTax
  255.  
  256. //**************************************************************
  257. // Function: calcNetPay
  258. //
  259. // Purpose: Calculates net pay after taxes.
  260. //
  261. // Parameters:
  262. // employeeData - array of employee structures
  263. // theSize - number of employees
  264. //
  265. // Returns: void
  266. //**************************************************************
  267. void calcNetPay(struct employee employeeData[], int theSize)
  268. {
  269. int i; // loop index
  270. float totalTaxes; // sum of taxes
  271.  
  272. for (i = 0; i < theSize; i++)
  273. {
  274. totalTaxes = employeeData[i].stateTax + employeeData[i].fedTax;
  275. employeeData[i].netPay = employeeData[i].grossPay - totalTaxes;
  276. }
  277.  
  278. } // calcNetPay
  279.  
  280. //**************************************************************
  281. // Function: printHeader
  282. //
  283. // Purpose: Prints report header.
  284. //
  285. // Parameters: none
  286. //
  287. // Returns: void
  288. //**************************************************************
  289. void printHeader(void)
  290. {
  291. printf("\n\n*** Pay Calculator ***\n");
  292. printf("\n---------------------------------------------------------------------------------");
  293. printf("\nName Tax Clock# Wage Hours OT Gross State Fed Net");
  294. printf("\n State Pay Tax Tax Pay");
  295. printf("\n---------------------------------------------------------------------------------");
  296. }
  297.  
  298. //**************************************************************
  299. // Function: printEmp
  300. //
  301. // Purpose: Prints employee data.
  302. //
  303. // Parameters:
  304. // employeeData - array of employees
  305. // theSize - number of employees
  306. //
  307. // Returns: void
  308. //**************************************************************
  309. void printEmp(struct employee employeeData[], int theSize)
  310. {
  311. int i; // loop index
  312. char name[25]; // formatted name
  313.  
  314. for (i = 0; i < theSize; i++)
  315. {
  316. strcpy(name, employeeData[i].empName.firstName);
  317. strcat(name, " ");
  318. strcat(name, employeeData[i].empName.lastName);
  319.  
  320. printf("\n%-20s %-2s %06li %6.2f %6.1f %5.1f %7.2f %6.2f %7.2f %8.2f",
  321. name, employeeData[i].taxState, employeeData[i].clockNumber,
  322. employeeData[i].wageRate, employeeData[i].hours,
  323. employeeData[i].overtimeHrs, employeeData[i].grossPay,
  324. employeeData[i].stateTax, employeeData[i].fedTax,
  325. employeeData[i].netPay);
  326. }
  327.  
  328. } // printEmp
  329.  
  330. //**************************************************************
  331. // Function: calcEmployeeTotals
  332. //
  333. // Purpose: Calculates totals of all numeric fields.
  334. //
  335. // Parameters:
  336. // employeeData - array of employees
  337. // employeeTotals - totals structure
  338. // theSize - number of employees
  339. //
  340. // Returns: updated totals structure
  341. //**************************************************************
  342. struct totals calcEmployeeTotals(struct employee employeeData[],
  343. struct totals employeeTotals,
  344. int theSize)
  345. {
  346. int i; // loop index
  347.  
  348. for (i = 0; i < theSize; i++)
  349. {
  350. employeeTotals.total_wageRate += employeeData[i].wageRate;
  351. employeeTotals.total_hours += employeeData[i].hours;
  352. employeeTotals.total_overtimeHrs += employeeData[i].overtimeHrs;
  353. employeeTotals.total_grossPay += employeeData[i].grossPay;
  354. employeeTotals.total_stateTax += employeeData[i].stateTax;
  355. employeeTotals.total_fedTax += employeeData[i].fedTax;
  356. employeeTotals.total_netPay += employeeData[i].netPay;
  357. }
  358.  
  359. return employeeTotals;
  360.  
  361. } // calcEmployeeTotals
  362.  
  363. //**************************************************************
  364. // Function: calcEmployeeMinMax
  365. //
  366. // Purpose: Determines min and max values.
  367. //
  368. // Parameters:
  369. // employeeData - array of employees
  370. // employeeMinMax - min/max structure
  371. // theSize - number of employees
  372. //
  373. // Returns: updated min/max structure
  374. //**************************************************************
  375. struct min_max calcEmployeeMinMax(struct employee employeeData[],
  376. struct min_max employeeMinMax,
  377. int theSize)
  378. {
  379. int i; // loop index
  380.  
  381. employeeMinMax.min_wageRate = employeeMinMax.max_wageRate = employeeData[0].wageRate;
  382. employeeMinMax.min_hours = employeeMinMax.max_hours = employeeData[0].hours;
  383. employeeMinMax.min_overtimeHrs = employeeMinMax.max_overtimeHrs = employeeData[0].overtimeHrs;
  384. employeeMinMax.min_grossPay = employeeMinMax.max_grossPay = employeeData[0].grossPay;
  385. employeeMinMax.min_stateTax = employeeMinMax.max_stateTax = employeeData[0].stateTax;
  386. employeeMinMax.min_fedTax = employeeMinMax.max_fedTax = employeeData[0].fedTax;
  387. employeeMinMax.min_netPay = employeeMinMax.max_netPay = employeeData[0].netPay;
  388.  
  389. for (i = 1; i < theSize; i++)
  390. {
  391. if (employeeData[i].hours < employeeMinMax.min_hours)
  392. employeeMinMax.min_hours = employeeData[i].hours;
  393. if (employeeData[i].hours > employeeMinMax.max_hours)
  394. employeeMinMax.max_hours = employeeData[i].hours;
  395.  
  396. if (employeeData[i].overtimeHrs < employeeMinMax.min_overtimeHrs)
  397. employeeMinMax.min_overtimeHrs = employeeData[i].overtimeHrs;
  398. if (employeeData[i].overtimeHrs > employeeMinMax.max_overtimeHrs)
  399. employeeMinMax.max_overtimeHrs = employeeData[i].overtimeHrs;
  400.  
  401. if (employeeData[i].grossPay < employeeMinMax.min_grossPay)
  402. employeeMinMax.min_grossPay = employeeData[i].grossPay;
  403. if (employeeData[i].grossPay > employeeMinMax.max_grossPay)
  404. employeeMinMax.max_grossPay = employeeData[i].grossPay;
  405.  
  406. if (employeeData[i].stateTax < employeeMinMax.min_stateTax)
  407. employeeMinMax.min_stateTax = employeeData[i].stateTax;
  408. if (employeeData[i].stateTax > employeeMinMax.max_stateTax)
  409. employeeMinMax.max_stateTax = employeeData[i].stateTax;
  410.  
  411. if (employeeData[i].fedTax < employeeMinMax.min_fedTax)
  412. employeeMinMax.min_fedTax = employeeData[i].fedTax;
  413. if (employeeData[i].fedTax > employeeMinMax.max_fedTax)
  414. employeeMinMax.max_fedTax = employeeData[i].fedTax;
  415.  
  416. if (employeeData[i].netPay < employeeMinMax.min_netPay)
  417. employeeMinMax.min_netPay = employeeData[i].netPay;
  418. if (employeeData[i].netPay > employeeMinMax.max_netPay)
  419. employeeMinMax.max_netPay = employeeData[i].netPay;
  420. }
  421.  
  422. return employeeMinMax;
  423.  
  424. } // calcEmployeeMinMax
  425.  
  426. //**************************************************************
  427. // Function: printEmpStatistics
  428. //
  429. // Purpose: Prints totals, averages, min, and max values.
  430. //
  431. // Parameters:
  432. // employeeTotals - totals structure
  433. // employeeMinMax - min/max structure
  434. // theSize - number of employees
  435. //
  436. // Returns: void
  437. //**************************************************************
  438. void printEmpStatistics(struct totals employeeTotals,
  439. struct min_max employeeMinMax,
  440. int theSize)
  441. {
  442. printf("\n---------------------------------------------------------------------------------");
  443.  
  444. printf("\nTotals: %5.2f %5.1f %5.1f %7.2f %6.2f %7.2f %8.2f",
  445. employeeTotals.total_wageRate,
  446. employeeTotals.total_hours,
  447. employeeTotals.total_overtimeHrs,
  448. employeeTotals.total_grossPay,
  449. employeeTotals.total_stateTax,
  450. employeeTotals.total_fedTax,
  451. employeeTotals.total_netPay);
  452.  
  453. printf("\nAverages: %5.2f %5.1f %5.1f %7.2f %6.2f %7.2f %8.2f",
  454. employeeTotals.total_wageRate / theSize,
  455. employeeTotals.total_hours / theSize,
  456. employeeTotals.total_overtimeHrs / theSize,
  457. employeeTotals.total_grossPay / theSize,
  458. employeeTotals.total_stateTax / theSize,
  459. employeeTotals.total_fedTax / theSize,
  460. employeeTotals.total_netPay / theSize);
  461.  
  462. printf("\nMinimum: %5.2f %5.1f %5.1f %7.2f %6.2f %7.2f %8.2f",
  463. employeeMinMax.min_wageRate,
  464. employeeMinMax.min_hours,
  465. employeeMinMax.min_overtimeHrs,
  466. employeeMinMax.min_grossPay,
  467. employeeMinMax.min_stateTax,
  468. employeeMinMax.min_fedTax,
  469. employeeMinMax.min_netPay);
  470.  
  471. printf("\nMaximum: %5.2f %5.1f %5.1f %7.2f %6.2f %7.2f %8.2f\n",
  472. employeeMinMax.max_wageRate,
  473. employeeMinMax.max_hours,
  474. employeeMinMax.max_overtimeHrs,
  475. employeeMinMax.max_grossPay,
  476. employeeMinMax.max_stateTax,
  477. employeeMinMax.max_fedTax,
  478. employeeMinMax.max_netPay);
  479.  
  480. } // printEmpStatistics
Success #stdin #stdout 0s 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:                        10.60  37.0   0.0  334.00   0.00   83.50   227.12
Maximum:                        10.60  51.0  11.0  598.90  46.55  149.73   419.23