fork(1) download
  1. //********************************************************
  2. //
  3. // Assignment 5 - Functions
  4. //
  5. // Name: Brian Fallon
  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 hoursTotal,
  30. float overtimeHrs, float grossPay);
  31.  
  32. // TODO: Add other function prototypes here as needed
  33. float calcOT (float hoursTotal); // Function Prototype
  34. float calcGross (float wageRate, float hoursTotal, float overtimeHrs); // Function Prototype
  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 hoursTotal[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. float overtimePay [SIZE]; // overtime pay for a given week
  48. float normalPay [SIZE]; // normal weekly pay without any overtime
  49.  
  50. // process each employee
  51. for (i = 0; i < SIZE; ++i)
  52. {
  53.  
  54. // Read in hours for employee
  55. hoursTotal[i] = getHours (clockNumber[i]);
  56.  
  57. // TODO: Function call to calculate overtime hours
  58. overtimeHrs[i] = calcOT(hoursTotal[i]);
  59.  
  60. // TODO: Function call to calculate gross pay
  61. grossPay[i] = calcGross(wageRate[i], hoursTotal[i], overtimeHrs[i]);
  62.  
  63. } // End for
  64.  
  65. // print the header info
  66. printHeader();
  67.  
  68. // print out each employee
  69. for (i = 0; i < SIZE; ++i)
  70. {
  71.  
  72. // Print all the employees - call by value
  73. printEmp (clockNumber[i], wageRate[i], hoursTotal[i],
  74. overtimeHrs[i], grossPay[i]);
  75.  
  76. } // End for
  77.  
  78. return (0);
  79.  
  80. } // main
  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) //Called Function
  96. {
  97.  
  98. float hoursWorked; // hours worked in a given week
  99.  
  100. // Read in hours for employee
  101. printf("\nEnter hours worked by emp # %06li: ", clockNumber);
  102. scanf ("%f", &hoursWorked);
  103.  
  104. // return hours back to the calling function
  105. return (hoursWorked);
  106.  
  107. } // getHours
  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.  
  123. printf ("\n\n*** Pay Calculator ***\n");
  124.  
  125. // print the table header
  126. printf("\nClock# Wage Hours OT Gross\n");
  127. printf("------------------------------------------------\n");
  128.  
  129. } // printHeader
  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 hoursTotal,
  150. float overtimeHrs, float grossPay)
  151. {
  152.  
  153. // print the employee
  154. // TODO: add code to print out a single employee
  155. printf("%06d %5.2f %5.1f %5.1f %8.2f\n",
  156. clockNumber, wageRate, hoursTotal, overtimeHrs, grossPay);
  157. } //printEmp
  158.  
  159.  
  160. // TODO: Add other functions here as needed
  161. // ... remember your comment block headers for each function
  162.  
  163. //*************************************************************
  164. // Function: calcOT
  165. //
  166. // Purpose: Calculate the overtime hours of employees if logged
  167. // more than the standard week
  168. //
  169. // Parameters:
  170. // hoursTotal- total logged hours per employee
  171. // STD_WORK_WEEK - the standard amount of 40 hours per week
  172. //
  173. // Returns: overtimeHrs
  174. //
  175. //**************************************************************
  176. //
  177. float calcOT (float hoursTotal) // Function Definition
  178. {
  179. float overtimeHrs;
  180.  
  181. overtimeHrs = hoursTotal - STD_WORK_WEEK;
  182.  
  183. return (overtimeHrs);
  184. } // calcOT
  185.  
  186. //*************************************************************
  187. // Function: calcGross
  188. //
  189. // Purpose: Calculate the Gross Pay of employees considering
  190. // any overtime hours logged
  191. //
  192. // Parameters:
  193. // wageRate - hourly wage of employees
  194. // hoursTotal- the total hours logged for employees
  195. // overtimeHrs - any overtime hours for any applicable employee logs
  196. //
  197. // Returns: grossPay
  198. //
  199. //**************************************************************
  200. //
  201. float calcGross (float wageRate, float hoursTotal, float overtimeHrs) // Function Definition
  202. {
  203. float grossPay;
  204. float normalPay;
  205. float overtimePay;
  206.  
  207. if (hoursTotal > STD_WORK_WEEK)
  208. {
  209. overtimeHrs = hoursTotal - STD_WORK_WEEK;
  210. overtimePay = overtimeHrs * (OVERTIME_RATE*wageRate);
  211. normalPay = STD_WORK_WEEK*wageRate;
  212. grossPay = normalPay + overtimePay;
  213. } // End if
  214.  
  215. else // No Overtime
  216. {
  217. overtimeHrs=0;
  218. normalPay=wageRate*hoursTotal;
  219. grossPay=normalPay;
  220. } // End else
  221.  
  222. return (grossPay);
  223. } // calcGross
  224. //
  225. //**************************************************************
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.0  11.0   598.90
526488  9.75  42.5   2.5   426.56
765349 10.50  37.0  -3.0   388.50
034645 12.25  45.0   5.0   581.88
127615  8.35   0.0 -40.0     0.00