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