fork download
  1. //********************************************************
  2. // Assignment 7 - Structures and Strings
  3. // Name: Robert Hyde
  4. // Class: C Programming, Fall 2026
  5. // Date: 10/30/2026
  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. // This assignment also adds the employee name, their tax state,
  12. // and calculates the state tax, federal tax, and net pay. It
  13. // also calculates totals, averages, minimum, and maximum values.
  14. //
  15. // Call by Reference design
  16. //********************************************************
  17.  
  18. #include <stdio.h>
  19. #include <string.h>
  20. #include <ctype.h>
  21.  
  22. #define SIZE 5
  23. #define STD_HOURS 40.0
  24. #define OT_RATE 1.5
  25. #define MA_TAX_RATE 0.05
  26. #define NH_TAX_RATE 0.0
  27. #define VT_TAX_RATE 0.06
  28. #define CA_TAX_RATE 0.07
  29. #define DEFAULT_TAX_RATE 0.08
  30. #define FED_TAX_RATE 0.25
  31. #define FIRST_NAME_SIZE 10
  32. #define LAST_NAME_SIZE 10
  33. #define TAX_STATE_SIZE 3
  34.  
  35. struct name {
  36. char firstName[FIRST_NAME_SIZE];
  37. char lastName[LAST_NAME_SIZE];
  38. };
  39.  
  40. struct employee {
  41. struct name empName;
  42. char taxState[TAX_STATE_SIZE];
  43. long int clockNumber;
  44. float wageRate, hours, overtimeHrs;
  45. float grossPay, stateTax, fedTax, netPay;
  46. };
  47.  
  48. struct totals {
  49. float total_wageRate, total_hours, total_overtimeHrs;
  50. float total_grossPay, total_stateTax, total_fedTax, total_netPay;
  51. };
  52.  
  53. struct min_max {
  54. float min_wageRate, min_hours, min_overtimeHrs;
  55. float min_grossPay, min_stateTax, min_fedTax, min_netPay;
  56. float max_wageRate, max_hours, max_overtimeHrs;
  57. float max_grossPay, max_stateTax, max_fedTax, max_netPay;
  58. };
  59.  
  60. void getHours(struct employee data[], int size);
  61. void calcOvertimeHrs(struct employee data[], int size);
  62. void calcGrossPay(struct employee data[], int size);
  63. void calcStateTax(struct employee data[], int size);
  64. void calcFedTax(struct employee data[], int size);
  65. void calcNetPay(struct employee data[], int size);
  66. void printHeader(void);
  67. void printEmp(struct employee data[], int size);
  68.  
  69. struct totals calcEmployeeTotals(
  70. struct employee data[], struct totals totals, int size);
  71.  
  72. struct min_max calcEmployeeMinMax(
  73. struct employee data[], struct min_max mm, int size);
  74.  
  75. void printEmpStatistics(struct totals totals, struct min_max mm, int size);
  76.  
  77. int main(void)
  78. {
  79. struct employee employeeData[SIZE] = {
  80. {{"Connie", "Cobol"}, "MA", 98401, 10.60},
  81. {{"Mary", "Apl"}, "NH", 526488, 9.75},
  82. {{"Frank", "Fortran"}, "VT", 765349, 10.50},
  83. {{"Jeff", "Ada"}, "NY", 34645, 12.25},
  84. {{"Anton", "Pascal"}, "CA", 127615, 8.35}
  85. };
  86.  
  87. struct totals employeeTotals = {0};
  88. struct min_max employeeMinMax = {0};
  89.  
  90. getHours(employeeData, SIZE);
  91. calcOvertimeHrs(employeeData, SIZE);
  92. calcGrossPay(employeeData, SIZE);
  93. calcStateTax(employeeData, SIZE);
  94. calcFedTax(employeeData, SIZE);
  95. calcNetPay(employeeData, SIZE);
  96.  
  97. employeeTotals = calcEmployeeTotals(employeeData, employeeTotals, SIZE);
  98. employeeMinMax = calcEmployeeMinMax(employeeData, employeeMinMax, SIZE);
  99.  
  100. printHeader();
  101. printEmp(employeeData, SIZE);
  102. printEmpStatistics(employeeTotals, employeeMinMax, SIZE);
  103.  
  104. printf("\n");
  105. return 0;
  106. }
  107.  
  108. void getHours(struct employee data[], int size)
  109. {
  110. int i;
  111. for (i = 0; i < size; i++) {
  112. printf("\nEnter hours worked by emp # %06li: ", data[i].clockNumber);
  113. scanf("%f", &data[i].hours);
  114. }
  115. }
  116.  
  117. void calcOvertimeHrs(struct employee data[], int size)
  118. {
  119. int i;
  120. for (i = 0; i < size; i++) {
  121. data[i].overtimeHrs =
  122. data[i].hours > STD_HOURS ? data[i].hours - STD_HOURS : 0.0;
  123. }
  124. }
  125.  
  126. void calcGrossPay(struct employee data[], int size)
  127. {
  128. int i;
  129. float normalPay, overtimePay;
  130.  
  131. for (i = 0; i < size; i++) {
  132. normalPay = data[i].wageRate *
  133. (data[i].hours - data[i].overtimeHrs);
  134. overtimePay = data[i].overtimeHrs * OT_RATE * data[i].wageRate;
  135. data[i].grossPay = normalPay + overtimePay;
  136. }
  137. }
  138.  
  139. void calcStateTax(struct employee data[], int size)
  140. {
  141. int i;
  142. float rate;
  143.  
  144. for (i = 0; i < size; i++) {
  145. data[i].taxState[0] =
  146. (char)toupper((unsigned char)data[i].taxState[0]);
  147. data[i].taxState[1] =
  148. (char)toupper((unsigned char)data[i].taxState[1]);
  149.  
  150. if (strcmp(data[i].taxState, "MA") == 0)
  151. rate = MA_TAX_RATE;
  152. else if (strcmp(data[i].taxState, "NH") == 0)
  153. rate = NH_TAX_RATE;
  154. else if (strcmp(data[i].taxState, "VT") == 0)
  155. rate = VT_TAX_RATE;
  156. else if (strcmp(data[i].taxState, "CA") == 0)
  157. rate = CA_TAX_RATE;
  158. else
  159. rate = DEFAULT_TAX_RATE;
  160.  
  161. data[i].stateTax = data[i].grossPay * rate;
  162. }
  163. }
  164.  
  165. void calcFedTax(struct employee data[], int size)
  166. {
  167. int i;
  168. for (i = 0; i < size; i++)
  169. data[i].fedTax = data[i].grossPay * FED_TAX_RATE;
  170. }
  171.  
  172. void calcNetPay(struct employee data[], int size)
  173. {
  174. int i;
  175. for (i = 0; i < size; i++)
  176. data[i].netPay = data[i].grossPay - data[i].stateTax - data[i].fedTax;
  177. }
  178.  
  179. void printHeader(void)
  180. {
  181. printf("\n\n*** Pay Calculator ***\n");
  182. printf("\n--------------------------------------------------------------");
  183. printf("-------------------");
  184. printf("\nName Tax Clock# Wage Hours OT Gross ");
  185. printf(" State Fed Net");
  186. printf("\n State Pay ");
  187. printf("Tax Tax Pay");
  188. printf("\n--------------------------------------------------------------");
  189. printf("-------------------");
  190. }
  191.  
  192. void printEmp(struct employee data[], int size)
  193. {
  194. int i;
  195. char name[FIRST_NAME_SIZE + LAST_NAME_SIZE + 1];
  196.  
  197. for (i = 0; i < size; i++) {
  198. strcpy(name, data[i].empName.firstName);
  199. strcat(name, " ");
  200. strcat(name, data[i].empName.lastName);
  201.  
  202. printf("\n%-20.20s %-2.2s %06li %5.2f %4.1f %4.1f "
  203. "%7.2f %6.2f %7.2f %8.2f",
  204. name, data[i].taxState, data[i].clockNumber,
  205. data[i].wageRate, data[i].hours, data[i].overtimeHrs,
  206. data[i].grossPay, data[i].stateTax, data[i].fedTax,
  207. data[i].netPay);
  208. }
  209. }
  210.  
  211. struct totals calcEmployeeTotals(
  212. struct employee data[], struct totals t, int size)
  213. {
  214. int i;
  215. for (i = 0; i < size; i++) {
  216. t.total_wageRate += data[i].wageRate;
  217. t.total_hours += data[i].hours;
  218. t.total_overtimeHrs += data[i].overtimeHrs;
  219. t.total_grossPay += data[i].grossPay;
  220. t.total_stateTax += data[i].stateTax;
  221. t.total_fedTax += data[i].fedTax;
  222. t.total_netPay += data[i].netPay;
  223. }
  224. return t;
  225. }
  226.  
  227. struct min_max calcEmployeeMinMax(
  228. struct employee data[], struct min_max m, int size)
  229. {
  230. int i;
  231.  
  232. if (size <= 0)
  233. return m;
  234.  
  235. m.min_wageRate = m.max_wageRate = data[0].wageRate;
  236. m.min_hours = m.max_hours = data[0].hours;
  237. m.min_overtimeHrs = m.max_overtimeHrs = data[0].overtimeHrs;
  238. m.min_grossPay = m.max_grossPay = data[0].grossPay;
  239. m.min_stateTax = m.max_stateTax = data[0].stateTax;
  240. m.min_fedTax = m.max_fedTax = data[0].fedTax;
  241. m.min_netPay = m.max_netPay = data[0].netPay;
  242.  
  243. for (i = 1; i < size; i++) {
  244. if (data[i].wageRate < m.min_wageRate) m.min_wageRate = data[i].wageRate;
  245. if (data[i].wageRate > m.max_wageRate) m.max_wageRate = data[i].wageRate;
  246.  
  247. if (data[i].hours < m.min_hours) m.min_hours = data[i].hours;
  248. if (data[i].hours > m.max_hours) m.max_hours = data[i].hours;
  249.  
  250. if (data[i].overtimeHrs < m.min_overtimeHrs) m.min_overtimeHrs = data[i].overtimeHrs;
  251. if (data[i].overtimeHrs > m.max_overtimeHrs) m.max_overtimeHrs = data[i].overtimeHrs;
  252.  
  253. if (data[i].grossPay < m.min_grossPay) m.min_grossPay = data[i].grossPay;
  254. if (data[i].grossPay > m.max_grossPay) m.max_grossPay = data[i].grossPay;
  255.  
  256. if (data[i].stateTax < m.min_stateTax) m.min_stateTax = data[i].stateTax;
  257. if (data[i].stateTax > m.max_stateTax) m.max_stateTax = data[i].stateTax;
  258.  
  259. if (data[i].fedTax < m.min_fedTax) m.min_fedTax = data[i].fedTax;
  260. if (data[i].fedTax > m.max_fedTax) m.max_fedTax = data[i].fedTax;
  261.  
  262. if (data[i].netPay < m.min_netPay) m.min_netPay = data[i].netPay;
  263. if (data[i].netPay > m.max_netPay) m.max_netPay = data[i].netPay;
  264. }
  265. return m;
  266. }
  267.  
  268. void printEmpStatistics(struct totals t, struct min_max m, int size)
  269. {
  270. printf("\n--------------------------------------------------------------");
  271. printf("-------------------");
  272.  
  273. printf("\nTotals: %5.2f %5.1f %5.1f %7.2f %6.2f %7.2f %8.2f",
  274. t.total_wageRate, t.total_hours, t.total_overtimeHrs,
  275. t.total_grossPay, t.total_stateTax, t.total_fedTax, t.total_netPay);
  276.  
  277. if (size > 0) {
  278. printf("\nAverages: %5.2f %5.1f %5.1f %7.2f %6.2f %7.2f %8.2f",
  279. t.total_wageRate / size, t.total_hours / size,
  280. t.total_overtimeHrs / size, t.total_grossPay / size,
  281. t.total_stateTax / size, t.total_fedTax / size,
  282. t.total_netPay / size);
  283. }
  284.  
  285. printf("\nMinimum: %5.2f %5.1f %5.1f %7.2f %6.2f %7.2f %8.2f",
  286. m.min_wageRate, m.min_hours, m.min_overtimeHrs,
  287. m.min_grossPay, m.min_stateTax, m.min_fedTax, m.min_netPay);
  288.  
  289. printf("\nMaximum: %5.2f %5.1f %5.1f %7.2f %6.2f %7.2f %8.2f",
  290. m.max_wageRate, m.max_hours, m.max_overtimeHrs,
  291. m.max_grossPay, m.max_stateTax, m.max_fedTax, m.max_netPay);
  292. }
Success #stdin #stdout 0s 5328KB
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