fork download
  1. //********************************************************
  2. //
  3. // Assignment 7 - Structures and Strings
  4. //
  5. // Name: Carlos Dominguez
  6. //
  7. // Class: C Programming, Spring 2026
  8. //
  9. // Date:03/29/2026
  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. //********************************************************
  20.  
  21. #include <stdio.h>
  22. #include <string.h>
  23. #include <ctype.h>
  24.  
  25. //-------------------------------
  26. // Constant Definitions
  27. //-------------------------------
  28. #define SIZE 5
  29. #define STD_HOURS 40.0
  30. #define OT_RATE 1.5
  31. #define MA_TAX_RATE 0.05
  32. #define NH_TAX_RATE 0.0
  33. #define VT_TAX_RATE 0.06
  34. #define CA_TAX_RATE 0.07
  35. #define DEFAULT_TAX_RATE 0.08
  36. #define FED_TAX_RATE 0.25
  37. #define FIRST_NAME_SIZE 10
  38. #define LAST_NAME_SIZE 10
  39. #define TAX_STATE_SIZE 3
  40.  
  41. //-------------------------------
  42. // Structure Definitions
  43. //-------------------------------
  44.  
  45. // Stores first and last name separately
  46. struct name
  47. {
  48. char firstName[FIRST_NAME_SIZE];
  49. char lastName[LAST_NAME_SIZE];
  50. };
  51.  
  52. // Stores all employee-related information
  53. struct employee
  54. {
  55. struct name empName;
  56. char taxState[TAX_STATE_SIZE];
  57. long int clockNumber;
  58. float wageRate;
  59. float hours;
  60. float overtimeHrs;
  61. float grossPay;
  62. float stateTax;
  63. float fedTax;
  64. float netPay;
  65. };
  66.  
  67. // Stores running totals for all floating-point fields
  68. struct totals
  69. {
  70. float total_wageRate;
  71. float total_hours;
  72. float total_overtimeHrs;
  73. float total_grossPay;
  74. float total_stateTax;
  75. float total_fedTax;
  76. float total_netPay;
  77. };
  78.  
  79. // Stores min and max values for all floating-point fields
  80. struct min_max
  81. {
  82. float min_wageRate;
  83. float min_hours;
  84. float min_overtimeHrs;
  85. float min_grossPay;
  86. float min_stateTax;
  87. float min_fedTax;
  88. float min_netPay;
  89.  
  90. float max_wageRate;
  91. float max_hours;
  92. float max_overtimeHrs;
  93. float max_grossPay;
  94. float max_stateTax;
  95. float max_fedTax;
  96. float max_netPay;
  97. };
  98.  
  99. //-------------------------------
  100. // Function Prototypes
  101. //-------------------------------
  102. void getHours(struct employee employeeData[], int theSize);
  103. void calcOvertimeHrs(struct employee employeeData[], int theSize);
  104. void calcGrossPay(struct employee employeeData[], int theSize);
  105. void calcStateTax(struct employee employeeData[], int theSize);
  106. void calcFedTax(struct employee employeeData[], int theSize);
  107. void calcNetPay(struct employee employeeData[], int theSize);
  108. void printHeader(void);
  109. void printEmp(struct employee employeeData[], int theSize);
  110. void printEmpStatistics(struct totals employeeTotals,
  111. struct min_max employeeMinMax,
  112. int theSize);
  113.  
  114. struct totals calcEmployeeTotals(struct employee employeeData[],
  115. struct totals employeeTotals,
  116. int theSize);
  117.  
  118. struct min_max calcEmployeeMinMax(struct employee employeeData[],
  119. struct min_max employeeMinMax,
  120. int theSize);
  121.  
  122. //********************************************************
  123. // main
  124. //********************************************************
  125. int main()
  126. {
  127. // Initialize employee data (names, tax state, clock#, wage)
  128. struct employee employeeData[SIZE] = {
  129. {{"Connie", "Cobol"}, "MA", 98401, 10.60},
  130. {{"Mary", "Apl"}, "NH", 526488, 9.75},
  131. {{"Frank", "Fortran"}, "VT", 765349, 10.50},
  132. {{"Jeff", "Ada"}, "NY", 34645, 12.25},
  133. {{"Anton", "Pascal"}, "CA", 127615, 8.35}
  134. };
  135.  
  136. // Initialize totals and min/max structures
  137. struct totals employeeTotals = {0,0,0,0,0,0,0};
  138. struct min_max employeeMinMax = {0};
  139.  
  140. // Collect and compute all employee data
  141. getHours(employeeData, SIZE);
  142. calcOvertimeHrs(employeeData, SIZE);
  143. calcGrossPay(employeeData, SIZE);
  144. calcStateTax(employeeData, SIZE);
  145. calcFedTax(employeeData, SIZE);
  146. calcNetPay(employeeData, SIZE);
  147.  
  148. // Compute totals and min/max values
  149. employeeTotals = calcEmployeeTotals(employeeData, employeeTotals, SIZE);
  150. employeeMinMax = calcEmployeeMinMax(employeeData, employeeMinMax, SIZE);
  151.  
  152. // Display results
  153. printHeader();
  154. printEmp(employeeData, SIZE);
  155. printEmpStatistics(employeeTotals, employeeMinMax, SIZE);
  156.  
  157. return 0;
  158. }
  159.  
  160. //********************************************************
  161. // getHours
  162. // Purpose: Reads hours worked for each employee
  163. //********************************************************
  164. void getHours(struct employee employeeData[], int theSize)
  165. {
  166. int i;
  167. for (i = 0; i < theSize; ++i)
  168. {
  169. printf("\nEnter hours worked by emp # %06li: ",
  170. employeeData[i].clockNumber);
  171. scanf("%f", &employeeData[i].hours);
  172. }
  173. }
  174.  
  175. //********************************************************
  176. // calcOvertimeHrs
  177. // Purpose: Determines overtime hours (> 40)
  178. //********************************************************
  179. void calcOvertimeHrs(struct employee employeeData[], int theSize)
  180. {
  181. int i;
  182. for (i = 0; i < theSize; ++i)
  183. {
  184. if (employeeData[i].hours > STD_HOURS)
  185. employeeData[i].overtimeHrs = employeeData[i].hours - STD_HOURS;
  186. else
  187. employeeData[i].overtimeHrs = 0;
  188. }
  189. }
  190.  
  191. //********************************************************
  192. // calcGrossPay
  193. // Purpose: Computes gross pay including overtime
  194. //********************************************************
  195. void calcGrossPay(struct employee employeeData[], int theSize)
  196. {
  197. int i;
  198. float normalPay, overtimePay;
  199.  
  200. for (i = 0; i < theSize; ++i)
  201. {
  202. normalPay = employeeData[i].wageRate *
  203. (employeeData[i].hours - employeeData[i].overtimeHrs);
  204.  
  205. overtimePay = employeeData[i].overtimeHrs *
  206. (employeeData[i].wageRate * OT_RATE);
  207.  
  208. employeeData[i].grossPay = normalPay + overtimePay;
  209. }
  210. }
  211.  
  212. //********************************************************
  213. // calcStateTax
  214. // Purpose: Computes state tax based on employee's taxState
  215. //********************************************************
  216. void calcStateTax(struct employee employeeData[], int theSize)
  217. {
  218. int i;
  219.  
  220. for (i = 0; i < theSize; ++i)
  221. {
  222. // Normalize state code to uppercase
  223. employeeData[i].taxState[0] = toupper(employeeData[i].taxState[0]);
  224. employeeData[i].taxState[1] = toupper(employeeData[i].taxState[1]);
  225.  
  226. // Apply correct state tax rate
  227. if (strcmp(employeeData[i].taxState, "MA") == 0)
  228. employeeData[i].stateTax = employeeData[i].grossPay * MA_TAX_RATE;
  229. else if (strcmp(employeeData[i].taxState, "NH") == 0)
  230. employeeData[i].stateTax = employeeData[i].grossPay * NH_TAX_RATE;
  231. else if (strcmp(employeeData[i].taxState, "VT") == 0)
  232. employeeData[i].stateTax = employeeData[i].grossPay * VT_TAX_RATE;
  233. else if (strcmp(employeeData[i].taxState, "CA") == 0)
  234. employeeData[i].stateTax = employeeData[i].grossPay * CA_TAX_RATE;
  235. else
  236. employeeData[i].stateTax = employeeData[i].grossPay * DEFAULT_TAX_RATE;
  237. }
  238. }
  239.  
  240. //********************************************************
  241. // calcFedTax
  242. // Purpose: Computes federal tax (flat rate)
  243. //********************************************************
  244. void calcFedTax(struct employee employeeData[], int theSize)
  245. {
  246. int i;
  247. for (i = 0; i < theSize; ++i)
  248. {
  249. employeeData[i].fedTax = employeeData[i].grossPay * FED_TAX_RATE;
  250. }
  251. }
  252.  
  253. //********************************************************
  254. // calcNetPay
  255. // Purpose: Computes take-home pay after taxes
  256. //********************************************************
  257. void calcNetPay(struct employee employeeData[], int theSize)
  258. {
  259. int i;
  260. float totalTaxes;
  261.  
  262. for (i = 0; i < theSize; ++i)
  263. {
  264. totalTaxes = employeeData[i].stateTax + employeeData[i].fedTax;
  265. employeeData[i].netPay = employeeData[i].grossPay - totalTaxes;
  266. }
  267. }
  268.  
  269. //********************************************************
  270. // printHeader
  271. // Purpose: Prints formatted table header
  272. //********************************************************
  273. void printHeader(void)
  274. {
  275. printf("\n\n*** Pay Calculator ***\n");
  276. printf("\n--------------------------------------------------------------");
  277. printf("-------------------");
  278. printf("\nName Tax Clock# Wage Hours OT Gross ");
  279. printf(" State Fed Net");
  280. printf("\n State Pay ");
  281. printf(" Tax Tax Pay");
  282. printf("\n--------------------------------------------------------------");
  283. printf("-------------------");
  284. }
  285.  
  286. //********************************************************
  287. // printEmp
  288. // Purpose: Prints each employee's computed payroll data
  289. //********************************************************
  290. void printEmp(struct employee employeeData[], int theSize)
  291. {
  292. int i;
  293. char fullName[FIRST_NAME_SIZE + LAST_NAME_SIZE + 1];
  294.  
  295. for (i = 0; i < theSize; ++i)
  296. {
  297. // Build full name string
  298. strcpy(fullName, employeeData[i].empName.firstName);
  299. strcat(fullName, " ");
  300. strcat(fullName, employeeData[i].empName.lastName);
  301.  
  302. printf("\n%-20.20s %-2.2s %06li %5.2f %4.1f %4.1f %7.2f %6.2f %7.2f %8.2f",
  303. fullName,
  304. employeeData[i].taxState,
  305. employeeData[i].clockNumber,
  306. employeeData[i].wageRate,
  307. employeeData[i].hours,
  308. employeeData[i].overtimeHrs,
  309. employeeData[i].grossPay,
  310. employeeData[i].stateTax,
  311. employeeData[i].fedTax,
  312. employeeData[i].netPay);
  313. }
  314. }
  315.  
  316. //********************************************************
  317. // printEmpStatistics
  318. // Purpose: Prints totals, averages, min, and max values
  319. //********************************************************
  320. void printEmpStatistics(struct totals employeeTotals,
  321. struct min_max employeeMinMax,
  322. int theSize)
  323. {
  324. printf("\n--------------------------------------------------------------");
  325. printf("-------------------");
  326.  
  327. // Totals
  328. printf("\nTotals: %5.2f %5.1f %5.1f %7.2f %6.2f %7.2f %8.2f",
  329. employeeTotals.total_wageRate,
  330. employeeTotals.total_hours,
  331. employeeTotals.total_overtimeHrs,
  332. employeeTotals.total_grossPay,
  333. employeeTotals.total_stateTax,
  334. employeeTotals.total_fedTax,
  335. employeeTotals.total_netPay);
  336.  
  337. // Averages
  338. if (theSize > 0)
  339. {
  340. printf("\nAverages: %5.2f %5.1f %5.1f %7.2f %6.2f %7.2f %8.2f",
  341. employeeTotals.total_wageRate / theSize,
  342. employeeTotals.total_hours / theSize,
  343. employeeTotals.total_overtimeHrs / theSize,
  344. employeeTotals.total_grossPay / theSize,
  345. employeeTotals.total_stateTax / theSize,
  346. employeeTotals.total_fedTax / theSize,
  347. employeeTotals.total_netPay / theSize);
  348. }
  349.  
  350. // Minimum values
  351. printf("\nMinimum: %5.2f %5.1f %5.1f %7.2f %6.2f %7.2f %8.2f",
  352. employeeMinMax.min_wageRate,
  353. employeeMinMax.min_hours,
  354. employeeMinMax.min_overtimeHrs,
  355. employeeMinMax.min_grossPay,
  356. employeeMinMax.min_stateTax,
  357. employeeMinMax.min_fedTax,
  358. employeeMinMax.min_netPay);
  359.  
  360. // Maximum values
  361. printf("\nMaximum: %5.2f %5.1f %5.1f %7.2f %6.2f %7.2f %8.2f",
  362. employeeMinMax.max_wageRate,
  363. employeeMinMax.max_hours,
  364. employeeMinMax.max_overtimeHrs,
  365. employeeMinMax.max_grossPay,
  366. employeeMinMax.max_stateTax,
  367. employeeMinMax.max_fedTax,
  368. employeeMinMax.max_netPay);
  369. }
  370.  
  371. //********************************************************
  372. // calcEmployeeTotals
  373. // Purpose: Accumulates totals for all float fields
  374. //********************************************************
  375. struct totals calcEmployeeTotals(struct employee employeeData[],
  376. struct totals employeeTotals,
  377. int theSize)
  378. {
  379. int i;
  380.  
  381. for (i = 0; i < theSize; ++i)
  382. {
  383. employeeTotals.total_wageRate += employeeData[i].wageRate;
  384. employeeTotals.total_hours += employeeData[i].hours;
  385. employeeTotals.total_overtimeHrs += employeeData[i].overtimeHrs;
  386. employeeTotals.total_grossPay += employeeData[i].grossPay;
  387. employeeTotals.total_stateTax += employeeData[i].stateTax;
  388. employeeTotals.total_fedTax += employeeData[i].fedTax;
  389. employeeTotals.total_netPay += employeeData[i].netPay;
  390. }
  391.  
  392. return employeeTotals;
  393. }
  394.  
  395. //********************************************************
  396. // calcEmployeeMinMax
  397. // Purpose: Determines min and max values for all fields
  398. //********************************************************
  399. struct min_max calcEmployeeMinMax(struct employee employeeData[],
  400. struct min_max employeeMinMax,
  401. int theSize)
  402. {
  403. int i;
  404.  
  405. // Initialize min and max with first employee
  406. employeeMinMax.min_wageRate = employeeMinMax.max_wageRate = employeeData[0].wageRate;
  407. employeeMinMax.min_hours = employeeMinMax.max_hours = employeeData[0].hours;
  408. employeeMinMax.min_overtimeHrs = employeeMinMax.max_overtimeHrs = employeeData[0].overtimeHrs;
  409. employeeMinMax.min_grossPay = employeeMinMax.max_grossPay = employeeData[0].grossPay;
  410. employeeMinMax.min_stateTax = employeeMinMax.max_stateTax = employeeData[0].stateTax;
  411. employeeMinMax.min_fedTax = employeeMinMax.max_fedTax = employeeData[0].fedTax;
  412. employeeMinMax.min_netPay = employeeMinMax.max_netPay = employeeData[0].netPay;
  413.  
  414. // Compare remaining employees
  415. for (i = 1; i < theSize; ++i)
  416. {
  417. if (employeeData[i].wageRate < employeeMinMax.min_wageRate)
  418. employeeMinMax.min_wageRate = employeeData[i].wageRate;
  419. if (employeeData[i].wageRate > employeeMinMax.max_wageRate)
  420. employeeMinMax.max_wageRate = employeeData[i].wageRate;
  421.  
  422. if (employeeData[i].hours < employeeMinMax.min_hours)
  423. employeeMinMax.min_hours = employeeData[i].hours;
  424. if (employeeData[i].hours > employeeMinMax.max_hours)
  425. employeeMinMax.max_hours = employeeData[i].hours;
  426.  
  427. if (employeeData[i].overtimeHrs < employeeMinMax.min_overtimeHrs)
  428. employeeMinMax.min_overtimeHrs = employeeData[i].overtimeHrs;
  429. if (employeeData[i].overtimeHrs > employeeMinMax.max_overtimeHrs)
  430. employeeMinMax.max_overtimeHrs = employeeData[i].overtimeHrs;
  431.  
  432. if (employeeData[i].grossPay < employeeMinMax.min_grossPay)
  433. employeeMinMax.min_grossPay = employeeData[i].grossPay;
  434. if (employeeData[i].grossPay > employeeMinMax.max_grossPay)
  435. employeeMinMax.max_grossPay = employeeData[i].grossPay;
  436.  
  437. if (employeeData[i].stateTax < employeeMinMax.min_stateTax)
  438. employeeMinMax.min_stateTax = employeeData[i].stateTax;
  439. if (employeeData[i].stateTax > employeeMinMax.max_stateTax)
  440. employeeMinMax.max_stateTax = employeeData[i].stateTax;
  441.  
  442. if (employeeData[i].fedTax < employeeMinMax.min_fedTax)
  443. employeeMinMax.min_fedTax = employeeData[i].fedTax;
  444. if (employeeData[i].fedTax > employeeMinMax.max_fedTax)
  445. employeeMinMax.max_fedTax = employeeData[i].fedTax;
  446.  
  447. if (employeeData[i].netPay < employeeMinMax.min_netPay)
  448. employeeMinMax.min_netPay = employeeData[i].netPay;
  449. if (employeeData[i].netPay > employeeMinMax.max_netPay)
  450. employeeMinMax.max_netPay = employeeData[i].netPay;
  451. }
  452.  
  453. return employeeMinMax;
  454. }
Success #stdin #stdout 0.01s 5268KB
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