fork download
  1. //********************************************************
  2. //
  3. // Assignment 5 - Functions
  4. //
  5. // Name: <Kyle Merrihew>
  6. //
  7. // Class: C Programming, <Spring 2025>
  8. //
  9. // Date: <March 2,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. float calculateOvertime (float hours);
  32. float calculateGrossPay (float wageRate, float hours, float overtimeHrs);
  33.  
  34. //**************************************************************
  35. // Function: getHours
  36. //
  37. // Purpose: Obtains input from user, the number of hours worked
  38. // per employee and stores the result in a local variable
  39. // that is passed back to the calling function.
  40. //
  41. // Parameters: clockNumber - The unique employee ID
  42. //
  43. // Returns: hoursWorked - hours worked in a given week
  44. //
  45. //**************************************************************
  46.  
  47. float getHours (long int clockNumber)
  48. {
  49. float hoursWorked; // hours worked in a given week
  50.  
  51. // Read in hours for employee
  52. printf("\nEnter hours worked by emp # %06li: ", clockNumber);
  53. scanf ("%f", &hoursWorked);
  54.  
  55. // return hours back to the calling function
  56. return (hoursWorked);
  57.  
  58. } // getHours
  59.  
  60. //**************************************************************
  61. // Function: printHeader
  62. //
  63. // Purpose: Prints the initial table header information.
  64. //
  65. // Parameters: none
  66. //
  67. // Returns: void
  68. //
  69. //**************************************************************
  70.  
  71. void printHeader (void)
  72. {
  73. printf ("\n\n*** Pay Calculator ***\n");
  74.  
  75. // print the table header
  76. printf("\nClock# Wage Hours OT Gross\n");
  77. printf("------------------------------------------------\n");
  78.  
  79. } // printHeader
  80.  
  81. //*************************************************************
  82. // Function: printEmp
  83. //
  84. // Purpose: Prints out all the information for an employee
  85. // in a nice and orderly table format.
  86. //
  87. // Parameters:
  88. //
  89. // clockNumber - unique employee ID
  90. // wageRate - hourly wage rate
  91. // hours - Hours worked for the week
  92. // overtimeHrs - overtime hours worked in a week
  93. // grossPay - gross pay for the week
  94. //
  95. // Returns: void
  96. //
  97. //**************************************************************
  98.  
  99. void printEmp (long int clockNumber, float wageRate, float hours,
  100. float overtimeHrs, float grossPay)
  101. {
  102. // print the employee's details
  103. printf("%06li %.2f %.2f %.2f %.2f\n", clockNumber, wageRate, hours, overtimeHrs, grossPay);
  104. }
  105.  
  106. //*************************************************************
  107. // Function: calculateOvertime
  108. //
  109. // Purpose: Calculates the overtime hours worked based on total
  110. // hours worked in a week, assuming 40 hours is the regular work week.
  111. //
  112. // Parameters:
  113. // hours - total hours worked in the week
  114. //
  115. // Returns: overtimeHrs - the overtime hours worked
  116. //
  117. //**************************************************************
  118.  
  119. float calculateOvertime (float hours)
  120. {
  121. if (hours > STD_WORK_WEEK) {
  122. return hours - STD_WORK_WEEK; // calculate overtime hours
  123. }
  124. return 0.0f; // no overtime if hours are less than or equal to 40
  125. }
  126.  
  127. //*************************************************************
  128. // Function: calculateGrossPay
  129. //
  130. // Purpose: Calculates the gross pay based on regular and overtime
  131. // hours worked, multiplying by the wage rate and applying overtime
  132. // rate for hours worked beyond the standard 40-hour week.
  133. //
  134. // Parameters:
  135. // wageRate - hourly wage rate
  136. // hours - total hours worked
  137. // overtimeHrs - overtime hours worked
  138. //
  139. // Returns: grossPay - the total gross pay for the week
  140. //
  141. //**************************************************************
  142.  
  143. float calculateGrossPay (float wageRate, float hours, float overtimeHrs)
  144. {
  145. // regular pay for standard hours worked
  146. float regularPay = wageRate * (hours - overtimeHrs);
  147.  
  148. // overtime pay for extra hours worked
  149. float overtimePay = overtimeHrs * wageRate * OVERTIME_RATE;
  150.  
  151. // total gross pay is the sum of regular pay and overtime pay
  152. return regularPay + overtimePay;
  153. }
  154.  
  155. int main()
  156. {
  157. /* Variable Declarations */
  158.  
  159. long int clockNumber[SIZE] = {98401,526488,765349,34645,127615}; // ID
  160. float grossPay[SIZE]; // gross pay
  161. float hours[SIZE]; // hours worked in a given week
  162. int i; // loop and array index
  163. float overtimeHrs[SIZE]; // overtime hours
  164. float wageRate[SIZE] = {10.60,9.75,10.50,12.25,8.35}; // hourly wage rate
  165.  
  166. // process each employee
  167. for (i = 0; i < SIZE; ++i)
  168. {
  169. // Read in hours for employee
  170. hours[i] = getHours (clockNumber[i]);
  171.  
  172. // Function call to calculate overtime hours
  173. overtimeHrs[i] = calculateOvertime(hours[i]);
  174.  
  175. // Function call to calculate gross pay
  176. grossPay[i] = calculateGrossPay(wageRate[i], hours[i], overtimeHrs[i]);
  177. }
  178.  
  179. // print the header info
  180. printHeader();
  181.  
  182. // print out each employee
  183. for (i = 0; i < SIZE; ++i)
  184. {
  185. // Print all the employees - call by value
  186. printEmp (clockNumber[i], wageRate[i], hours[i],
  187. overtimeHrs[i], grossPay[i]);
  188.  
  189. } // for
  190.  
  191. return (0);
  192.  
  193. } // main
Success #stdin #stdout 0.01s 5284KB
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.00 11.00 598.90
526488 9.75 42.50 2.50 426.56
765349 10.50 37.00 0.00 388.50
034645 12.25 45.00 5.00 581.88
127615 8.35 0.00 0.00 0.00