fork(1) 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. float calculateOvertime(float hours);
  14. float calculateGrossPay(float wageRate, float hours, float overtimeHrs);
  15.  
  16. int main() {
  17. // Variable Declarations
  18. long int clockNumber[SIZE] = {98401, 526488, 765349, 34645, 127615}; // ID
  19. float grossPay[SIZE]; // Gross pay
  20. float hours[SIZE]; // Hours worked in a given week
  21. float overtimeHrs[SIZE]; // Overtime hours
  22. float wageRate[SIZE] = {10.60, 9.75, 10.50, 12.25, 8.35}; // Hourly wage rate
  23. int i; // Loop index
  24.  
  25. // Process each employee
  26. for (i = 0; i < SIZE; ++i) {
  27. hours[i] = getHours(clockNumber[i]);
  28. overtimeHrs[i] = calculateOvertime(hours[i]);
  29. grossPay[i] = calculateGrossPay(wageRate[i], hours[i], overtimeHrs[i]);
  30. }
  31.  
  32. // Print the header info
  33. printHeader();
  34.  
  35. // Print out each employee's details
  36. for (i = 0; i < SIZE; ++i) {
  37. printEmp(clockNumber[i], wageRate[i], hours[i], overtimeHrs[i], grossPay[i]);
  38. }
  39.  
  40. return 0;
  41. }
  42.  
  43. // Function to get hours worked from user
  44. float getHours(long int clockNumber) {
  45. float hoursWorked;
  46. printf("\nEnter hours worked by emp # %06li: ", clockNumber);
  47. scanf("%f", &hoursWorked);
  48. return hoursWorked;
  49. }
  50.  
  51. // Function to print the table header
  52. void printHeader(void) {
  53. printf("\n*** Pay Calculator ***\n\n");
  54. printf("Clock# Wage Hours OT Gross\n");
  55. printf("------------------------------------\n");
  56. }
  57.  
  58. // Function to print employee details
  59. void printEmp(long int clockNumber, float wageRate, float hours,
  60. float overtimeHrs, float grossPay) {
  61. printf("%06li %.2f %.2f %.2f %.2f\n",
  62. clockNumber, wageRate, hours, overtimeHrs, grossPay);
  63. }
  64.  
  65. // Function to calculate overtime hours
  66. float calculateOvertime(float hours) {
  67. return (hours > STD_WORK_WEEK) ? (hours - STD_WORK_WEEK) : 0.0f;
  68. }
  69.  
  70. // Function to calculate gross pay
  71. float calculateGrossPay(float wageRate, float hours, float overtimeHrs) {
  72. float regularPay = wageRate * (hours - overtimeHrs);
  73. float overtimePay = overtimeHrs * (wageRate * OVERTIME_RATE);
  74. return regularPay + overtimePay;
  75. }
Success #stdin #stdout 0s 5276KB
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 Calculator ***

Clock#   Wage  Hours  OT     Gross
------------------------------------
098401  10.60  0.00  0.00  0.00
526488  9.75  0.00  0.00  0.00
765349  10.50  0.00  0.00  0.00
034645  12.25  0.00  0.00  0.00
127615  8.35  0.00  0.00  0.00