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:
  167. //
  168. // Parameters:
  169. //
  170. //
  171. //
  172. // Returns: overtimeHrs
  173. //
  174. //**************************************************************
  175. //
  176. float calcOT (float hoursTotal) // Function Definition
  177. {
  178. float overtimeHrs;
  179.  
  180. if (hoursTotal > STD_WORK_WEEK)
  181. {
  182. overtimeHrs = hoursTotal - STD_WORK_WEEK;
  183.  
  184. } // End if
  185.  
  186. return (overtimeHrs);
  187. } // calcOT
  188.  
  189. //*************************************************************
  190. // Function: calcGross
  191. //
  192. // Purpose:
  193. //
  194. // Parameters:
  195. //
  196. //
  197. //
  198. // Returns: grossPay
  199. //
  200. //**************************************************************
  201. //
  202. float calcGross (float wageRate, float hoursTotal, float overtimeHrs) // Function Definition
  203. {
  204. float grossPay;
  205. float normalPay;
  206. float overtimePay;
  207.  
  208. overtimePay = overtimeHrs * (OVERTIME_RATE*wageRate);
  209. normalPay= hoursTotal * wageRate;
  210. grossPay = normalPay + overtimePay;
  211.  
  212. return (grossPay);
  213. } // calcGross
  214. //
  215. //**************************************************************
Success #stdin #stdout 0.01s 5296KB
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   715.50
526488  9.75  42.5   2.5   450.94
765349 10.50  37.0   2.5   427.88
034645 12.25  45.0   5.0   643.12
127615  8.35   0.0   5.0    62.63