fork(1) download
  1. //********************************************************
  2. //
  3. // Assignment 6 - Structures
  4. //
  5. // Name: Heather Grothe
  6. //
  7. // Class: C Programming, Spring 2026
  8. //
  9. // Date: March 7, 2026
  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. // Call by Value design
  16. //
  17. //********************************************************
  18.  
  19. // Define and Includes
  20.  
  21. #include <stdio.h>
  22.  
  23. // Define Constants
  24. #define SIZE 5
  25. #define STD_HOURS 40.0
  26. #define OT_RATE 1.5
  27.  
  28. // Define a global structure to pass employee data between functions
  29. // Note that the structure type is global, but you don't want a variable
  30. // of that type to be global. Best to declare a variable of that type
  31. // in a function like main or another function and pass as needed.
  32.  
  33. struct employee
  34. {
  35. long int clockNumber;
  36. float wageRate;
  37. float hours;
  38. float overtimeHrs;
  39. float grossPay;
  40. };
  41.  
  42. // define prototypes here for each function except main
  43. float getHours (long int clockNumber);
  44. void printHeader (void);
  45. void printEmp (long int clockNumber, float wageRate, float hours,
  46. float overtimeHrs, float grossPay);
  47. float calculateOvertimeHours(float hours);
  48. float calculateGrossPay(float wageRate, float hours, float overtimeHrs);
  49.  
  50.  
  51. int main ()
  52. {
  53. // Set up a local variable to store the employee information
  54. struct employee employeeData[SIZE] = {
  55. { 98401, 10.60 },
  56. { 526488, 9.75 },
  57. { 765349, 10.50 }, // initialize clock and wage values
  58. { 34645, 12.25 },
  59. { 127615, 8.35 }
  60. };
  61.  
  62. int i; // loop and array index
  63.  
  64. // Call functions as needed to read and calculate information
  65. for (i = 0; i < SIZE; ++i)
  66. {
  67.  
  68. // Prompt for the number of hours worked by the employee
  69. employeeData[i].hours = getHours (employeeData[i].clockNumber);
  70.  
  71. //calculate overtime hours
  72. employeeData[i].overtimeHrs = calculateOvertimeHours(employeeData[i].hours);
  73.  
  74. //calculate gross pay
  75. employeeData[i].grossPay = calculateGrossPay(employeeData[i].wageRate, employeeData[i].hours, employeeData[i].overtimeHrs);
  76. } // for
  77.  
  78. // Print the column headers
  79. printHeader();
  80.  
  81. // print out each employee
  82. for (i = 0; i < SIZE; ++i)
  83. {
  84. printEmp (employeeData[i].clockNumber,
  85. employeeData[i].wageRate,
  86. employeeData[i].hours,
  87. employeeData[i].overtimeHrs,
  88. employeeData[i].grossPay);
  89. }
  90.  
  91. return(0); // success
  92.  
  93. } // main
  94.  
  95. //**************************************************************
  96. // Function: getHours
  97. //
  98. // Purpose: Obtains input from user, the number of hours worked
  99. // per employee and stores the result in a local variable
  100. // that is passed back to the calling function.
  101. //
  102. // Parameters: clockNumber - The unique employee ID
  103. //
  104. // Returns: hoursWorked - hours worked in a given week
  105. //
  106. //**************************************************************
  107.  
  108. float getHours (long int clockNumber)
  109. {
  110.  
  111. float hoursWorked; // hours worked in a given week
  112.  
  113. // Read in hours for employee
  114. printf("\nEnter hours worked by emp # %06li: ", clockNumber);
  115. scanf ("%f", &hoursWorked);
  116.  
  117. // return hours back to the calling function
  118. return (hoursWorked);
  119.  
  120. } // getHours
  121.  
  122. //**************************************************************
  123. // Function: printHeader
  124. //
  125. // Purpose: Prints the initial table header information.
  126. //
  127. // Parameters: none
  128. //
  129. // Returns: void
  130. //
  131. //**************************************************************
  132.  
  133. void printHeader (void)
  134. {
  135.  
  136. printf ("\n\n*** Pay Calculator ***\n");
  137.  
  138. // print the table header
  139. printf("\nClock# Wage Hours OT Gross\n");
  140. printf("------------------------------------------------\n");
  141.  
  142. } // printHeader
  143.  
  144.  
  145. //*************************************************************
  146. // Function: printEmp
  147. //
  148. // Purpose: Prints out all the information for an employee
  149. // in a nice and orderly table format.
  150. //
  151. // Parameters:
  152. //
  153. // clockNumber - unique employee ID
  154. // wageRate - hourly wage rate
  155. // hours - Hours worked for the week
  156. // overtimeHrs - overtime hours worked in a week
  157. // grossPay - gross pay for the week
  158. //
  159. // Returns: void
  160. //
  161. //**************************************************************
  162.  
  163. void printEmp (long int clockNumber, float wageRate, float hours,
  164. float overtimeHrs, float grossPay)
  165. {
  166.  
  167. // Print out a single employee
  168. printf("\n %06li %6.2f %5.1f %5.1f %8.2f",
  169. clockNumber, wageRate, hours,
  170. overtimeHrs, grossPay);
  171.  
  172. } // printEmp
  173.  
  174. //**************************************************************
  175. // Function: calculateOvertimeHours
  176. //
  177. // Purpose: Calculate the number of hours of overtime worked.
  178. //
  179. // Parameters: hours - hours worked by the employee.
  180. //
  181. // Returns: overtimeHrs - number of hours worked above the STD_WORK_WEEK constant.
  182. //
  183. //**************************************************************
  184.  
  185.  
  186. float calculateOvertimeHours(float hours){
  187. float overtimeHrs;
  188. if (hours > STD_HOURS) {
  189. overtimeHrs = hours - STD_HOURS;
  190. } else {
  191. overtimeHrs = 0;
  192. }
  193. return overtimeHrs;
  194.  
  195. }
  196.  
  197. //**************************************************************
  198. // Function: calculateGrossPay
  199. //
  200. // Purpose: Calculate the gross pay for the employee.
  201. //
  202. // Parameters: wageRate - employee wages per hour
  203. // hours - hours worked by the employee.
  204. // overtimeHrs - overtime hours worked by the employee
  205. //
  206. // Returns: grossPay - total pay earned by employee for all hours worked including overtime.
  207. //
  208. //**************************************************************
  209.  
  210. float calculateGrossPay(float wageRate, float hours, float overtimeHrs){
  211. float grossPay;
  212. float overtimePay;
  213. float normalPay;
  214.  
  215. overtimePay = overtimeHrs * wageRate * OT_RATE;
  216. normalPay = (hours - overtimeHrs) * wageRate;
  217. grossPay = normalPay + overtimePay;
  218. return grossPay;
  219.  
  220.  
  221. }
Success #stdin #stdout 0.01s 5272KB
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