fork download
  1. //********************************************************
  2. //
  3. // Assignment 5 - Functions
  4. //
  5. // Name: Marin Haxhiaj
  6. //
  7. // Class: C Programming, Spring 2025
  8. //
  9. // Date: 03/03/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.5f
  24. #define STD_WORK_WEEK 40.0f
  25.  
  26. // function prototypes
  27. float getHours(long int clockNumber);
  28. float calculateOvertime(float hours);
  29. float calculateGrossPay(float wageRate, float hours, float overtimeHrs);
  30. void printHeader(void);
  31. void printEmp(long int clockNumber, float wageRate, float hours,
  32. float overtimeHrs, float grossPay);
  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. // Calculate overtime hours
  51. overtimeHrs[i] = calculateOvertime(hours[i]);
  52.  
  53. // 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. }
  67.  
  68. return 0;
  69. }
  70.  
  71. //**************************************************************
  72. // Function: getHours
  73. //
  74. // Purpose: Obtains input from user, the number of hours worked
  75. // per employee and stores the result in a local variable
  76. // that is passed back to the calling function.
  77. //
  78. // Parameters: clockNumber - The unique employee ID
  79. //
  80. // Returns: hoursWorked - hours worked in a given week
  81. //
  82. //**************************************************************
  83. float getHours(long int clockNumber)
  84. {
  85. float hoursWorked; // hours worked in a given week
  86.  
  87. // Read in hours for employee
  88. printf("\nEnter hours worked by emp # %06li: ", clockNumber);
  89. scanf("%f", &hoursWorked);
  90.  
  91. // return hours back to the calling function
  92. return hoursWorked;
  93. }
  94.  
  95. //**************************************************************
  96. // Function: calculateOvertime
  97. //
  98. // Purpose: Calculates the overtime hours worked by an employee.
  99. //
  100. // Parameters: hours - Total hours worked in a week
  101. //
  102. // Returns: overtimeHrs - Overtime hours worked
  103. //
  104. //**************************************************************
  105. float calculateOvertime(float hours)
  106. {
  107. float overtimeHrs = 0.0f;
  108.  
  109. // Calculate overtime hours if hours worked exceed the standard work week
  110. if (hours > STD_WORK_WEEK)
  111. {
  112. overtimeHrs = hours - STD_WORK_WEEK;
  113. }
  114.  
  115. return overtimeHrs;
  116. }
  117.  
  118. //**************************************************************
  119. // Function: calculateGrossPay
  120. //
  121. // Purpose: Calculates the gross pay for an employee, including
  122. // overtime pay if applicable.
  123. //
  124. // Parameters:
  125. // wageRate - Hourly wage rate
  126. // hours - Total hours worked in a week
  127. // overtimeHrs - Overtime hours worked
  128. //
  129. // Returns: grossPay - Gross pay for the week
  130. //
  131. //**************************************************************
  132. float calculateGrossPay(float wageRate, float hours, float overtimeHrs)
  133. {
  134. float grossPay;
  135.  
  136. // Calculate gross pay with overtime if applicable
  137. if (overtimeHrs > 0)
  138. {
  139. grossPay = (STD_WORK_WEEK * wageRate) + (overtimeHrs * wageRate * OVERTIME_RATE);
  140. }
  141. else
  142. {
  143. grossPay = hours * wageRate;
  144. }
  145.  
  146. return grossPay;
  147. }
  148.  
  149. //**************************************************************
  150. // Function: printHeader
  151. //
  152. // Purpose: Prints the initial table header information.
  153. //
  154. // Parameters: none
  155. //
  156. // Returns: void
  157. //
  158. //**************************************************************
  159. void printHeader(void)
  160. {
  161. printf("\n\n*** Pay Calculator ***\n");
  162.  
  163. // print the table header
  164. printf("\nClock# Wage Hours OT Gross\n");
  165. printf("------------------------------------------------\n");
  166. }
  167.  
  168. //*************************************************************
  169. // Function: printEmp
  170. //
  171. // Purpose: Prints out all the information for an employee
  172. // in a nice and orderly table format.
  173. //
  174. // Parameters:
  175. // clockNumber - unique employee ID
  176. // wageRate - hourly wage rate
  177. // hours - Hours worked for the week
  178. // overtimeHrs - overtime hours worked in a week
  179. // grossPay - gross pay for the week
  180. //
  181. // Returns: void
  182. //
  183. //**************************************************************
  184. void printEmp(long int clockNumber, float wageRate, float hours,
  185. float overtimeHrs, float grossPay)
  186. {
  187. // print the employee details
  188. printf("%06li %5.2f %6.2f %6.2f %8.2f\n",
  189. clockNumber, wageRate, hours, overtimeHrs, grossPay);
  190. }
Success #stdin #stdout 0s 5244KB
stdin
51.0
42.5
37.0
45.0
0.0
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  51.00  11.00   598.90
526488  9.75  42.50   2.50   426.56
765349 10.50  37.00   0.00   388.50
034645 12.25  45.00   5.00   581.88
127615  8.35   0.00   0.00     0.00