fork download
  1. #include <stdio.h>
  2.  
  3. // constants
  4. #define SIZE 5 // total number of employees in the payroll system
  5. #define OVERTIME_RATE 1.5f // overtime pay multiplier (time and a half)
  6. #define STD_WORK_WEEK 40.0f // number of regular hours before overtime begins
  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 entire payroll process. The function gathers
  21.   employee work hours, calculates overtime and gross pay,
  22.   stores results in arrays, prints a payroll report, and
  23.   displays totals and averages for all employees.
  24.  
  25. Parameters:
  26.   None
  27.  
  28. Returns:
  29.   int - program exit status (0 indicates successful execution)
  30. ********************************************************************/
  31. int main()
  32. {
  33. long int clockNumber[SIZE] = {98401,526488,765349,34645,127615};
  34. // array storing each employee's clock number (ID)
  35.  
  36. float grossPay[SIZE];
  37. // array storing calculated gross pay for each employee
  38.  
  39. float hours[SIZE];
  40. // array storing hours worked for each employee
  41.  
  42. float overtimeHrs[SIZE];
  43. // array storing overtime hours for each employee
  44.  
  45. float wageRate[SIZE] = {10.60,9.75,10.50,12.25,8.35};
  46. // array storing hourly wage rate for each employee
  47.  
  48. float totalWage = 0.0f;
  49. // accumulator storing total of all wage rates
  50.  
  51. float totalHours = 0.0f;
  52. // accumulator storing total hours worked by all employees
  53.  
  54. float totalOT = 0.0f;
  55. // accumulator storing total overtime hours worked
  56.  
  57. float totalGross = 0.0f;
  58. // accumulator storing total gross pay for all employees
  59.  
  60. int i;
  61. // loop counter used to iterate through employee arrays
  62.  
  63. // ----------------------------------------------------------
  64. // First loop: gather employee hours and calculate payroll
  65. // ----------------------------------------------------------
  66. for (i = 0; i < SIZE; ++i)
  67. {
  68. // Prompt user to enter hours worked for current employee
  69. hours[i] = getHours(clockNumber[i]);
  70.  
  71. // Determine overtime hours based on hours worked
  72. overtimeHrs[i] = calcOvertime(hours[i]);
  73.  
  74. // Calculate total gross pay including overtime
  75. grossPay[i] = calcGross(hours[i], wageRate[i], overtimeHrs[i]);
  76.  
  77. // Update running totals for final payroll summary
  78. totalWage += wageRate[i];
  79. totalHours += hours[i];
  80. totalOT += overtimeHrs[i];
  81. totalGross += grossPay[i];
  82. }
  83.  
  84. // Print payroll report header
  85. printHeader();
  86.  
  87. // ----------------------------------------------------------
  88. // Second loop: display payroll information for each employee
  89. // ----------------------------------------------------------
  90. for (i = 0; i < SIZE; ++i)
  91. {
  92. printEmp(clockNumber[i], wageRate[i], hours[i],
  93. overtimeHrs[i], grossPay[i]);
  94. }
  95.  
  96. // Display totals and averages for all employees
  97. printTotals(totalWage, totalHours, totalOT, totalGross);
  98.  
  99. return 0; // indicate successful program execution
  100. }
  101.  
  102.  
  103. /********************************************************************
  104. Function: getHours
  105. Purpose: Prompts the user to enter the number of hours worked
  106.   by a specific employee and returns the entered value.
  107.  
  108. Parameters:
  109.   clockNumber - unique identification number for the employee
  110.  
  111. Returns:
  112.   float - number of hours worked entered by the user
  113. ********************************************************************/
  114. float getHours (long int clockNumber)
  115. {
  116. float hoursWorked;
  117. // variable used to store the number of hours entered by the user
  118.  
  119. // Ask user to input hours worked for the employee
  120. printf("\n Hours worked by emp # %06li: ", clockNumber);
  121.  
  122. // Read the user input from the keyboard
  123. scanf ("%f", &hoursWorked);
  124.  
  125. // Return the entered value to the calling function
  126. return hoursWorked;
  127. }
  128.  
  129.  
  130. /********************************************************************
  131. Function: calcOvertime
  132. Purpose: Determines how many overtime hours an employee worked
  133.   based on the standard work week limit.
  134.  
  135. Parameters:
  136.   hours - total hours worked by the employee
  137.  
  138. Returns:
  139.   float - number of overtime hours worked
  140. ********************************************************************/
  141. float calcOvertime (float hours)
  142. {
  143. float overtime;
  144. // variable storing the calculated overtime hours
  145.  
  146. // Check whether hours exceed the standard 40-hour work week
  147. if (hours > STD_WORK_WEEK)
  148. overtime = hours - STD_WORK_WEEK; // extra hours are overtime
  149. else
  150. overtime = 0.0f; // no overtime if ≤ 40 hours
  151.  
  152. // Return calculated overtime hours
  153. return overtime;
  154. }
  155.  
  156.  
  157. /********************************************************************
  158. Function: calcGross
  159. Purpose: Calculates the total gross pay for an employee including
  160.   both regular pay and overtime pay.
  161.  
  162. Parameters:
  163.   hours - total hours worked
  164.   wageRate - hourly pay rate for the employee
  165.   overtimeHrs - number of overtime hours worked
  166.  
  167. Returns:
  168.   float - total gross pay for the employee
  169. ********************************************************************/
  170. float calcGross (float hours, float wageRate, float overtimeHrs)
  171. {
  172. float regularPay;
  173. // pay earned from regular (non-overtime) hours
  174.  
  175. float overtimePay;
  176. // pay earned from overtime hours
  177.  
  178. float gross;
  179. // total gross pay (regular pay + overtime pay)
  180.  
  181. // Calculate pay for regular hours only
  182. regularPay = (hours - overtimeHrs) * wageRate;
  183.  
  184. // Calculate pay for overtime hours using overtime multiplier
  185. overtimePay = overtimeHrs * wageRate * OVERTIME_RATE;
  186.  
  187. // Total gross pay equals regular pay plus overtime pay
  188. gross = regularPay + overtimePay;
  189.  
  190. // Return total calculated gross pay
  191. return gross;
  192. }
  193.  
  194.  
  195. /********************************************************************
  196. Function: printHeader
  197. Purpose: Displays the formatted header for the payroll report,
  198.   including the title and column labels.
  199.  
  200. Parameters:
  201.   None
  202.  
  203. Returns:
  204.   Nothing
  205. ********************************************************************/
  206. void printHeader (void)
  207. {
  208. // Print report title
  209. printf("\n\n*** Pay Calculator ***\n");
  210.  
  211. // Print table borders and column headings
  212. printf("\n----------------------------------------------------------\n");
  213. printf("Clock# Wage Hours OT Gross\n");
  214. printf("----------------------------------------------------------\n");
  215. }
  216.  
  217.  
  218. /********************************************************************
  219. Function: printEmp
  220. Purpose: Displays payroll information for a single employee in
  221.   formatted column layout.
  222.  
  223. Parameters:
  224.   clockNumber - employee identification number
  225.   wageRate - hourly wage rate
  226.   hours - total hours worked
  227.   overtimeHrs - overtime hours worked
  228.   grossPay - total calculated gross pay
  229.  
  230. Returns:
  231.   Nothing
  232. ********************************************************************/
  233. void printEmp (long int clockNumber, float wageRate, float hours,
  234. float overtimeHrs, float grossPay)
  235. {
  236. // Display employee payroll data in aligned columns
  237. printf("%06li %5.2f %5.1f %5.1f %8.2f\n",
  238. clockNumber, wageRate, hours, overtimeHrs, grossPay);
  239. }
  240.  
  241.  
  242. /********************************************************************
  243. Function: printTotals
  244. Purpose: Prints total and average payroll statistics for all
  245.   employees in the report.
  246.  
  247. Parameters:
  248.   totalWage - sum of all employee wage rates
  249.   totalHours - total hours worked by all employees
  250.   totalOT - total overtime hours worked
  251.   totalGross - total gross payroll amount
  252.  
  253. Returns:
  254.   Nothing
  255. ********************************************************************/
  256. void printTotals(float totalWage, float totalHours,
  257. float totalOT, float totalGross)
  258. {
  259. // Print separator line for totals section
  260. printf("----------------------------------------------------------\n");
  261.  
  262. // Display total values
  263. printf("Total %5.2f %5.1f %5.1f %8.2f\n",
  264. totalWage, totalHours, totalOT, totalGross);
  265.  
  266. // Display averages by dividing totals by number of employees
  267. printf("Average %5.2f %5.1f %5.1f %8.2f\n",
  268. totalWage / SIZE,
  269. totalHours / SIZE,
  270. totalOT / SIZE,
  271. totalGross / SIZE);
  272. }
Success #stdin #stdout 0s 5304KB
stdin
Standard input is empty
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     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
----------------------------------------------------------
Total    51.45     0.0     0.0       0.00
Average  10.29     0.0     0.0       0.00