fork(6) 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. //-------------------- Constants ------------------------
  26.  
  27. #define SIZE 5 // number of employees
  28. #define STD_HOURS 40.0 // standard hours before overtime
  29. #define OT_RATE 1.5 // overtime pay multiplier
  30.  
  31. //-------------------- Structure Definition -------------
  32.  
  33. // structure used to store employee payroll information
  34. struct employee
  35. {
  36. long int clockNumber; // employee clock number
  37. float wageRate; // hourly wage rate
  38. float hours; // hours worked in the week
  39. float overtimeHrs; // overtime hours worked
  40. float grossPay; // total gross pay
  41. };
  42.  
  43. //-------------------- Function Prototypes --------------
  44.  
  45. float getHours (long int clockNumber);
  46. float calcOvertime(float hours);
  47. float calcGross(float hours, float wageRate, float overtimeHrs);
  48. void printHeader (void);
  49. void printEmp (long int clockNumber, float wageRate, float hours,
  50. float overtimeHrs, float grossPay);
  51.  
  52.  
  53.  
  54. int main ()
  55. {
  56. // Array of structures to store employee payroll data
  57. struct employee employeeData[SIZE] = {
  58. { 98401, 10.60 },
  59. { 526488, 9.75 },
  60. { 765349, 10.50 }, // initialize clock numbers and wages
  61. { 34645, 12.25 },
  62. { 127615, 8.35 }
  63. };
  64.  
  65. int i; // loop counter used to process each employee
  66.  
  67. // Loop through each employee to gather hours and calculate payroll
  68. for (i = 0; i < SIZE; ++i)
  69. {
  70. // Prompt the user to enter hours worked for the employee
  71. employeeData[i].hours = getHours(employeeData[i].clockNumber);
  72.  
  73. // Calculate overtime hours based on total hours worked
  74. employeeData[i].overtimeHrs = calcOvertime(employeeData[i].hours);
  75.  
  76. // Calculate the employee's gross pay
  77. employeeData[i].grossPay = calcGross(
  78. employeeData[i].hours,
  79. employeeData[i].wageRate,
  80. employeeData[i].overtimeHrs
  81. );
  82. }
  83.  
  84. // Print report header before displaying employee data
  85. printHeader();
  86.  
  87. // Loop through the structure array and print each employee record
  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. return(0); // indicate successful program completion
  98.  
  99. } // end main
  100.  
  101.  
  102.  
  103. //**************************************************************
  104. //
  105. // Function: getHours
  106. //
  107. // Purpose: Prompts the user to enter the number of hours worked
  108. // for a specific employee and returns the value to the caller.
  109. //
  110. // Parameters:
  111. // clockNumber - unique employee clock number
  112. //
  113. // Returns:
  114. // float - hours worked by the employee
  115. //
  116. //**************************************************************
  117.  
  118. float getHours (long int clockNumber)
  119. {
  120. float hoursWorked; // local variable to store user input
  121.  
  122. // Ask the user to enter the hours worked for the employee
  123. printf("\nEnter hours worked by emp # %06li: ", clockNumber);
  124.  
  125. // Read the value entered by the user
  126. scanf ("%f", &hoursWorked);
  127.  
  128. // Return the hours worked to the calling function
  129. return (hoursWorked);
  130.  
  131. } // end getHours
  132.  
  133.  
  134.  
  135. //**************************************************************
  136. //
  137. // Function: calcOvertime
  138. //
  139. // Purpose: Determines the number of overtime hours worked
  140. // by an employee based on the standard work week.
  141. //
  142. // Parameters:
  143. // hours - total hours worked in the week
  144. //
  145. // Returns:
  146. // float - overtime hours worked
  147. //
  148. //**************************************************************
  149.  
  150. float calcOvertime(float hours)
  151. {
  152. float overtime; // local variable to store overtime hours
  153.  
  154. // Determine if employee worked more than standard hours
  155. if (hours > STD_HOURS)
  156. {
  157. // Overtime is the number of hours beyond 40
  158. overtime = hours - STD_HOURS;
  159. }
  160. else
  161. {
  162. // No overtime if hours are less than or equal to 40
  163. overtime = 0.0;
  164. }
  165.  
  166. // Return calculated overtime hours
  167. return overtime;
  168.  
  169. } // end calcOvertime
  170.  
  171.  
  172.  
  173. //**************************************************************
  174. //
  175. // Function: calcGross
  176. //
  177. // Purpose: Calculates the total gross pay for an employee
  178. // including regular pay and overtime pay.
  179. //
  180. // Parameters:
  181. // hours - total hours worked
  182. // wageRate - employee hourly wage
  183. // overtimeHrs - number of overtime hours worked
  184. //
  185. // Returns:
  186. // float - total gross pay
  187. //
  188. //**************************************************************
  189.  
  190. float calcGross(float hours, float wageRate, float overtimeHrs)
  191. {
  192. float regularPay; // pay earned from regular hours
  193. float overtimePay; // pay earned from overtime hours
  194. float gross; // total gross pay
  195.  
  196. // Calculate pay for regular hours (hours minus overtime)
  197. regularPay = (hours - overtimeHrs) * wageRate;
  198.  
  199. // Calculate overtime pay using overtime multiplier
  200. overtimePay = overtimeHrs * wageRate * OT_RATE;
  201.  
  202. // Total gross pay is the sum of regular and overtime pay
  203. gross = regularPay + overtimePay;
  204.  
  205. // Return total gross pay
  206. return gross;
  207.  
  208. } // end calcGross
  209.  
  210.  
  211.  
  212. //**************************************************************
  213. //
  214. // Function: printHeader
  215. //
  216. // Purpose: Prints the payroll report header and column labels.
  217. //
  218. // Parameters: none
  219. //
  220. // Returns: void
  221. //
  222. //**************************************************************
  223.  
  224. void printHeader (void)
  225. {
  226. // Print report title
  227. printf ("\n\n*** Pay Calculator ***\n");
  228.  
  229. // Print column headings for the payroll table
  230. printf("\nClock# Wage Hours OT Gross\n");
  231.  
  232. // Print a separator line for readability
  233. printf("------------------------------------------------\n");
  234.  
  235. } // end printHeader
  236.  
  237.  
  238.  
  239. //*************************************************************
  240. //
  241. // Function: printEmp
  242. //
  243. // Purpose: Displays payroll information for a single employee
  244. // in formatted column output.
  245. //
  246. // Parameters:
  247. // clockNumber - employee identification number
  248. // wageRate - hourly wage rate
  249. // hours - total hours worked
  250. // overtimeHrs - overtime hours worked
  251. // grossPay - calculated gross pay
  252. //
  253. // Returns: void
  254. //
  255. //**************************************************************
  256.  
  257. void printEmp (long int clockNumber, float wageRate, float hours,
  258. float overtimeHrs, float grossPay)
  259. {
  260. // Print employee payroll information in a formatted row
  261. printf("\n %06li %5.2f %4.1f %4.1f %8.2f",
  262. clockNumber, wageRate, hours,
  263. overtimeHrs, grossPay);
  264.  
  265. } // end printEmp
Success #stdin #stdout 0.01s 5328KB
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