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