fork download
  1. //********************************************************
  2. //
  3. // Assignment 5 - Functions
  4. //
  5. // Name: K. Asha Vigilante-Pini
  6. //
  7. // Class: C Programming, <replace with Semester and Year>
  8. //
  9. // Date: 29/06/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. // Functions called by a combination of by value and by
  16. // reference.
  17. //
  18. //********************************************************
  19.  
  20. #include <stdio.h>
  21.  
  22. // constants
  23. #define SIZE 5
  24. #define OVERTIME_RATE 1.5f
  25. #define STD_WORK_WEEK 40.0f
  26.  
  27. // function prototypes
  28. float getHours (long int clockNumber);
  29. void printHeader (void);
  30. void printEmp (long int clockNumber[], float wageRate[], float hours[],
  31. float overtimeHrs[], float grossPay[], int theSize);
  32.  
  33. // TODO: Add other function prototypes here as needed
  34. float calOvertimeHrs (float hours);
  35. float calGrossPay (float hours, float overtimeHrs, float wageRate);
  36.  
  37. int main()
  38. {
  39.  
  40. // Variable Declarations
  41.  
  42. long int clockNumber[SIZE] = {98401,526488,765349,34645,127615}; // ID
  43. float grossPay[SIZE]; // gross pay
  44. float hours[SIZE]; // hours worked in a given week
  45. int i; // loop and array index
  46. float overtimeHrs[SIZE]; // overtime hours
  47. float wageRate[SIZE] = {10.60,9.75,10.50,12.25,8.35}; // hourly wage rate
  48.  
  49. // process each employee
  50. for (i = 0; i < SIZE; ++i)
  51. {
  52.  
  53. // read in hours for the current employee
  54. hours[i] = getHours (clockNumber[i]);
  55.  
  56. // TODO: Function call to calculate overtime hours
  57. overtimeHrs[i] = calOvertimeHrs(hours[i]);
  58.  
  59. // TODO: Function call to calculate gross pay
  60. grossPay[i] = calGrossPay(hours[i], overtimeHrs[i], wageRate[i]);
  61.  
  62. }
  63.  
  64. // Print the header info
  65. printHeader();
  66.  
  67. // Print all the employees - call by reference
  68. printEmp (clockNumber, wageRate, hours,
  69. overtimeHrs, grossPay, SIZE);
  70.  
  71. return (0);
  72.  
  73. } // main
  74.  
  75. //**************************************************************
  76. // Function: getHours
  77. //
  78. // Purpose: Obtains input from user, the number of hours worked
  79. // per employee and stores the result in a local variable
  80. // that is passed back to the calling function.
  81. //
  82. // Parameters: clockNumber - The unique employee ID
  83. //
  84. // Returns: hoursWorked - hours worked in a given week
  85. //
  86. //**************************************************************
  87.  
  88. float getHours (long int clockNumber)
  89. {
  90.  
  91. float hoursWorked; // hours worked in a given week
  92.  
  93. // Read in hours for employee
  94. printf("\nEnter hours worked by emp # %06li: ", clockNumber);
  95. scanf ("%f", &hoursWorked);
  96.  
  97. // return hours back to the calling function
  98. return (hoursWorked);
  99.  
  100. } // getHours
  101.  
  102. //**************************************************************
  103. // Function: printHeader
  104. //
  105. // Purpose: Prints the initial table header information.
  106. //
  107. // Parameters: none
  108. //
  109. // Returns: void
  110. //
  111. //**************************************************************
  112.  
  113. void printHeader (void)
  114. {
  115.  
  116. printf ("\n\n*** Pay Calculator ***\n");
  117.  
  118. // print the table header
  119. printf("\nClock# Wage Hours OT Gross\n");
  120. printf("------------------------------------------------\n");
  121.  
  122. } // printHeader
  123.  
  124. //*************************************************************
  125. // Function: printEmp
  126. //
  127. // Purpose: Prints out all the employee information in a
  128. // nice and orderly table format.
  129. //
  130. // Parameters:
  131. //
  132. // clockNumber - Array of employee clock numbers
  133. // wageRate - Array of employee wages per hour
  134. // hours - Array of number of hours worked by an employee
  135. // overtimeHrs - Array of overtime hours for each employee
  136. // grossPay - Array of gross pay calculations for each employee
  137. // theSize - Number of employees to process
  138. //
  139. // Returns: Nothing (call by reference)
  140. //
  141. //**************************************************************
  142.  
  143. void printEmp (long int clockNumber[], float wageRate[], float hours[],
  144. float overtimeHrs[], float grossPay[], int theSize)
  145. {
  146.  
  147. int i; // loop index
  148.  
  149. // access and print each employee
  150. for (i = 0; i < theSize; ++i)
  151. {
  152. // TODO: add code to print out each employee one at a time
  153.  
  154. printf("%06ld %5.2f %5.1f %6.1f %7.2f\n",
  155. clockNumber[i], wageRate[i], hours[i], overtimeHrs[i], grossPay[i]);
  156. }
  157. }
  158.  
  159. // TODO: Add other functions here as needed
  160. // ... remember your comment block headers for each function
  161.  
  162. //**************************************************************
  163. // Function: calOvertimeHrs
  164. //
  165. // Purpose: Calculate Overtime Hours for Each Employee
  166. //
  167. //**************************************************************
  168.  
  169. float calOvertimeHrs(float hours)
  170. {
  171. float overTime;
  172. if (hours > STD_WORK_WEEK) {
  173. overTime = hours - STD_WORK_WEEK;
  174. } else
  175. {
  176. overTime = 0.0f;
  177. }
  178. return (overTime);
  179. }
  180.  
  181. //**************************************************************
  182. // Function: calGrossPay
  183. //
  184. // Purpose: Calculate Gross Pay for Each Employee
  185. //
  186. //**************************************************************
  187.  
  188. float calGrossPay (float hours, float overtimeHrs, float wageRate)
  189. {
  190. float normalHours;
  191. if (hours > STD_WORK_WEEK)
  192. {
  193. normalHours = STD_WORK_WEEK;
  194. }
  195. else
  196. {
  197. normalHours = hours;
  198. }
  199.  
  200. float normalPay = normalHours * wageRate;
  201. float overtimePay = overtimeHrs * wageRate * OVERTIME_RATE;
  202. float grossPay = normalPay + overtimePay;
  203.  
  204. return (grossPay);
  205. }
Success #stdin #stdout 0s 5316KB
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.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