fork download
  1. //********************************************************
  2. //
  3. // Assignment 7 - Structures and Strings
  4. //
  5. // Name: Rose Samedi
  6. //
  7. // Class: C Programming, Fall 2025
  8. //
  9. // Date: 11/16/2025
  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. // Call by Reference design
  20. //
  21. //********************************************************
  22.  
  23. // necessary header files
  24. #include <stdio.h>
  25. #include <string.h>
  26. #include <ctype.h>
  27.  
  28. // define constants
  29. #define SIZE 5
  30. #define STD_HOURS 40.0
  31. #define OT_RATE 1.5
  32. #define MA_TAX_RATE 0.05
  33. #define NH_TAX_RATE 0.0
  34. #define VT_TAX_RATE 0.06
  35. #define CA_TAX_RATE 0.07
  36. #define DEFAULT_TAX_RATE 0.08
  37. #define NAME_SIZE 20
  38. #define TAX_STATE_SIZE 3
  39. #define FED_TAX_RATE 0.25
  40. #define FIRST_NAME_SIZE 10
  41. #define LAST_NAME_SIZE 10
  42.  
  43. // Define a structure type to store an employee name
  44. struct name
  45. {
  46. char firstName[FIRST_NAME_SIZE];
  47. char lastName [LAST_NAME_SIZE];
  48. };
  49.  
  50. // Define a structure type to pass employee data between functions
  51. struct employee
  52. {
  53. struct name empName;
  54. char taxState [TAX_STATE_SIZE];
  55. long int clockNumber;
  56. float wageRate;
  57. float hours;
  58. float overtimeHrs;
  59. float grossPay;
  60. float stateTax;
  61. float fedTax;
  62. float netPay;
  63. };
  64.  
  65. // this structure type defines the totals of all floating point items
  66. // so they can be totaled and used also to calculate averages
  67. struct totals
  68. {
  69. float total_wageRate;
  70. float total_hours;
  71. float total_overtimeHrs;
  72. float total_grossPay;
  73. float total_stateTax;
  74. float total_fedTax;
  75. float total_netPay;
  76. };
  77.  
  78. // Function prototypes
  79. void calculatePay(struct employee *emp);
  80. void calculateTaxes(struct employee *emp);
  81. void updateTotals(struct totals *tots, const struct employee *emp);
  82. void findMinMax(const struct employee emps[], int size, struct employee *minGrossPayEmp, struct employee *maxGrossPayEmp);
  83. void displayOutput(const struct employee emps[], int size, const struct totals *tots, const struct employee *minGrossPayEmp, const struct employee *maxGrossPayEmp);
  84. void convertToUpper(char *str);
  85.  
  86. int main(void)
  87. {
  88. // Initialize employee data for SIZE employees
  89. struct employee employees[SIZE] = {
  90. {{"Pat", "Curry"}, "MA", 980124L, 10.60, 51.5, 0.0, 0.0, 0.0, 0.0, 0.0},
  91. {{"Leo", "Demers"}, "NH", 980221L, 10.60, 40.0, 0.0, 0.0, 0.0, 0.0, 0.0},
  92. {{"Kim", "Wong"}, "VT", 980345L, 10.60, 41.2, 0.0, 0.0, 0.0, 0.0, 0.0},
  93. {{"Jack", "Gourdeau"}, "MA", 980400L, 10.60, 60.0, 0.0, 0.0, 0.0, 0.0, 0.0},
  94. {{"Phil", "Lacourse"}, "CA", 980512L, 10.60, 38.5, 0.0, 0.0, 0.0, 0.0, 0.0}
  95. };
  96.  
  97. struct totals allTotals = {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0};
  98. struct employee minGrossPayEmp;
  99. struct employee maxGrossPayEmp;
  100. int i;
  101.  
  102. // Process each employee
  103. for (i = 0; i < SIZE; i++)
  104. {
  105. calculatePay(&employees[i]);
  106. calculateTaxes(&employees[i]);
  107. updateTotals(&allTotals, &employees[i]);
  108. }
  109.  
  110. // Find min/max after all calculations are done
  111. findMinMax(employees, SIZE, &minGrossPayEmp, &maxGrossPayEmp);
  112.  
  113. // Display the results
  114. displayOutput(employees, SIZE, &allTotals, &minGrossPayEmp, &maxGrossPayEmp);
  115.  
  116. return 0;
  117. }
  118.  
  119. // Function to calculate overtime hours and gross pay for a single employee
  120. void calculatePay(struct employee *emp)
  121. {
  122. if (emp->hours > STD_HOURS)
  123. {
  124. emp->overtimeHrs = emp->hours - STD_HOURS;
  125. emp->grossPay = (STD_HOURS * emp->wageRate) + (emp->overtimeHrs * emp->wageRate * OT_RATE);
  126. }
  127. else
  128. {
  129. emp->overtimeHrs = 0.0;
  130. emp->grossPay = emp->hours * emp->wageRate;
  131. }
  132. }
  133.  
  134. // Function to calculate state tax, federal tax, and net pay for a single employee
  135. void calculateTaxes(struct employee *emp)
  136. {
  137. float stateTaxRate;
  138.  
  139. // Convert state code to uppercase for consistent comparison
  140. convertToUpper(emp->taxState);
  141.  
  142. if (strcmp(emp->taxState, "MA") == 0)
  143. {
  144. stateTaxRate = MA_TAX_RATE;
  145. }
  146. else if (strcmp(emp->taxState, "NH") == 0)
  147. {
  148. stateTaxRate = NH_TAX_RATE;
  149. }
  150. else if (strcmp(emp->taxState, "VT") == 0)
  151. {
  152. stateTaxRate = VT_TAX_RATE;
  153. }
  154. else if (strcmp(emp->taxState, "CA") == 0)
  155. {
  156. stateTaxRate = CA_TAX_RATE;
  157. }
  158. else
  159. {
  160. stateTaxRate = DEFAULT_TAX_RATE;
  161. }
  162.  
  163. emp->stateTax = emp->grossPay * stateTaxRate;
  164. emp->fedTax = emp->grossPay * FED_TAX_RATE;
  165. emp->netPay = emp->grossPay - emp->stateTax - emp->fedTax;
  166. }
  167.  
  168. // Function to update the running totals structure
  169. void updateTotals(struct totals *tots, const struct employee *emp)
  170. {
  171. tots->total_wageRate += emp->wageRate;
  172. tots->total_hours += emp->hours;
  173. tots->total_overtimeHrs += emp->overtimeHrs;
  174. tots->total_grossPay += emp->grossPay;
  175. tots->total_stateTax += emp->stateTax;
  176. tots->total_fedTax += emp->fedTax;
  177. tots->total_netPay += emp->netPay;
  178. }
  179.  
  180. // Function to find the employees with minimum and maximum gross pay
  181. void findMinMax(const struct employee emps[], int size, struct employee *minGrossPayEmp, struct employee *maxGrossPayEmp)
  182. {
  183. int i;
  184. // Initialize min/max to the first employee's data
  185. *minGrossPayEmp = emps[0];
  186. *maxGrossPayEmp = emps[0];
  187.  
  188. for (i = 1; i < size; i++)
  189. {
  190. if (emps[i].grossPay < minGrossPayEmp->grossPay)
  191. {
  192. *minGrossPayEmp = emps[i];
  193. }
  194. if (emps[i].grossPay > maxGrossPayEmp->grossPay)
  195. {
  196. *maxGrossPayEmp = emps[i];
  197. }
  198. }
  199. }
  200.  
  201. // Function to display all employee details, totals, averages, and min/max
  202. void displayOutput(const struct employee emps[], int size, const struct totals *tots, const struct employee *minGrossPayEmp, const struct employee *maxGrossPayEmp)
  203. {
  204. int i;
  205.  
  206. printf("\n\n-------------------------------------------------------------------------------------------------------------------\n");
  207. printf("Name\t\tState\tClock #\t Wage\t Hours\t OT Hrs\t Gross Pay\t State Tax\t Fed Tax\t Net Pay\n");
  208. printf("-------------------------------------------------------------------------------------------------------------------\n");
  209.  
  210. for (i = 0; i < size; i++)
  211. {
  212. printf("%s %s\t%s\t%ld\t%.2f\t%.1f\t%.1f\t$%.2f\t$%.2f\t$%.2f\t$%.2f\n",
  213. emps[i].empName.firstName, emps[i].empName.lastName,
  214. emps[i].taxState, emps[i].clockNumber, emps[i].wageRate,
  215. emps[i].hours, emps[i].overtimeHrs, emps[i].grossPay,
  216. emps[i].stateTax, emps[i].fedTax, emps[i].netPay);
  217. }
  218.  
  219. printf("-------------------------------------------------------------------------------------------------------------------\n");
  220.  
  221. // Display Totals
  222. printf("Totals:\t\t\t\t\t%.1f\t%.1f\t$%.2f\t$%.2f\t$%.2f\t$%.2f\n",
  223. tots->total_hours, tots->total_overtimeHrs, tots->total_grossPay,
  224. tots->total_stateTax, tots->total_fedTax, tots->total_netPay);
  225.  
  226. // Display Averages (calculated on the fly)
  227. printf("Averages:\t\t\t\t%.1f\t%.1f\t$%.2f\t$%.2f\t$%.2f\t$%.2f\n",
  228. tots->total_hours / size, tots->total_overtimeHrs / size, tots->total_grossPay / size,
  229. tots->total_stateTax / size, tots->total_fedTax / size, tots->total_netPay / size);
  230.  
  231. printf("-------------------------------------------------------------------------------------------------------------------\n");
  232.  
  233. // Display Min/Max Gross Pay employees
  234. printf("\nMinimum Gross Pay Employee: %s %s (Clock #%ld) - $%.2f\n",
  235. minGrossPayEmp->empName.firstName, minGrossPayEmp->empName.lastName,
  236. minGrossPayEmp->clockNumber, minGrossPayEmp->grossPay);
  237.  
  238. printf("Maximum Gross Pay Employee: %s %s (Clock #%ld) - $%.2f\n\n",
  239. maxGrossPayEmp->empName.firstName, maxGrossPayEmp->empName.lastName,
  240. maxGrossPayEmp->clockNumber, maxGrossPayEmp->grossPay);
  241. }
  242.  
  243. // Function to convert a string to uppercase
  244. void convertToUpper(char *str) {
  245. for (int i = 0; str[i] != '\0'; i++) {
  246. str[i] = toupper((unsigned char)str[i]);
  247. }
  248. }
Success #stdin #stdout 0.01s 5324KB
stdin
51.0
42.5
37.0
45.0
40.0
stdout

-------------------------------------------------------------------------------------------------------------------
Name		State	Clock #	 Wage	 Hours	 OT Hrs	 Gross Pay	 State Tax	 Fed Tax	 Net Pay
-------------------------------------------------------------------------------------------------------------------
Pat Curry	MA	980124	10.60	51.5	11.5	$606.85	$30.34	$151.71	$424.79
Leo Demers	NH	980221	10.60	40.0	0.0	$424.00	$0.00	$106.00	$318.00
Kim Wong	VT	980345	10.60	41.2	1.2	$443.08	$26.58	$110.77	$305.73
Jack Gourdeau	MA	980400	10.60	60.0	20.0	$742.00	$37.10	$185.50	$519.40
Phil Lacourse	CA	980512	10.60	38.5	0.0	$408.10	$28.57	$102.03	$277.51
-------------------------------------------------------------------------------------------------------------------
Totals:					231.2	32.7	$2624.03	$122.59	$656.01	$1845.43
Averages:				46.2	6.5	$524.81	$24.52	$131.20	$369.09
-------------------------------------------------------------------------------------------------------------------

Minimum Gross Pay Employee: Phil Lacourse (Clock #980512) - $408.10
Maximum Gross Pay Employee: Jack Gourdeau (Clock #980400) - $742.00