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 printHeader (void);
  11. void printEmp (long int clockNumber, float wageRate, float hours,
  12. float overtimeHrs, float grossPay);
  13.  
  14. // Other required function prototypes
  15. float calculateOvertime (float hours);
  16. float calculateGrossPay (float wageRate, float hours, float overtimeHrs);
  17.  
  18. int main()
  19. {
  20. /* Variable Declarations */
  21. long int clockNumber[SIZE] = {98401, 526488, 765349, 34645, 127615}; // ID
  22. float grossPay[SIZE]; // gross pay
  23. float hours[SIZE]; // hours worked in a given week
  24. int i; // loop and array index
  25. float overtimeHrs[SIZE]; // overtime hours
  26. float wageRate[SIZE] = {10.60, 9.75, 10.50, 12.25, 8.35}; // hourly wage rate
  27.  
  28. // process each employee
  29. for (i = 0; i < SIZE; ++i)
  30. {
  31. // Read in hours for employee
  32. hours[i] = getHours (clockNumber[i]);
  33.  
  34. // Function call to calculate overtime hours
  35. overtimeHrs[i] = calculateOvertime (hours[i]);
  36.  
  37. // Function call to calculate gross pay
  38. grossPay[i] = calculateGrossPay (wageRate[i], hours[i], overtimeHrs[i]);
  39. }
  40.  
  41. // print the header info
  42. printHeader();
  43.  
  44. // print out each employee
  45. for (i = 0; i < SIZE; ++i)
  46. {
  47. // Print all the employees - call by value
  48. printEmp (clockNumber[i], wageRate[i], hours[i],
  49. overtimeHrs[i], grossPay[i]);
  50. }
  51.  
  52. return (0);
  53. }
  54.  
  55. //**************************************************************
  56. // Function: getHours
  57. // Purpose: Obtains input from user for hours worked per employee
  58. //**************************************************************
  59. float getHours (long int clockNumber)
  60. {
  61. float hoursWorked;
  62. printf("Enter hours worked for clock #%ld: ", clockNumber);
  63. scanf("%f", &hoursWorked);
  64. return hoursWorked;
  65. }
  66.  
  67. //**************************************************************
  68. // Function: calculateOvertime
  69. // Purpose: Computes the number of hours worked past 40
  70. //**************************************************************
  71. float calculateOvertime (float hours)
  72. {
  73. if (hours > STD_WORK_WEEK)
  74. {
  75. return hours - STD_WORK_WEEK;
  76. }
  77. return 0.0f;
  78. }
  79.  
  80. //**************************************************************
  81. // Function: calculateGrossPay
  82. // Purpose: Calculates regular pay and adds 1.5x overtime pay
  83. //**************************************************************
  84. float calculateGrossPay (float wageRate, float hours, float overtimeHrs)
  85. {
  86. float regularHours = hours - overtimeHrs;
  87. float regularPay = regularHours * wageRate;
  88. float overtimePay = overtimeHrs * (wageRate * OVERTIME_RATE);
  89.  
  90. return regularPay + overtimePay;
  91. }
  92.  
  93. //**************************************************************
  94. // Function: printHeader
  95. // Purpose: Prints the table column headers
  96. //**************************************************************
  97. void printHeader (void)
  98. {
  99. printf("\n--------------------------------------------------------------\n");
  100. printf("%-10s %-10s %-10s %-12s %-10s\n",
  101. "Clock #", "Wage Rate", "Hours", "OT Hours", "Gross Pay");
  102. printf("--------------------------------------------------------------\n");
  103. }
  104.  
  105. //**************************************************************
  106. // Function: printEmp
  107. // Purpose: Prints aligned fields for a single employee record
  108. //**************************************************************
  109. void printEmp (long int clockNumber, float wageRate, float hours,
  110. float overtimeHrs, float grossPay)
  111. {
  112. printf("%-10ld $%-9.2f %-10.1f %-12.1f $%-9.2f\n",
  113. clockNumber, wageRate, hours, overtimeHrs, grossPay);
  114. }
  115.  
Success #stdin #stdout 0.01s 5288KB
stdin
51.0
42.5
37.0
45.0
0.0
stdout
Enter hours worked for clock #98401: Enter hours worked for clock #526488: Enter hours worked for clock #765349: Enter hours worked for clock #34645: Enter hours worked for clock #127615: 
--------------------------------------------------------------
Clock #    Wage Rate  Hours      OT Hours     Gross Pay 
--------------------------------------------------------------
98401      $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   
34645      $12.25     45.0       5.0          $581.88   
127615     $8.35      0.0        0.0          $0.00