fork download
  1. //********************************************************
  2. //
  3. // Assignment 5 - Functions
  4. //
  5. // Name: John Eneh
  6. //
  7. // Class: C Programming, Fall 2025
  8. //
  9. // Date: 10/11/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_HOURS 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. // Prototypes for the formulas I am adding
  33. float calcOT (float hours);
  34. float calcGross (float wageRate, float hours, float overtimeHrs);
  35.  
  36. int main()
  37. {
  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.  
  52. // Read in hours for employee
  53. hours[i] = getHours (clockNumber[i]);
  54.  
  55. // calcOT for overtime
  56. overtimeHrs[i] = calcOT (hours[i]);
  57.  
  58. // calcGross for gross pay
  59. grossPay[i] = calcGross (wageRate[i], hours[i], overtimeHrs[i]);
  60.  
  61. }
  62.  
  63. // print the header info
  64. printHeader();
  65.  
  66. // print out each employee
  67. for (i = 0; i < SIZE; ++i)
  68. {
  69.  
  70. // Print all the employees - call by value
  71. printEmp (clockNumber[i], wageRate[i], hours[i],
  72. overtimeHrs[i], grossPay[i]);
  73.  
  74. } // for
  75.  
  76. return (0);
  77.  
  78. } // main
  79. //**************************************************************
  80. // Function: getHours
  81. //
  82. // Purpose: Obtains input from user, the number of hours worked
  83. // per employee and stores the result in a local variable
  84. // that is passed back to the calling function.
  85. //
  86. // Parameters: clockNumber - The unique employee ID
  87. //
  88. // Returns: hoursWorked - hours worked in a given week
  89. //
  90. //**************************************************************
  91.  
  92. float getHours (long int clockNumber)
  93. {
  94.  
  95. float hoursWorked; // hours worked in a given week
  96.  
  97. // Read in hours for employee
  98. printf("\nEnter hours worked by emp # %06li: ", clockNumber);
  99. scanf ("%f", &hoursWorked);
  100.  
  101. // return hours back to the calling function
  102. return (hoursWorked);
  103.  
  104. } // getHours
  105. //**************************************************************
  106. // Function: calcOT
  107. //
  108. // Purpose: takes the value of hours and determines if they
  109. // exceed the 40 hours of the stantdard work week
  110. // and then passes the overtime back to the calling function.
  111. //
  112. // Parameters:
  113. // hours - Hours worked for the week
  114. // STD_HOURS - 40 hours in the work week
  115. //
  116. // Returns:
  117. // overtime hours - the calculated hours beyond 40. Returns
  118. // 0 if hours do not exceed 40.
  119. //
  120. //**************************************************************
  121.  
  122. float calcOT (float hours)
  123. {
  124. if (hours > STD_HOURS)
  125. {
  126. return (hours - STD_HOURS);
  127. }
  128. else
  129. {
  130. return (0);
  131. }
  132.  
  133. } // calcOT
  134. //**************************************************************
  135. // Function: calcGross
  136. //
  137. // Purpose: calculates the gross pay using the employees'
  138. // wageRate, hours worked, and OT hours if any are available.
  139. // Passes the calculated the amount to main.
  140. //
  141. // Parameters:
  142. // wageRate - Employees' payrate
  143. // hours - Hours worked for the week
  144. // STD_HOURS - 40 hours in the work week
  145. // overtimeHrs - Hours worked beyond 40 hours
  146. //
  147. // Returns: gross pay - normal pay + available OT pay
  148. //
  149. //**************************************************************
  150.  
  151. float calcGross (float wageRate, float hours, float overtimeHrs)
  152. {
  153. if (hours > STD_HOURS)
  154. {
  155. return ((STD_HOURS * wageRate) + (overtimeHrs * wageRate * OVERTIME_RATE));
  156. }
  157. else
  158. {
  159. return (wageRate * hours);
  160. }
  161. } // calcGross
  162. //**************************************************************
  163. // Function: printHeader
  164. //
  165. // Purpose: Prints the initial table header information.
  166. //
  167. // Parameters: none
  168. //
  169. // Returns: void
  170. //
  171. //**************************************************************
  172.  
  173. void printHeader (void)
  174. {
  175.  
  176. printf ("\n\n*** Pay Calculator ***\n");
  177.  
  178. // print the table header
  179. printf("\nClock# Wage Hours OT Gross\n");
  180. printf("------------------------------------------------\n");
  181.  
  182. } // printHeader
  183.  
  184. //*************************************************************
  185. // Function: printEmp
  186. //
  187. // Purpose: Prints out all the information for an employee
  188. // in a nice and orderly table format.
  189. //
  190. // Parameters:
  191. //
  192. // clockNumber - unique employee ID
  193. // wageRate - hourly wage rate
  194. // hours - Hours worked for the week
  195. // overtimeHrs - overtime hours worked in a week
  196. // grossPay - gross pay for the week
  197. //
  198. // Returns: void
  199. //
  200. //**************************************************************
  201.  
  202. void printEmp (long int clockNumber, float wageRate, float hours,
  203. float overtimeHrs, float grossPay)
  204. {
  205.  
  206. // print the employee
  207.  
  208. // Reusing pintout from previous assignments
  209. printf("%06d %5.2f %5.1f %5.1f %8.2f\n",
  210. clockNumber, wageRate, hours, overtimeHrs, grossPay);
  211. }
Success #stdin #stdout 0.01s 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