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. float getHours(long int clockNumber);
  10. void calculateOvertime(float hoursWorked, float *overtimeHrs);
  11. void calculateGrossPay(float hours, float overtimeHrs, float wageRate, float *grossPay);
  12. void printHeader(void);
  13. void printEmp(long int clockNumber, float wageRate, float hours, float overtimeHrs, float grossPay);
  14.  
  15. int main() {
  16. /* Variable Declarations */
  17. long int clockNumber[SIZE] = {98401, 526488, 765349, 34645, 127615}; // ID
  18. float grossPay[SIZE]; // gross pay
  19. float hours[SIZE]; // hours worked in a given week
  20. float overtimeHrs[SIZE]; // overtime hours
  21. float wageRate[SIZE] = {10.60, 9.75, 10.50, 12.25, 8.35}; // hourly wage rate
  22.  
  23. // process each employee
  24. for (int i = 0; i < SIZE; ++i) {
  25. // Read in hours for employee
  26. hours[i] = getHours(clockNumber[i]);
  27.  
  28. // Calculate overtime hours
  29. calculateOvertime(hours[i], &overtimeHrs[i]);
  30.  
  31. // Calculate gross pay
  32. calculateGrossPay(hours[i], overtimeHrs[i], wageRate[i], &grossPay[i]);
  33. }
  34.  
  35. // print the header info
  36. printHeader();
  37.  
  38. // print out each employee
  39. for (int i = 0; i < SIZE; ++i) {
  40. // Print all the employees
  41. printEmp(clockNumber[i], wageRate[i], hours[i], overtimeHrs[i], grossPay[i]);
  42. }
  43.  
  44. return 0;
  45. }
  46.  
  47. // Function to obtain input from the user, the number of hours worked per employee.
  48. float getHours(long int clockNumber) {
  49. float hoursWorked;
  50. printf("\nEnter hours worked by emp # %06li: ", clockNumber);
  51. scanf("%f", &hoursWorked);
  52. return hoursWorked;
  53. }
  54.  
  55. // Function to calculate overtime hours
  56. void calculateOvertime(float hoursWorked, float *overtimeHrs) {
  57. if (hoursWorked > STD_WORK_WEEK) {
  58. *overtimeHrs = hoursWorked - STD_WORK_WEEK;
  59. } else {
  60. *overtimeHrs = 0.0;
  61. }
  62. }
  63.  
  64. // Function to calculate gross pay
  65. void calculateGrossPay(float hours, float overtimeHrs, float wageRate, float *grossPay) {
  66. *grossPay = (hours * wageRate) + (overtimeHrs * wageRate * OVERTIME_RATE);
  67. }
  68.  
  69. // Function to print the initial table header information.
  70. void printHeader(void) {
  71. printf("---------------------------------------------\n");
  72. printf("| Clock# | Wage | Hours | OT | Gross |\n");
  73. printf("---------------------------------------------\n");
  74. }
  75.  
  76. // Function to print out all the information for an employee in a table format.
  77. void printEmp(long int clockNumber, float wageRate, float hours, float overtimeHrs, float grossPay) {
  78. printf("| %8li | %6.2f | %5.1f | %4.1f | %6.2f |\n", clockNumber, wageRate, hours, overtimeHrs, grossPay);
  79. printf("---------------------------------------------\n");
  80. }
  81.  
  82.  
Success #stdin #stdout 0s 5288KB
stdin
Standard input is empty
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: ---------------------------------------------
|   Clock#  |  Wage  | Hours |  OT  |  Gross |
---------------------------------------------
|    98401 |  10.60 |   0.0 |  0.0 |   0.00 |
---------------------------------------------
|   526488 |   9.75 |   0.0 |  0.0 |   0.00 |
---------------------------------------------
|   765349 |  10.50 |   0.0 |  0.0 |   0.00 |
---------------------------------------------
|    34645 |  12.25 |   0.0 |  0.0 |   0.00 |
---------------------------------------------
|   127615 |   8.35 |   0.0 |  0.0 |   0.00 |
---------------------------------------------