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