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