fork download
  1. #include <stdio.h>
  2.  
  3. // constants
  4. #define SIZE 5 // number of employees
  5. #define OVERTIME_RATE 1.5f // overtime pay multiplier
  6. #define STD_WORK_WEEK 40.0f // standard hours before overtime
  7.  
  8. // function prototypes
  9. float getHours (long int clockNumber);
  10. float calcOvertime (float hours);
  11. float calcGross (float hours, float wageRate, float overtimeHrs);
  12. void printHeader (void);
  13. void printEmp (long int clockNumber, float wageRate, float hours,
  14. float overtimeHrs, float grossPay);
  15. void printTotals(float totalWage, float totalHours,
  16. float totalOT, float totalGross);
  17.  
  18. /********************************************************************
  19. Function: main
  20. Purpose: Controls the payroll program. It collects employee hours,
  21.   calculates overtime and gross pay, prints the payroll
  22.   report, and displays totals and averages.
  23. ********************************************************************/
  24. int main()
  25. {
  26. long int clockNumber[SIZE] = {98401,526488,765349,34645,127615}; // employee clock numbers
  27. float grossPay[SIZE]; // array to store gross pay for each employee
  28. float hours[SIZE]; // array to store hours worked
  29. float overtimeHrs[SIZE]; // array to store overtime hours
  30. float wageRate[SIZE] = {10.60,9.75,10.50,12.25,8.35}; // hourly wage rates
  31.  
  32. float totalWage = 0.0f; // sum of all wage rates
  33. float totalHours = 0.0f; // total hours worked by all employees
  34. float totalOT = 0.0f; // total overtime hours
  35. float totalGross = 0.0f; // total gross pay
  36.  
  37. int i; // loop counter
  38.  
  39. // Loop through all employees to collect hours and calculate payroll values
  40. for (i = 0; i < SIZE; ++i)
  41. {
  42. // Get hours worked from the user
  43. hours[i] = getHours(clockNumber[i]);
  44.  
  45. // Calculate overtime hours
  46. overtimeHrs[i] = calcOvertime(hours[i]);
  47.  
  48. // Calculate gross pay including overtime
  49. grossPay[i] = calcGross(hours[i], wageRate[i], overtimeHrs[i]);
  50.  
  51. // Accumulate totals for report summary
  52. totalWage += wageRate[i];
  53. totalHours += hours[i];
  54. totalOT += overtimeHrs[i];
  55. totalGross += grossPay[i];
  56. }
  57.  
  58. // Print the report header
  59. printHeader();
  60.  
  61. // Loop again to print each employee's payroll data
  62. for (i = 0; i < SIZE; ++i)
  63. {
  64. printEmp(clockNumber[i], wageRate[i], hours[i],
  65. overtimeHrs[i], grossPay[i]);
  66. }
  67.  
  68. // Print totals and averages
  69. printTotals(totalWage, totalHours, totalOT, totalGross);
  70.  
  71. return 0;
  72. }
  73.  
  74.  
  75. /********************************************************************
  76. Function: getHours
  77. Purpose: Prompts the user to enter the hours worked for a specific
  78.   employee and returns that value.
  79.  
  80. Parameters:
  81.   clockNumber - employee identification number
  82.  
  83. Returns:
  84.   float - number of hours worked
  85. ********************************************************************/
  86. float getHours (long int clockNumber)
  87. {
  88. float hoursWorked; // stores the number of hours entered by the user
  89.  
  90. // Prompt user to enter hours worked for the given employee
  91. printf("\n Hours worked by emp # %06li: ", clockNumber);
  92.  
  93. // Read the hours from keyboard input
  94. scanf ("%f", &hoursWorked);
  95.  
  96. // Return the hours worked to the calling function
  97. return hoursWorked;
  98. }
  99.  
  100.  
  101. /********************************************************************
  102. Function: calcOvertime
  103. Purpose: Determines how many overtime hours an employee worked.
  104.  
  105. Parameters:
  106.   hours - total hours worked
  107.  
  108. Returns:
  109.   float - overtime hours
  110. ********************************************************************/
  111. float calcOvertime (float hours)
  112. {
  113. float overtime; // variable to store calculated overtime hours
  114.  
  115. // Check if employee worked more than standard work week
  116. if (hours > STD_WORK_WEEK)
  117. overtime = hours - STD_WORK_WEEK; // overtime is extra hours above 40
  118. else
  119. overtime = 0.0f; // no overtime if 40 or fewer hours worked
  120.  
  121. // Return overtime hours
  122. return overtime;
  123. }
  124.  
  125.  
  126. /********************************************************************
  127. Function: calcGross
  128. Purpose: Calculates the total gross pay including regular and
  129.   overtime pay.
  130.  
  131. Parameters:
  132.   hours - total hours worked
  133.   wageRate - hourly pay rate
  134.   overtimeHrs - overtime hours worked
  135.  
  136. Returns:
  137.   float - total gross pay
  138. ********************************************************************/
  139. float calcGross (float hours, float wageRate, float overtimeHrs)
  140. {
  141. float regularPay; // pay for regular hours
  142. float overtimePay; // pay earned from overtime
  143. float gross; // total gross pay
  144.  
  145. // Calculate pay for regular hours
  146. regularPay = (hours - overtimeHrs) * wageRate;
  147.  
  148. // Calculate overtime pay using overtime rate multiplier
  149. overtimePay = overtimeHrs * wageRate * OVERTIME_RATE;
  150.  
  151. // Total gross pay is the sum of regular and overtime pay
  152. gross = regularPay + overtimePay;
  153.  
  154. // Return the calculated gross pay
  155. return gross;
  156. }
  157.  
  158.  
  159. /********************************************************************
  160. Function: printHeader
  161. Purpose: Prints the payroll report header and column titles.
  162.  
  163. Parameters:
  164.   None
  165.  
  166. Returns:
  167.   Nothing
  168. ********************************************************************/
  169. void printHeader (void)
  170. {
  171. // Print title of payroll report
  172. printf("\n\n*** Pay Calculator ***\n");
  173.  
  174. // Print table formatting lines and column headers
  175. printf("\n----------------------------------------------------------\n");
  176. printf("Clock# Wage Hours OT Gross\n");
  177. printf("----------------------------------------------------------\n");
  178. }
  179.  
  180.  
  181. /********************************************************************
  182. Function: printEmp
  183. Purpose: Prints payroll information for one employee.
  184.  
  185. Parameters:
  186.   clockNumber - employee clock number
  187.   wageRate - employee hourly wage
  188.   hours - hours worked
  189.   overtimeHrs - overtime hours worked
  190.   grossPay - calculated gross pay
  191.  
  192. Returns:
  193.   Nothing
  194. ********************************************************************/
  195. void printEmp (long int clockNumber, float wageRate, float hours,
  196. float overtimeHrs, float grossPay)
  197. {
  198. // Print employee payroll information in formatted columns
  199. printf("%06li %5.2f %5.1f %5.1f %8.2f\n",
  200. clockNumber, wageRate, hours, overtimeHrs, grossPay);
  201. }
  202.  
  203.  
  204. /********************************************************************
  205. Function: printTotals
  206. Purpose: Displays total and average payroll statistics for all
  207.   employees.
  208.  
  209. Parameters:
  210.   totalWage - sum of all wage rates
  211.   totalHours - total hours worked
  212.   totalOT - total overtime hours
  213.   totalGross - total gross pay
  214.  
  215. Returns:
  216.   Nothing
  217. ********************************************************************/
  218. void printTotals(float totalWage, float totalHours,
  219. float totalOT, float totalGross)
  220. {
  221. // Print separator line
  222. printf("----------------------------------------------------------\n");
  223.  
  224. // Print total values
  225. printf("Total %5.2f %5.1f %5.1f %8.2f\n",
  226. totalWage, totalHours, totalOT, totalGross);
  227.  
  228. // Print averages by dividing totals by number of employees
  229. printf("Average %5.2f %5.1f %5.1f %8.2f\n",
  230. totalWage / SIZE,
  231. totalHours / SIZE,
  232. totalOT / SIZE,
  233. totalGross / SIZE);
  234. }
Success #stdin #stdout 0s 5324KB
stdin
51.0
42.5
37.0
45.0
0.0
stdout
 Hours worked by emp # 098401: 
 Hours worked by emp # 526488: 
 Hours worked by emp # 765349: 
 Hours worked by emp # 034645: 
 Hours worked by emp # 127615: 

*** Pay Calculator ***

----------------------------------------------------------
Clock#   Wage   Hours    OT     Gross
----------------------------------------------------------
098401   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
034645   12.25    45.0     5.0     581.88
127615    8.35     0.0     0.0       0.00
----------------------------------------------------------
Total    51.45   175.5    18.5    1995.84
Average  10.29    35.1     3.7     399.17