fork download
  1. //********************************************************
  2. //
  3. // Assignment 5 - Functions
  4. //
  5. // Name: Rose Samedi
  6. //
  7. // Class: C Programming, Fall 2025
  8. //
  9. // Date: 10/20/2025
  10. //
  11. // Description: Program which determines overtime and
  12. // gross pay for a set of employees with outputs sent
  13. // to standard output (the screen).
  14. //
  15. // All functions are called by value
  16. //
  17. //********************************************************
  18.  
  19. #include <stdio.h>
  20.  
  21. // constants
  22. #define SIZE 5
  23. #define OVERTIME_RATE 1.5
  24. #define STD_WORK_WEEK 40.0
  25.  
  26. // function prototypes
  27. float getHours (long int clockNumber);
  28. void printHeader (void);
  29. void printEmp (long int clockNumber, float wageRate, float hours,
  30. float overtimeHrs, float grossPay);
  31. float calculateOvertime(float hours);
  32. float calculateGrossPay(float wageRate, float hours, float overtimeHrs);
  33.  
  34. int main()
  35. {
  36. /* Variable Declarations */
  37. long int clockNumber[SIZE] = {98401,526488,765349,34645,127615}; // ID
  38. float grossPay[SIZE]; // gross pay
  39. float hours[SIZE]; // hours worked in a given week
  40. int i; // loop and array index
  41. float overtimeHrs[SIZE]; // overtime hours
  42. float wageRate[SIZE] = {10.60,9.75,10.50,12.25,8.35}; // hourly wage rate
  43.  
  44. // process each employee
  45. for (i = 0; i < SIZE; ++i)
  46. {
  47. // Read in hours for employee
  48. hours[i] = getHours (clockNumber[i]);
  49.  
  50. // Function call to calculate overtime hours
  51. overtimeHrs[i] = calculateOvertime(hours[i]);
  52.  
  53. // Function call to calculate gross pay
  54. grossPay[i] = calculateGrossPay(wageRate[i], hours[i], overtimeHrs[i]);
  55. }
  56.  
  57. // print the header info
  58. printHeader();
  59.  
  60. // print out each employee
  61. for (i = 0; i < SIZE; ++i)
  62. {
  63. // Print all the employees - call by value
  64. printEmp (clockNumber[i], wageRate[i], hours[i],
  65. overtimeHrs[i], grossPay[i]);
  66. } // for
  67.  
  68. return (0);
  69. } // main
  70.  
  71. //**************************************************************
  72. // Function: getHours
  73. //
  74. // Purpose: Obtains input from user, the number of hours worked
  75. // per employee and stores the result
  76. //**************************************************************
  77. float getHours (long int clockNumber)
  78. {
  79. float hours;
  80. printf("Enter hours worked for employee %06ld: ", clockNumber);
  81. scanf("%f", &hours);
  82. return hours;
  83. }
  84.  
  85. //**************************************************************
  86. // Function: printHeader
  87. //
  88. // Purpose: Prints the header for the employee payroll table
  89. //**************************************************************
  90. void printHeader (void)
  91. {
  92. printf("\n*** Pay Calculator ***\n\n");
  93. printf("Clock #\t| Wage\t| Hours\t| OT\t| Gross\n");
  94. printf("--------|-------|-------|-------|--------\n");
  95. }
  96.  
  97. //**************************************************************
  98. // Function: printEmp
  99. //
  100. // Purpose: Prints a single line of employee information
  101. //**************************************************************
  102. void printEmp (long int clockNumber, float wageRate, float hours,
  103. float overtimeHrs, float grossPay)
  104. {
  105. printf("%06ld\t| %5.2f\t|%5.1f\t|%5.1f\t|%7.2f\n",
  106. clockNumber, wageRate, hours, overtimeHrs, grossPay);
  107. }
  108.  
  109. //**************************************************************
  110. // Function: calculateOvertime
  111. //
  112. // Purpose: Calculates overtime hours based on standard work week
  113. //**************************************************************
  114. float calculateOvertime(float hours)
  115. {
  116. if (hours > STD_WORK_WEEK)
  117. {
  118. return hours - STD_WORK_WEEK;
  119. }
  120. else
  121. {
  122. return 0.0f;
  123. }
  124. }
  125.  
  126. //**************************************************************
  127. // Function: calculateGrossPay
  128. //
  129. // Purpose: Calculates gross pay including overtime wages
  130. //**************************************************************
  131. float calculateGrossPay(float wageRate, float hours, float overtimeHrs)
  132. {
  133. float regularPay;
  134. float overtimePay;
  135.  
  136. if (overtimeHrs > 0)
  137. {
  138. regularPay = STD_WORK_WEEK * wageRate;
  139. overtimePay = overtimeHrs * (wageRate * OVERTIME_RATE);
  140. return regularPay + overtimePay;
  141. }
  142. else
  143. {
  144. return hours * wageRate;
  145. }
  146. }
Success #stdin #stdout 0.01s 5316KB
stdin
51.0
42.5
37.0
45.0
0.0
stdout
Enter hours worked for employee 098401: Enter hours worked for employee 526488: Enter hours worked for employee 765349: Enter hours worked for employee 034645: Enter hours worked for employee 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