fork(1) download
  1. //********************************************************
  2. //
  3. // Assignment 6 - Structures
  4. //
  5. // Name: Seth Hin
  6. //
  7. // Class: C Programming, Spring 2026
  8. //
  9. // Date: March 5, 2026
  10. //
  11. // Description: Program which determines overtime and
  12. // gross pay for a set of employees using an array of
  13. // structures. The program prompts the user for hours
  14. // worked, calculates overtime hours and gross pay,
  15. // and prints the payroll report.
  16. //
  17. // Call by Value design
  18. //
  19. //********************************************************
  20.  
  21. //-------------------- Includes -------------------------
  22.  
  23. #include <stdio.h>
  24.  
  25. #define SIZE 5 // number of employees
  26. #define STD_HOURS 40.0 // standard hours before overtime
  27. #define OT_RATE 1.5 // overtime pay multiplier
  28.  
  29. //-------------------- Structure Definition ------------------
  30.  
  31. // Structure to store employee payroll information
  32. struct employee
  33. {
  34. long int clockNumber; // Employee clock number
  35. float wageRate; // Hourly wage rate
  36. float hours; // Hours worked in the week
  37. float overtimeHrs; // Overtime hours worked
  38. float grossPay; // Total gross pay
  39. };
  40.  
  41. //-------------------- Function Prototypes -------------------
  42.  
  43. float getHours(long int clockNumber);
  44. float calcOvertime(float hours);
  45. float calcGross(float hours, float wageRate, float overtimeHrs);
  46. void printHeader(void);
  47. void printEmp(long int clockNumber, float wageRate, float hours,
  48. float overtimeHrs, float grossPay);
  49. void calcTotals(struct employee empData[], int size,
  50. float *totalWage, float *totalHours,
  51. float *totalOvertime, float *totalGross);
  52. void printTotalsAndAverages(float totalWage, float totalHours,
  53. float totalOvertime, float totalGross,
  54. int size);
  55.  
  56. int main()
  57. {
  58. // Array of structures to store employee payroll data
  59. struct employee employeeData[SIZE] = {
  60. {98401, 10.60},
  61. {526488, 9.75},
  62. {765349, 10.50},
  63. {34645, 12.25},
  64. {127615, 8.35}
  65. };
  66.  
  67. int i; // Loop counter
  68.  
  69. // Loop through each employee to gather hours and calculate payroll
  70. for (i = 0; i < SIZE; ++i)
  71. {
  72. // Prompt the user to enter hours worked
  73. employeeData[i].hours = getHours(employeeData[i].clockNumber);
  74.  
  75. // Calculate overtime hours
  76. employeeData[i].overtimeHrs = calcOvertime(employeeData[i].hours);
  77.  
  78. // Calculate gross pay
  79. employeeData[i].grossPay = calcGross(employeeData[i].hours,
  80. employeeData[i].wageRate,
  81. employeeData[i].overtimeHrs);
  82. }
  83.  
  84. // Print payroll report header
  85. printHeader();
  86.  
  87. // Print each employee's payroll information
  88. for (i = 0; i < SIZE; ++i)
  89. {
  90. printEmp(employeeData[i].clockNumber,
  91. employeeData[i].wageRate,
  92. employeeData[i].hours,
  93. employeeData[i].overtimeHrs,
  94. employeeData[i].grossPay);
  95. }
  96.  
  97. // Variables to store totals
  98. float totalWage, totalHours, totalOvertime, totalGross;
  99.  
  100. // Calculate totals
  101. calcTotals(employeeData, SIZE, &totalWage, &totalHours, &totalOvertime, &totalGross);
  102.  
  103. // Print totals and averages
  104. printTotalsAndAverages(totalWage, totalHours, totalOvertime, totalGross, SIZE);
  105.  
  106. return 0;
  107. }
  108.  
  109. //**************************************************************
  110. // Function: getHours
  111. // Purpose: Prompts the user to enter hours worked for an employee
  112. // Parameters: clockNumber - employee ID
  113. // Returns: hours worked (float)
  114. //**************************************************************
  115. float getHours(long int clockNumber)
  116. {
  117. float hoursWorked; // Local variable for input
  118. printf("\nEnter hours worked by emp # %06li: ", clockNumber);
  119. scanf("%f", &hoursWorked);
  120. return hoursWorked;
  121. }
  122.  
  123. //**************************************************************
  124. // Function: calcOvertime
  125. // Purpose: Calculates overtime hours
  126. // Parameters: hours - total hours worked
  127. // Returns: overtime hours (float)
  128. //**************************************************************
  129. float calcOvertime(float hours)
  130. {
  131. float overtime; // Local variable to store overtime
  132.  
  133. if (hours > STD_HOURS)
  134. overtime = hours - STD_HOURS;
  135. else
  136. overtime = 0.0;
  137.  
  138. return overtime;
  139. }
  140.  
  141. //**************************************************************
  142. // Function: calcGross
  143. // Purpose: Calculates total gross pay including overtime
  144. // Parameters: hours, wageRate, overtimeHrs
  145. // Returns: gross pay (float)
  146. //**************************************************************
  147. float calcGross(float hours, float wageRate, float overtimeHrs)
  148. {
  149. float regularPay = (hours - overtimeHrs) * wageRate; // Pay for regular hours
  150. float overtimePay = overtimeHrs * wageRate * OT_RATE; // Pay for overtime
  151. float gross = regularPay + overtimePay; // Total gross pay
  152. return gross;
  153. }
  154.  
  155. //**************************************************************
  156. // Function: printHeader
  157. // Purpose: Prints payroll report header
  158. // Parameters: none
  159. // Returns: void
  160. //**************************************************************
  161. void printHeader(void)
  162. {
  163. printf("\n\n*** Pay Calculator ***\n");
  164. printf("\nClock# Wage Hours OT Gross\n");
  165. printf("------------------------------------------------\n");
  166. }
  167.  
  168. //**************************************************************
  169. // Function: printEmp
  170. // Purpose: Prints employee payroll info in formatted columns
  171. // Parameters: clockNumber, wageRate, hours, overtimeHrs, grossPay
  172. // Returns: void
  173. //**************************************************************
  174. void printEmp(long int clockNumber, float wageRate, float hours,
  175. float overtimeHrs, float grossPay)
  176. {
  177. printf("\n %06li %5.2f %5.1f %4.1f %8.2f",
  178. clockNumber, wageRate, hours, overtimeHrs, grossPay);
  179. }
  180.  
  181. //**************************************************************
  182. // Function: calcTotals
  183. // Purpose: Calculates total wage, hours, overtime, and gross
  184. // Parameters: empData - array of employees, size - number of employees,
  185. // pointers to totals
  186. // Returns: void (totals passed by reference)
  187. //**************************************************************
  188. void calcTotals(struct employee empData[], int size,
  189. float *totalWage, float *totalHours,
  190. float *totalOvertime, float *totalGross)
  191. {
  192. int i; // Loop counter
  193. *totalWage = 0.0;
  194. *totalHours = 0.0;
  195. *totalOvertime = 0.0;
  196. *totalGross = 0.0;
  197.  
  198. // Loop through employees and sum each column
  199. for (i = 0; i < size; ++i)
  200. {
  201. *totalWage += empData[i].wageRate;
  202. *totalHours += empData[i].hours;
  203. *totalOvertime += empData[i].overtimeHrs;
  204. *totalGross += empData[i].grossPay;
  205. }
  206. }
  207.  
  208. //**************************************************************
  209. // Function: printTotalsAndAverages
  210. // Purpose: Prints totals and averages of wage, hours, OT, and gross
  211. // Parameters: totalWage, totalHours, totalOvertime, totalGross, size
  212. // Returns: void
  213. //**************************************************************
  214. void printTotalsAndAverages(float totalWage, float totalHours,
  215. float totalOvertime, float totalGross,
  216. int size)
  217. {
  218. float avgWage = totalWage / size; // Average wage
  219. float avgHours = totalHours / size; // Average hours
  220. float avgOvertime = totalOvertime / size; // Average overtime
  221. float avgGross = totalGross / size; // Average gross
  222.  
  223. printf("\n------------------------------------------------");
  224. printf("\nTotal %5.2f %5.1f %4.1f %8.2f",
  225. totalWage, totalHours, totalOvertime, totalGross);
  226. printf("\nAverage %5.2f %5.1f %4.1f %8.2f\n",
  227. avgWage, avgHours, avgOvertime, avgGross);
  228. }
Success #stdin #stdout 0s 5288KB
stdin
51.0
42.5
37.0
45.0
0.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 ***

Clock#  Wage  Hours  OT      Gross
------------------------------------------------

 098401 10.60  51.0 11.0   598.90
 526488  9.75  42.5  2.5   426.56
 765349 10.50  37.0  0.0   388.50
 034645 12.25  45.0  5.0   581.88
 127615  8.35   0.0  0.0     0.00
------------------------------------------------
Total   51.45 175.5 18.5  1995.84
Average 10.29  35.1  3.7   399.17