fork download
  1. //********************************************************
  2. //
  3. // Assignment 5 - Functions
  4. //
  5. // Name: Randall Cranshaw
  6. //
  7. // Class: C Programming, Fall 2025
  8. //
  9. // Date: October 12 2025
  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. // All functions are called by value
  16. //
  17. //********************************************************
  18.  
  19. #include <stdio.h>
  20.  
  21. // constants
  22. #define SIZE 5
  23. #define OVERTIME_RATE 1.5f
  24. #define STD_WORK_WEEK 40.0f
  25.  
  26. // function prototypes
  27. float getHours (long int clockNumber);
  28. void printHeader (void);
  29. void printEmp (long int clockNumber, float wageRate, float hours,
  30. float overtimeHrs, float grossPay);
  31.  
  32. // TODO: Add other function prototypes here as needed
  33. // function prototypes for overtime and gross pay
  34. float calc_overtime_hours(float hours_worked);
  35. float calc_gross_pay(float wage_rate, float hours_worked, float overtime_hours);
  36.  
  37. int main()
  38. {
  39. /* Variable Declarations */
  40.  
  41. long int clockNumber[SIZE] = {98401,526488,765349,34645,127615}; // ID
  42. float grossPay[SIZE]; // gross pay
  43. float hours[SIZE]; // hours worked in a given week
  44. int i; // loop and array index
  45. float overtimeHrs[SIZE]; // overtime hours
  46. float wageRate[SIZE] = {10.60,9.75,10.50,12.25,8.35}; // hourly wage rate
  47.  
  48. // process each employee
  49. for (i = 0; i < SIZE; ++i)
  50. {
  51. // Read in hours for employee
  52. hours[i] = getHours(clockNumber[i]);
  53.  
  54. // TODO: Function call to calculate overtime hours
  55. // Array calc for overtime
  56. overtimeHrs[i] = calc_overtime_hours(hours[i]);
  57.  
  58. // TODO: Function call to calculate gross pay
  59. // Array calc for Gross pay
  60. grossPay[i] = calc_gross_pay(wageRate[i], hours[i], overtimeHrs[i]);
  61. }
  62.  
  63. // print the header info
  64. printHeader();
  65.  
  66. // print out each employee
  67. for (i = 0; i < SIZE; ++i)
  68. {
  69. // Print all the employees - call by value
  70. printEmp(clockNumber[i], wageRate[i], hours[i],
  71. overtimeHrs[i], grossPay[i]);
  72. } // for
  73.  
  74. return (0);
  75.  
  76. } // main
  77.  
  78. //**************************************************************
  79. // Function: getHours
  80. //
  81. // Purpose: Obtains input from user, the number of hours worked
  82. // per employee and stores the result in a local variable
  83. // that is passed back to the calling function.
  84. //
  85. // Parameters: clockNumber - The unique employee ID
  86. //
  87. // Returns: hoursWorked - hours worked in a given week
  88. //
  89. //**************************************************************
  90.  
  91. float getHours (long int clockNumber)
  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. } // getHours
  102.  
  103. //**************************************************************
  104. // Function: printHeader
  105. //
  106. // Purpose: Prints the initial table header information.
  107. //
  108. // Parameters: none
  109. //
  110. // Returns: void
  111. //
  112. //**************************************************************
  113.  
  114. void printHeader (void)
  115. {
  116. printf ("\n\n*** Pay Calculator ***\n");
  117.  
  118. // print the table header
  119. printf("\nClock# Wage Hours OT Gross\n");
  120. printf("------------------------------------------------\n");
  121. } // printHeader
  122.  
  123. //*************************************************************
  124. // Function: printEmp
  125. //
  126. // Purpose: Prints out all the information for an employee
  127. // in a nice and orderly table format.
  128. //
  129. // Parameters:
  130. //
  131. // clockNumber - unique employee ID
  132. // wageRate - hourly wage rate
  133. // hours - Hours worked for the week
  134. // overtimeHrs - overtime hours worked in a week
  135. // grossPay - gross pay for the week
  136. //
  137. // Returns: void
  138. //
  139. //**************************************************************
  140.  
  141. void printEmp (long int clockNumber, float wageRate, float hours,
  142. float overtimeHrs, float grossPay)
  143. {
  144. // print the employee
  145. // TODO: add code to print out a single employee
  146. // Spacing of 10 for columns, not counting the clock
  147. printf("%06li %10.2f %10.1f %10.1f %10.2f\n",
  148. clockNumber, wageRate, hours, overtimeHrs, grossPay);
  149. }
  150.  
  151. //1st prototype function overtime hours
  152. //**************************************************************
  153. // Function: calc_overtime_hours
  154. //
  155. // Purpose: Calculates overtime if greater than STD_WORK_WEEK
  156. //
  157. // Parameters:
  158. // hours_worked - Total hours worked for the week
  159. //
  160. // Returns:
  161. // overtime hours (0 if hours_worked is less than STD_WORK_WEEK)
  162. //**************************************************************
  163. float calc_overtime_hours(float hours_worked)
  164. {
  165. if (hours_worked > STD_WORK_WEEK)
  166. return hours_worked - STD_WORK_WEEK;
  167. else
  168. return 0.0f;
  169. }
  170.  
  171. //2nd prototype function gross pay
  172. //**************************************************************
  173. // Function: calc_gross_pay
  174. //
  175. // Purpose: Calculates total gross pay including overtime pay
  176. //
  177. // Parameters:
  178. // wage_rate - Hourly pay rate
  179. // hours_worked - Total hours worked in a week
  180. // overtime_hours - Total overtime hours worked
  181. //
  182. // Returns:
  183. // gross pay
  184. //**************************************************************
  185. float calc_gross_pay(float wage_rate, float hours_worked, float overtime_hours)
  186. {
  187. float regular_hours = hours_worked;
  188. if (regular_hours > STD_WORK_WEEK)
  189. regular_hours = STD_WORK_WEEK;
  190.  
  191. float total_gross_pay = (wage_rate * regular_hours) +
  192. (wage_rate * OVERTIME_RATE * overtime_hours);
  193.  
  194. return total_gross_pay;
  195. }
  196.  
Success #stdin #stdout 0.01s 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