fork download
  1. #include <stdio.h>
  2. /* constants */
  3. #define NUM_EMPL 5
  4. #define OVERTIME_RATE 1.5f
  5. #define STD_WORK_WEEK 40.0f
  6.  
  7. /* function prototypes */
  8. float getHours(long int clockNumber);
  9. float calcOvertime(float hoursWorked);
  10. float calcGrossPay(float hoursWorked, float wageRate, float overtimeHours);
  11.  
  12. void printHeader(void);
  13. void printEmp(long int clockNumber, float wageRate, float hours, float overtimeHrs, float grossPay);
  14.  
  15. int main() {
  16.  
  17. /* Variable Declarations */
  18. long int clockNumber[NUM_EMPL] = {98401, 526488, 765349, 34645, 127615}; /* ID */
  19. float grossPay[NUM_EMPL]; /* gross pay */
  20. float hours[NUM_EMPL]; /* hours worked in a given week */
  21. int i; /* loop and array index */
  22. float overtimeHrs[NUM_EMPL]; /* overtime hours */
  23. float wageRate[NUM_EMPL] = {10.60, 9.75, 10.50, 12.25, 8.35}; /* hourly wage rate */
  24.  
  25. /* process each employee */
  26. for (i = 0; i < NUM_EMPL; ++i) {
  27.  
  28. /* Read in the hours for an employee */
  29. hours[i] = getHours(clockNumber[i]);
  30.  
  31. /* calculate overtime */
  32. overtimeHrs[i] = calcOvertime(hours[i]);
  33.  
  34. /* calculate gross pay */
  35. grossPay[i] = calcGrossPay(hours[i], wageRate[i], overtimeHrs[i]);
  36. }
  37.  
  38. /* print the header info */
  39. printHeader();
  40.  
  41. /* print out each employee */
  42. for (i = 0; i < NUM_EMPL; ++i) {
  43.  
  44. /* Print all the employees - call by reference */
  45. printEmp(clockNumber[i], wageRate[i], hours[i], overtimeHrs[i], grossPay[i]);
  46. } /* for */
  47.  
  48. return (0);
  49. }
  50.  
  51. float getHours(long int clockNumber) {
  52.  
  53. float hoursWorked; /* hours worked in a given week */
  54.  
  55. /* Read in hours for employee */
  56. printf("\nEnter hours worked by emp # %06li: ", clockNumber);
  57. scanf("%f", &hoursWorked);
  58.  
  59. /* return hours back to the calling function */
  60. return (hoursWorked);
  61. }
  62.  
  63. /* function to calculate overtime hours */
  64. float calcOvertime(float hoursWorked) {
  65. float overtimeHrs; /* overtime hours worked */
  66. if (hoursWorked > STD_WORK_WEEK) {
  67. overtimeHrs = hoursWorked - STD_WORK_WEEK; /* calculate overtime */
  68. } else {
  69. overtimeHrs = 0.0; /* no overtime */
  70. }
  71. return overtimeHrs;
  72. }
  73.  
  74. /* function to print header information */
  75. void printHeader(void) {
  76. printf("\nPay Slip Report\n");
  77. printf("====================================\n");
  78. printf("ID Wage Rate Hours Overtime Gross Pay\n");
  79. printf("------------------------------------\n");
  80. }
  81.  
  82. /* function to print employee information */
  83. void printEmp(long int clockNumber, float wageRate, float hours,float overtimeHrs, float grossPay) {
  84. printf("%06li $%6.2f %6.1f %6.1f $%8.2f\n",clockNumber, wageRate, hours, overtimeHrs, grossPay);
  85. }
  86.  
  87. /* function to calculate gross pay */
  88. float calcGrossPay(float hoursWorked, float wageRate, float overtimeHrs) {
  89. float grossPay; // gross pay for the week
  90. if (overtimeHrs > 0.0) { // if overtime was worked
  91. grossPay = (STD_WORK_WEEK * wageRate) + (overtimeHrs * OVERTIME_RATE * wageRate);
  92. }
  93. else { // no overtime worked
  94. grossPay = hoursWorked * wageRate;
  95. }
  96. return grossPay;
  97. }
  98.  
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: 
Pay Slip Report
====================================
ID  Wage Rate Hours Overtime Gross Pay
------------------------------------
098401 $ 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
034645 $ 12.25    0.0    0.0 $    0.00
127615 $  8.35    0.0    0.0 $    0.00