fork download
  1.  
  2. #include <stdio.h>
  3.  
  4. // Constants
  5. #define SIZE 5
  6. #define OVERTIME_RATE 1.5
  7. #define STD_WORK_WEEK 40.0
  8.  
  9. // Function prototypes
  10. float getHours(long int clockNumber);
  11. void printHeader(void);
  12. void printEmp(long int clockNumber, float wageRate, float hours, float overtimeHrs, float grossPay);
  13. float calculateOvertime(float hours);
  14. float calculateGrossPay(float wageRate, float hours, float overtimeHrs);
  15.  
  16. int main() {
  17. // Employee data
  18. long int clockNumber[SIZE] = {98401, 52648, 76534, 34645, 12761};
  19. float grossPay[SIZE];
  20. float hours[SIZE];
  21. float overtimeHrs[SIZE];
  22. float wageRate[SIZE] = {10.60, 9.75, 10.50, 12.25, 8.35};
  23.  
  24. // Process each employee
  25. for (int i = 0; i < SIZE; i++) {
  26. // Read hours for employee
  27. hours[i] = getHours(clockNumber[i]);
  28.  
  29. // Calculate overtime hours
  30. overtimeHrs[i] = calculateOvertime(hours[i]);
  31.  
  32. // Calculate gross pay
  33. grossPay[i] = calculateGrossPay(wageRate[i], hours[i], overtimeHrs[i]);
  34. }
  35.  
  36. // Print the header info
  37. printHeader();
  38.  
  39. // Print all employees
  40. for (int i = 0; i < SIZE; i++) {
  41. printEmp(clockNumber[i], wageRate[i], hours[i], overtimeHrs[i], grossPay[i]);
  42. }
  43.  
  44. return 0;
  45. }
  46.  
  47. // Function: getHours
  48. // Purpose: Obtains input from user, the number of hours worked per employee and stores the result.
  49. float getHours(long int clockNumber) {
  50. float hoursWorked;
  51. printf("Enter hours worked for employee ID %ld: ", clockNumber);
  52. scanf("%f", &hoursWorked);
  53. return hoursWorked;
  54. }
  55.  
  56. // Function: printHeader
  57. // Purpose: Prints the header for the employee summary output
  58. void printHeader(void) {
  59. printf("\nEmployee Summary:\n");
  60. printf("---------------------------------------------------------\n");
  61. printf("Employee ID | Wage Rate | Hours Worked | Overtime Hours | Gross Pay\n");
  62. printf("---------------------------------------------------------\n");
  63. }
  64.  
  65. // Function: printEmp
  66. // Purpose: Prints the details for each employee
  67. void printEmp(long int clockNumber, float wageRate, float hours, float overtimeHrs, float grossPay) {
  68. printf("%10ld | %9.2f | %12.2f | %14.2f | %9.2f\n", clockNumber, wageRate, hours, overtimeHrs, grossPay);
  69. }
  70.  
  71. // Function: calculateOvertime
  72. // Purpose: Calculates the overtime hours worked
  73. float calculateOvertime(float hours) {
  74. if (hours > STD_WORK_WEEK) {
  75. return hours - STD_WORK_WEEK;
  76. }
  77. return 0.0;
  78. }
  79.  
  80. // Function: calculateGrossPay
  81. // Purpose: Calculates the gross pay based on wage rate, hours worked, and overtime
  82. float calculateGrossPay(float wageRate, float hours, float overtimeHrs) {
  83. float regularPay = wageRate * (hours - overtimeHrs);
  84. float overtimePay = overtimeHrs * wageRate * OVERTIME_RATE;
  85. return regularPay + overtimePay;
  86. }
Success #stdin #stdout 0s 5284KB
stdin
Standard input is empty
stdout
Enter hours worked for employee ID 98401: Enter hours worked for employee ID 52648: Enter hours worked for employee ID 76534: Enter hours worked for employee ID 34645: Enter hours worked for employee ID 12761: 
Employee Summary:
---------------------------------------------------------
Employee ID | Wage Rate | Hours Worked | Overtime Hours | Gross Pay
---------------------------------------------------------
     98401 |     10.60 |         0.00 |           0.00 |      0.00
     52648 |      9.75 |         0.00 |           0.00 |      0.00
     76534 |     10.50 |         0.00 |           0.00 |      0.00
     34645 |     12.25 |         0.00 |           0.00 |      0.00
     12761 |      8.35 |         0.00 |           0.00 |      0.00