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