fork download
  1. //********************************************************
  2. //
  3. // Assignment 5 - Functions
  4. //
  5. // Name: Matthew Chen
  6. //
  7. // Class: C Programming, Fall 2026
  8. //
  9. // Date: 10/5/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. // 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. float calcOvertime(float hours);
  33. float calcGrossPay(float wageRate, float hours, float overtimeHrs);
  34.  
  35. int main()
  36. {
  37. /* Variable Declarations */
  38.  
  39. long int clockNumber[SIZE] = {
  40. 98401, 526488, 765349, 34645, 127615
  41. };
  42.  
  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.  
  48. float wageRate[SIZE] = {
  49. 10.60, 9.75, 10.50, 12.25, 8.35
  50. }; // hourly wage rate
  51.  
  52. // process each employee
  53. for (i = 0; i < SIZE; ++i)
  54. {
  55. // Read in hours for employee
  56. hours[i] = getHours(clockNumber[i]);
  57.  
  58. // Calculate overtime hours
  59. overtimeHrs[i] = calcOvertime(hours[i]);
  60.  
  61. // Calculate gross pay
  62. grossPay[i] = calcGrossPay(wageRate[i], hours[i],
  63. overtimeHrs[i]);
  64. }
  65.  
  66. // print the header info
  67. printHeader();
  68.  
  69. // print out each employee
  70. for (i = 0; i < SIZE; ++i)
  71. {
  72. // Print all the employees - call by value
  73. printEmp(clockNumber[i], wageRate[i], hours[i],
  74. overtimeHrs[i], grossPay[i]);
  75. }
  76.  
  77. return (0);
  78.  
  79. } // main
  80.  
  81.  
  82. //**************************************************************
  83. // Function: getHours
  84. //
  85. // Purpose: Obtains input from user, the number of hours worked
  86. // per employee and stores the result in a local variable
  87. // that is passed back to the calling function.
  88. //
  89. // Parameters: clockNumber - The unique employee ID
  90. //
  91. // Returns: hoursWorked - hours worked in a given week
  92. //
  93. //**************************************************************
  94.  
  95. float getHours(long int clockNumber)
  96. {
  97. float hoursWorked; // hours worked in a given week
  98.  
  99. // Read in hours for employee
  100. printf("\nEnter hours worked by emp # %06li: ", clockNumber);
  101. scanf("%f", &hoursWorked);
  102.  
  103. // return hours back to the calling function
  104. return (hoursWorked);
  105.  
  106. } // getHours
  107.  
  108.  
  109. //**************************************************************
  110. // Function: printHeader
  111. //
  112. // Purpose: Prints the initial table header information.
  113. //
  114. // Parameters: none
  115. //
  116. // Returns: void
  117. //
  118. //**************************************************************
  119.  
  120. void printHeader(void)
  121. {
  122. printf("\n\n*** Pay Calculator ***\n");
  123.  
  124. // print the table header
  125. printf("\nClock# Wage Hours OT Gross\n");
  126. printf("------------------------------------------------\n");
  127.  
  128. } // printHeader
  129.  
  130.  
  131. //**************************************************************
  132. // Function: printEmp
  133. //
  134. // Purpose: Prints out all the information for an employee
  135. // in a nice and orderly table format.
  136. //
  137. // Parameters:
  138. //
  139. // clockNumber - unique employee ID
  140. // wageRate - hourly wage rate
  141. // hours - Hours worked for the week
  142. // overtimeHrs - overtime hours worked in a week
  143. // grossPay - gross pay for the week
  144. //
  145. // Returns: void
  146. //
  147. //**************************************************************
  148.  
  149. void printEmp(long int clockNumber, float wageRate, float hours,
  150. float overtimeHrs, float grossPay)
  151. {
  152. // print the employee
  153. printf("%06li %5.2f %5.1f %5.1f %8.2f\n",
  154. clockNumber, wageRate, hours,
  155. overtimeHrs, grossPay);
  156.  
  157. } // printEmp
  158.  
  159.  
  160. //**************************************************************
  161. // Function: calcOvertime
  162. //
  163. // Purpose: Calculates the number of overtime hours worked.
  164. //
  165. // Parameters:
  166. // hours - total hours worked during the week
  167. //
  168. // Returns: overtime hours
  169. //
  170. //**************************************************************
  171.  
  172. float calcOvertime(float hours)
  173. {
  174. if (hours > STD_WORK_WEEK)
  175. {
  176. return hours - STD_WORK_WEEK;
  177. }
  178. else
  179. {
  180. return 0.0f;
  181. }
  182.  
  183. } // calcOvertime
  184.  
  185.  
  186. //**************************************************************
  187. // Function: calcGrossPay
  188. //
  189. // Purpose: Calculates the employee's gross pay, including
  190. // overtime pay.
  191. //
  192. // Parameters:
  193. // wageRate - hourly wage rate
  194. // hours - total hours worked
  195. // overtimeHrs - overtime hours worked
  196. //
  197. // Returns: gross pay
  198. //
  199. //**************************************************************
  200.  
  201. float calcGrossPay(float wageRate, float hours, float overtimeHrs)
  202. {
  203. float regularHours;
  204.  
  205. if (hours > STD_WORK_WEEK)
  206. {
  207. regularHours = STD_WORK_WEEK;
  208. }
  209. else
  210. {
  211. regularHours = hours;
  212. }
  213.  
  214. return (regularHours * wageRate) +
  215. (overtimeHrs * wageRate * OVERTIME_RATE);
  216.  
  217. } // calcGrossPay
Success #stdin #stdout 0.01s 5320KB
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