fork download
  1. #include <stdio.h>
  2.  
  3. // Define constants for the program
  4. #define NUM_EMPL 5
  5. #define STD_HOURS 40.0
  6. #define OT_RATE 1.5
  7.  
  8. // Define a struct for storing employee data
  9. struct employee
  10. {
  11. long int clockNumber; // employee ID number
  12. float wageRate; // hourly wage rate
  13. float hours; // hours worked in a week
  14. float overtimeHrs; // overtime hours worked in a week
  15. float grossPay; // gross pay for the week
  16. };
  17.  
  18. // Declare functions used in the program
  19. float getHours(long int clockNumber); // gets the number of hours worked by an employee
  20. void printHeader(void); // prints a header for the table of employee data
  21. void printEmp(struct employee emp); // prints a row of data for a single employee
  22.  
  23. int main()
  24. {
  25. // Declare an array of employee structs and initialize with data
  26. struct employee employees[NUM_EMPL] = {
  27. {98401, 10.60},
  28. {526488, 9.75},
  29. {765349, 10.50},
  30. {34645, 12.25},
  31. {127615, 8.35}
  32. };
  33.  
  34. // Loop through each employee, get their hours worked, and calculate their gross pay
  35. for (int i = 0; i < NUM_EMPL; ++i)
  36. {
  37. employees[i].hours = getHours(employees[i].clockNumber);
  38.  
  39. // If the employee worked overtime, calculate the overtime pay
  40. if (employees[i].hours > STD_HOURS)
  41. {
  42. employees[i].overtimeHrs = employees[i].hours - STD_HOURS;
  43. employees[i].grossPay = STD_HOURS * employees[i].wageRate
  44. + employees[i].overtimeHrs * employees[i].wageRate * OT_RATE;
  45. }
  46. // If the employee did not work overtime, calculate regular pay only
  47. else
  48. {
  49. employees[i].overtimeHrs = 0.0;
  50. employees[i].grossPay = employees[i].hours * employees[i].wageRate;
  51. }
  52. }
  53.  
  54. // Print a header for the table of employee data
  55. printHeader();
  56.  
  57. // Loop through each employee and print their data
  58. for (int i = 0; i < NUM_EMPL; ++i)
  59. {
  60. printEmp(employees[i]);
  61. }
  62.  
  63. return 0;
  64. }
  65.  
  66. // Function to get the number of hours worked by an employee
  67. float getHours(long int clockNumber)
  68. {
  69. float hoursWorked;
  70.  
  71. // Prompt the user to enter the number of hours worked by the employee
  72. printf("Enter hours worked by emp # %06li: ", clockNumber);
  73. scanf("%f", &hoursWorked);
  74.  
  75. return hoursWorked;
  76. }
  77.  
  78. // Function to print a header for the table of employee data
  79. void printHeader(void)
  80. {
  81. printf("\n*** Pay Calculator ***\n\n");
  82. printf("Clock# Wage Hours OT Gross\n");
  83. printf("--------------------------------------\n");
  84. }
  85.  
  86. // Function to print a row of data for a single employee
  87. void printEmp(struct employee emp)
  88. {
  89. // Print the employee's ID number, wage rate, hours worked, overtime hours worked, and gross pay
  90. printf("%06li %5.2f %5.1f %5.1f %7.2f\n",
  91. emp.clockNumber, emp.wageRate, emp.hours,
  92. emp.overtimeHrs, emp.grossPay);
  93. }
Success #stdin #stdout 0.01s 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 Calculator ***

Clock#   Wage  Hours      OT     Gross
--------------------------------------
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