fork download
  1. //********************************************************
  2. //
  3. // Assignment 5 - Functions
  4. //
  5. // Name: Larson Klipic
  6. //
  7. // Class: C Programming, SUMMER 26
  8. //
  9. // Date: JUN 29th
  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[]);
  32. float OTwork (float hours);
  33. float pay (float wageRate, float hours, float overtimeHrs);
  34. // TODO: Add other function prototypes here as needed
  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 hours[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.  
  48. // process each employee
  49. for (i = 0; i < SIZE; ++i)
  50. {
  51.  
  52. // read in hours for the current employee
  53. hours[i] = getHours (clockNumber[i]);
  54.  
  55. // calculate overtime hours
  56. overtimeHrs[i] = OTwork (hours[i]);
  57. // calculate gross pay
  58. grossPay[i] = pay (wageRate[i],hours[i], overtimeHrs[i]);
  59.  
  60. }
  61.  
  62. // Print the header info
  63. printHeader();
  64.  
  65. // Print all the employees - call by reference
  66. printEmp (clockNumber, wageRate, hours,
  67. overtimeHrs, grossPay);
  68.  
  69. return (0);
  70.  
  71. } // main
  72.  
  73. //**************************************************************
  74. // Function: getHours
  75. //
  76. // Purpose: Obtains input from user, the number of hours worked
  77. // per employee and stores the result in a local variable
  78. // that is passed back to the calling function.
  79. //
  80. // Parameters: clockNumber - The unique employee ID
  81. //
  82. // Returns: hoursWorked - hours worked in a given week
  83. //
  84. //**************************************************************
  85.  
  86. float getHours (long int clockNumber)
  87. {
  88.  
  89. float hoursWorked; // hours worked in a given week
  90.  
  91. // Read in hours for employee
  92. printf("\nEnter hours worked by emp # %06li: ", clockNumber);
  93. scanf ("%f", &hoursWorked);
  94.  
  95. // return hours back to the calling function
  96. return (hoursWorked);
  97.  
  98. } // getHours
  99.  
  100. //**************************************************************
  101. // Function: printHeader
  102. //
  103. // Purpose: Prints the initial table header information.
  104. //
  105. // Parameters: none
  106. //
  107. // Returns: void
  108. //
  109. //**************************************************************
  110.  
  111. void printHeader (void)
  112. {
  113.  
  114. printf ("\n\n*** Pay Calculator ***\n");
  115.  
  116. // print the table header
  117. printf("\nClock# Wage Hours OT Gross\n");
  118. printf("------------------------------------------------\n");
  119.  
  120. } // printHeader
  121.  
  122. //*************************************************************
  123. // Function: printEmp
  124. //
  125. // Purpose: Prints out all the employee information in a
  126. // nice and orderly table format.
  127. //
  128. // Parameters:
  129. //
  130. // clockNumber - Array of employee clock numbers
  131. // wageRate - Array of employee wages per hour
  132. // hours - Array of number of hours worked by an employee
  133. // overtimeHrs - Array of overtime hours for each employee
  134. // grossPay - Array of gross pay calculations for each employee
  135. //
  136. // Returns: Nothing (call by reference)
  137. //
  138. //**************************************************************
  139.  
  140. void printEmp(long int clockNumber[], float wageRate[], float hours[],
  141. float overtimeHrs[], float grossPay[])
  142. {
  143.  
  144. int i; // loop index
  145.  
  146. // access and print each employee
  147. for (i = 0; i < SIZE; ++i)
  148. {
  149. // TODO: add code to print out each employee one at a time
  150. printf("%06ld %5.2f %5.1f %5.1f %8.2f\n", clockNumber[i],
  151. wageRate[i], hours[i], overtimeHrs[i], grossPay[i]);
  152. }
  153. } // printEmp
  154.  
  155. //**************************************************************
  156. // Function: OTwork
  157. //
  158. // Purpose: calculate the overtime for an employee.
  159. //
  160. // Parameters: hours - an array of time the employee worked
  161. //
  162. // Returns: hrsWork - overtime hours
  163. //
  164. //**************************************************************
  165.  
  166. float OTwork (float hours)
  167. {
  168. float hrsWork; // hours worked
  169.  
  170. // overtime
  171. if (hours > STD_WORK_WEEK )
  172. {
  173. hrsWork = hours - STD_WORK_WEEK;
  174. }
  175. else // no OT
  176. {
  177. hrsWork = 0.0;
  178. }
  179.  
  180. return(hrsWork);
  181. } // OTwork
  182.  
  183. //**************************************************************
  184. // Function: pay
  185. //
  186. // Purpose: calculate grosspay for an employee.
  187. //
  188. // Parameters: wageRate - hourly wage of a employee
  189. // hours - time worked from a employee
  190. // overtimeHrs - overtime hours of a empoyee
  191. //
  192. // Returns: gross - calculated grosspay of the employee
  193. //
  194. //**************************************************************
  195.  
  196. float pay (float wageRate, float hours, float overtimeHrs)
  197. {
  198. float gross; // gross pay
  199. float normalPay; // pay without overtime
  200. float overtimePay; // only overtime pay
  201.  
  202. // calculate gross with normal pay and overtime pay
  203. if (overtimeHrs > 0)
  204. {
  205. // overtime
  206. normalPay = wageRate * STD_WORK_WEEK;
  207. overtimePay = overtimeHrs * (wageRate * OVERTIME_RATE);
  208. gross = normalPay + overtimePay;
  209. }
  210. else
  211. {
  212. // No overtime
  213. gross = hours * wageRate;
  214. }
  215.  
  216. return (gross);
  217. } // pay
Success #stdin #stdout 0s 5328KB
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