fork 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);
  34. float calcGross (float wageRate, float hoursTotal, float overtimeHrs);
  35.  
  36. int main()
  37. {
  38.  
  39. /* Variable Declarations */
  40. long int clockNumber[SIZE] = {98401,526488,765349,34645,127615}; // ID
  41. float grossPay[SIZE]; // gross pay
  42. float hoursTotal[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. float overtimePay [SIZE]; // overtime pay for a given week
  47. float normalPay [SIZE]; // normal weekly pay without any overtime
  48.  
  49. // process each employee
  50. for (i = 0; i < SIZE; ++i)
  51. {
  52.  
  53. // Read in hours for employee
  54. hoursTotal[i] = getHours (clockNumber[i]);
  55.  
  56. // TODO: Function call to calculate overtime hours
  57. overtimeHrs[i] = calcOT(hoursTotal[i]);
  58.  
  59. // TODO: Function call to calculate gross pay
  60. grossPay[i] = calcGross(wageRate[i], hoursTotal[i], overtimeHrs[i]);
  61.  
  62. } // End for
  63.  
  64. // print the header info
  65. printHeader();
  66.  
  67. // print out each employee
  68. for (i = 0; i < SIZE; ++i)
  69. {
  70.  
  71. // Print all the employees - call by value
  72. printEmp (clockNumber[i], wageRate[i], hoursTotal[i],
  73. overtimeHrs[i], grossPay[i]);
  74.  
  75. } // End for
  76.  
  77. return (0);
  78.  
  79. } // main
  80.  
  81. //**************************************************************
  82. // Function: getHours
  83. //
  84. // Purpose: Obtains input from user, the number of hours worked
  85. // per employee and stores the result in a local variable
  86. // that is passed back to the calling function.
  87. //
  88. // Parameters: clockNumber - The unique employee ID
  89. //
  90. // Returns: hoursWorked - hours worked in a given week
  91. //
  92. //**************************************************************
  93.  
  94. float getHours (long int clockNumber) //Called Function
  95. {
  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. // Function: printHeader
  110. //
  111. // Purpose: Prints the initial table header information.
  112. //
  113. // Parameters: none
  114. //
  115. // Returns: void
  116. //
  117. //**************************************************************
  118.  
  119. void printHeader (void)
  120. {
  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. // Function: printEmp
  132. //
  133. // Purpose: Prints out all the information for an employee
  134. // in a nice and orderly table format.
  135. //
  136. // Parameters:
  137. //
  138. // clockNumber - unique employee ID
  139. // wageRate - hourly wage rate
  140. // hours - Hours worked for the week
  141. // overtimeHrs - overtime hours worked in a week
  142. // grossPay - gross pay for the week
  143. //
  144. // Returns: void
  145. //
  146. //**************************************************************
  147.  
  148. void printEmp (long int clockNumber, float wageRate, float hoursTotal,
  149. float overtimeHrs, float grossPay)
  150. {
  151.  
  152. // print the employee
  153. // TODO: add code to print out a single employee
  154. printf("%06d %5.2f %5.1f %5.1f %8.2f\n",
  155. clockNumber, wageRate, hoursTotal, overtimeHrs, grossPay);
  156. } //printEmp
  157.  
  158.  
  159. // TODO: Add other functions here as needed
  160. // ... remember your comment block headers for each function
  161.  
  162. //*************************************************************
  163. // Function: calcOT
  164. //
  165. // Purpose: Calculate the overtime hours of employees if logged
  166. // more than the standard week
  167. //
  168. // Parameters:
  169. // hoursTotal- total logged hours per employee
  170. // STD_WORK_WEEK - the standard amount of 40 hours per week
  171. //
  172. // Returns: overtimeHrs
  173. //
  174. //**************************************************************
  175. //
  176. float calcOT (float hoursTotal) // Function Definition
  177. {
  178. //local variable
  179. float overtimeHrs;
  180. //computation
  181. if (hoursTotal > STD_WORK_WEEK)
  182. {
  183. overtimeHrs = hoursTotal - STD_WORK_WEEK;
  184. }
  185. //return computation
  186. return (overtimeHrs);
  187. } // calcOT
  188.  
  189. //*************************************************************
  190. // Function: calcGross
  191. //
  192. // Purpose: Calculate the Gross Pay of employees considering
  193. // any overtime hours logged
  194. //
  195. // Parameters:
  196. // wageRate - hourly wage of employees
  197. // hoursTotal- the total hours logged for employees
  198. // overtimeHrs - any overtime hours for any applicable employee logs
  199. //
  200. // Returns: grossPay
  201. //
  202. //**************************************************************
  203. //
  204. float calcGross (float wageRate, float hoursTotal, float overtimeHrs) // Function Definition
  205. {
  206. //local variable
  207. float grossPay;
  208. float normalPay;
  209. float overtimePay;
  210. //computation
  211. if (hoursTotal > STD_WORK_WEEK)
  212. {
  213. overtimeHrs = hoursTotal - STD_WORK_WEEK;
  214. overtimePay = overtimeHrs * (OVERTIME_RATE*wageRate);
  215. normalPay = STD_WORK_WEEK*wageRate;
  216. grossPay = normalPay + overtimePay;
  217. } // End if
  218.  
  219. else // No Overtime
  220. {
  221. overtimeHrs=0;
  222. normalPay=wageRate*hoursTotal;
  223. grossPay=normalPay;
  224. } // End else
  225. //return computation
  226. return (grossPay);
  227.  
  228. } // calcGross
  229. //**************************************************************
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