fork download
  1. #include <stdio.h>
  2.  
  3. // ------------------------------------------------------------
  4. // Program Constants
  5. // ------------------------------------------------------------
  6. #define SIZE 5 // number of employees to process
  7. #define STD_HOURS 40.0 // standard weekly hours before overtime applies
  8. #define OT_RATE 1.5 // overtime pay multiplier (time and a half)
  9.  
  10. int main()
  11. {
  12. // ------------------------------------------------------------
  13. // Variable Declarations
  14. // ------------------------------------------------------------
  15.  
  16. // Unique employee ID numbers
  17. long int clockNumber[SIZE] = {98401, 526488, 765349, 34645, 127615};
  18.  
  19. // Hourly wage rate for each employee
  20. float wageRate[SIZE] = {10.6, 9.75, 10.5, 12.25, 8.35};
  21.  
  22. // Arrays to store weekly payroll data
  23. float hours[SIZE]; // hours worked in the week
  24. float overtimeHrs[SIZE]; // overtime hours worked
  25. float normalPay[SIZE]; // pay for up to 40 hours
  26. float overtimePay[SIZE]; // pay for overtime hours
  27. float grossPay[SIZE]; // total weekly pay (normal + overtime)
  28.  
  29. int i; // loop index
  30.  
  31. // Totals for summary calculations
  32. float totalWage = 0.0; // total of all wage rates
  33. float totalHours = 0.0; // total hours worked
  34. float totalOTHours = 0.0; // total overtime hours
  35. float totalNormal = 0.0; // total normal pay
  36. float totalOTPay = 0.0; // total overtime pay
  37. float totalGross = 0.0; // total gross pay
  38.  
  39. // ------------------------------------------------------------
  40. // Program Header
  41. // ------------------------------------------------------------
  42. printf("\n*** Pay Calculator ***\n\n");
  43.  
  44. // ------------------------------------------------------------
  45. // Input Loop — Read Hours Worked for Each Employee
  46. // ------------------------------------------------------------
  47. for (i = 0; i < SIZE; i++)
  48. {
  49. // Prompt user for hours worked for the current employee
  50. printf("\nEnter the number of hours worked for employee %ld: ",
  51. clockNumber[i]);
  52. scanf("%f", &hours[i]);
  53.  
  54. // --------------------------------------------------------
  55. // Payroll Calculations for Each Employee
  56. // --------------------------------------------------------
  57.  
  58. // Check if employee worked overtime
  59. if (hours[i] >= STD_HOURS)
  60. {
  61. // Calculate overtime hours
  62. overtimeHrs[i] = hours[i] - STD_HOURS;
  63.  
  64. // Normal pay is capped at 40 hours
  65. normalPay[i] = STD_HOURS * wageRate[i];
  66.  
  67. // Overtime pay uses the OT multiplier
  68. overtimePay[i] = overtimeHrs[i] * wageRate[i] * OT_RATE;
  69. }
  70. else
  71. {
  72. // No overtime worked
  73. overtimeHrs[i] = 0.0;
  74.  
  75. // All hours are normal hours
  76. normalPay[i] = hours[i] * wageRate[i];
  77.  
  78. // No overtime pay
  79. overtimePay[i] = 0.0;
  80. }
  81.  
  82. // Gross pay is the sum of normal and overtime pay
  83. grossPay[i] = normalPay[i] + overtimePay[i];
  84. }
  85.  
  86. // ------------------------------------------------------------
  87. // Output Table Header
  88. // ------------------------------------------------------------
  89. printf("\nClock# Wage Hours OT Normal OT Pay Gross\n");
  90. printf("-----------------------------------------------------------\n");
  91.  
  92. // ------------------------------------------------------------
  93. // Output Loop — Print Payroll Data for Each Employee
  94. // ------------------------------------------------------------
  95. for (i = 0; i < SIZE; i++)
  96. {
  97. // Print one row of payroll information
  98. printf("%06ld %5.2f %5.1f %4.1f %7.2f %7.2f %7.2f\n",
  99. clockNumber[i],
  100. wageRate[i],
  101. hours[i],
  102. overtimeHrs[i],
  103. normalPay[i],
  104. overtimePay[i],
  105. grossPay[i]);
  106.  
  107. // --------------------------------------------------------
  108. // Accumulate Totals for Summary Section
  109. // --------------------------------------------------------
  110. totalWage += wageRate[i];
  111. totalHours += hours[i];
  112. totalOTHours += overtimeHrs[i];
  113. totalNormal += normalPay[i];
  114. totalOTPay += overtimePay[i];
  115. totalGross += grossPay[i];
  116. }
  117.  
  118. // Divider line before totals
  119. printf("-----------------------------------------------------------\n");
  120.  
  121. // ------------------------------------------------------------
  122. // Totals Row — Aligned with Table Columns
  123. // ------------------------------------------------------------
  124. printf("TOTALS %5.2f %5.1f %4.1f %7.2f %7.2f %7.2f\n",
  125. totalWage, // total wage rates
  126. totalHours, // total hours worked
  127. totalOTHours, // total overtime hours
  128. totalNormal, // total normal pay
  129. totalOTPay, // total overtime pay
  130. totalGross); // total gross pay
  131.  
  132. // ------------------------------------------------------------
  133. // Averages Row — Aligned with Table Columns
  134. // ------------------------------------------------------------
  135. printf("AVERAGES %5.2f %5.1f %4.1f %7.2f %7.2f %7.2f\n",
  136. totalWage / SIZE, // average wage rate
  137. totalHours / SIZE, // average hours worked
  138. totalOTHours / SIZE, // average overtime hours
  139. totalNormal / SIZE, // average normal pay
  140. totalOTPay / SIZE, // average overtime pay
  141. totalGross / SIZE); // average gross pay
  142.  
  143. // End of program
  144. return 0;
  145. }
Success #stdin #stdout 0s 5316KB
stdin
51.0
42.5
37.0
45.0
0.0
stdout
*** Pay Calculator ***


Enter the number of hours worked for employee 98401: 
Enter the number of hours worked for employee 526488: 
Enter the number of hours worked for employee 765349: 
Enter the number of hours worked for employee 34645: 
Enter the number of hours worked for employee 127615: 
Clock#     Wage   Hours   OT   Normal   OT Pay   Gross
-----------------------------------------------------------
098401   10.60   51.0  11.0   424.00   174.90   598.90
526488    9.75   42.5   2.5   390.00    36.56   426.56
765349   10.50   37.0   0.0   388.50     0.00   388.50
034645   12.25   45.0   5.0   490.00    91.88   581.88
127615    8.35    0.0   0.0     0.00     0.00     0.00
-----------------------------------------------------------
TOTALS    51.45  175.5  18.5  1692.50   303.34  1995.84
AVERAGES  10.29   35.1   3.7   338.50    60.67   399.17