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