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