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