fork download
  1. //********************************************************
  2. //
  3. // Assignment 5 - Functions
  4. //
  5. // Name: Will Vanier
  6. //
  7. // Class: C Programming, <replace with Semester and Year>
  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. float calcOvertimeHrs(float hours);
  33. float calcGrossPay(float hours, float wageRate, float overtimeHrs);
  34.  
  35. int main()
  36. {
  37.  
  38. /* Variable Declarations */
  39.  
  40. long int clockNumber[SIZE] = {98401,526488,765349,34645,127615}; // ID
  41. float grossPay[SIZE]; // gross pay
  42. float hours[SIZE]; // hours worked in a given week
  43. int i; // loop and array index
  44. float overtimeHrs[SIZE]; // overtime hours
  45. float wageRate[SIZE] = {10.60,9.75,10.50,12.25,8.35}; // hourly wage rate
  46.  
  47. // process each employee
  48. for (i = 0; i < SIZE; ++i)
  49. {
  50.  
  51. // Read in hours for employee
  52. hours[i] = getHours (clockNumber[i]);
  53.  
  54. // TODO: Function call to calculate overtime hours
  55. overtimeHrs[i] = calcOvertimeHrs(hours[i]);
  56.  
  57. // TODO: Function call to calculate gross pay
  58. grossPay[i] = calcGrossPay(hours[i], wageRate[i], overtimeHrs[i]);
  59. }
  60.  
  61. // print the header info
  62. printHeader();
  63.  
  64. // print out each employee
  65. for (i = 0; i < SIZE; ++i)
  66. {
  67.  
  68. // Print all the employees - call by value
  69. printEmp (clockNumber[i], wageRate[i], hours[i],
  70. overtimeHrs[i], grossPay[i]);
  71.  
  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.  
  94. float hoursWorked; // hours worked in a given week
  95.  
  96. // Read in hours for employee
  97. printf("\nEnter hours worked by emp # %06li: ", clockNumber);
  98. scanf ("%f", &hoursWorked);
  99.  
  100. // return hours back to the calling function
  101. return (hoursWorked);
  102.  
  103. } // getHours
  104.  
  105.  
  106.  
  107. //**************************************************************
  108. // Function: calcOvertimeHrs
  109. //
  110. // Purpose: Calculates overtime hours worked in a week
  111. //
  112. // Parameters: hours - (float) total hours worked for the week
  113. //
  114. // Returns: (float) the number of OT hours (0 if none)
  115. //**************************************************************
  116. float calcOvertimeHrs(float hours)
  117. {
  118. if (hours > STD_WORK_WEEK)
  119. return hours - STD_WORK_WEEK;
  120. else
  121. return 0.0f;
  122. }
  123. //**************************************************************
  124. // Function: calcGrossPay
  125. //
  126. // Purpose: Calculates total pay
  127. //
  128. // Parameters:
  129. // hours-(float) total hours worked in a week.
  130. // wageRate- (float) employee’s hourly wage.
  131. // overtimeHrs- (float) hours worked beyond 40.
  132. //
  133. // Returns: (float) The calculated gross pay for the week
  134. //**************************************************************
  135. float calcGrossPay(float hours, float wageRate, float overtimeHrs)
  136. {
  137. float grossPay;
  138.  
  139. if (hours > STD_WORK_WEEK)
  140. {
  141. grossPay = (STD_WORK_WEEK * wageRate) +
  142. (overtimeHrs * wageRate * OVERTIME_RATE);
  143. }
  144. else
  145. {
  146. grossPay = hours * wageRate;
  147. }
  148. return grossPay;
  149. }
  150. //**************************************************************
  151. // Function: printHeader
  152. //
  153. // Purpose: Prints the initial table header information.
  154. //
  155. // Parameters: none
  156. //
  157. // Returns: void
  158. //
  159. //**************************************************************
  160.  
  161. void printHeader (void)
  162. {
  163.  
  164. printf ("\n\n*** Pay Calculator ***\n");
  165.  
  166. // print the table header
  167. printf("\nClock# Wage Hours OT Gross\n");
  168. printf("------------------------------------------------\n");
  169.  
  170. } // printHeader
  171.  
  172. //*************************************************************
  173. // Function: printEmp
  174. //
  175. // Purpose: Prints out all the information for an employee
  176. // in a nice and orderly table format.
  177. //
  178. // Parameters:
  179. //
  180. // clockNumber - unique employee ID
  181. // wageRate - hourly wage rate
  182. // hours - Hours worked for the week
  183. // overtimeHrs - overtime hours worked in a week
  184. // grossPay - gross pay for the week
  185. //
  186. // Returns: void
  187. //
  188. //**************************************************************
  189.  
  190. void printEmp (long int clockNumber, float wageRate, float hours,
  191. float overtimeHrs, float grossPay)
  192. {
  193. printf("%06li %5.2f %5.1f %8.2f\n",
  194. clockNumber, wageRate, hours, overtimeHrs, grossPay);
  195.  
  196.  
  197. }
  198.  
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.00
526488  9.75  42.5     2.50
765349 10.50  37.0     0.00
034645 12.25  45.0     5.00
127615  8.35   0.0     0.00