fork download
  1. //********************************************************
  2. //
  3. // Assignment 5 - Functions
  4. //
  5. // Name: Fisher Brown
  6. //
  7. // Class: C Programming, Spring 2025
  8. //
  9. // Date: March 2, 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 calcOvertime(float hoursWorked);
  29. float calcGross(float wageRate, float hoursWorked, float overtimeHours);
  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.  
  38. long int clockNumber[SIZE] = {98401, 526488, 765349, 34645, 127615}; // array of employee IDs
  39. float wageRate[SIZE] = {10.60, 9.75, 10.50, 12.25, 8.35}; // array of employee wage rates
  40. float hours[SIZE]; // array of hours worked for each employee
  41. float overtimeHrs[SIZE]; // array of overtime hours for each employee
  42. float grossPay[SIZE]; // array of gross pay for each employee
  43. int i; // loop counter
  44.  
  45. // process each employee
  46. for (i = 0; i < SIZE; ++i)
  47. {
  48. // Read in hours for employee
  49. hours[i] = getHours(clockNumber[i]);
  50.  
  51. // Calculate overtime hours
  52. overtimeHrs[i] = calcOvertime(hours[i]);
  53.  
  54. // Calculate gross pay
  55. grossPay[i] = calcGross(wageRate[i], hours[i], overtimeHrs[i]);
  56. }
  57.  
  58. // print the header info
  59. printHeader();
  60.  
  61. // print out each employee's information
  62. for (i = 0; i < SIZE; ++i)
  63. {
  64. printEmp(clockNumber[i], wageRate[i], hours[i],
  65. overtimeHrs[i], grossPay[i]);
  66. }
  67.  
  68. return (0);
  69.  
  70. } // main
  71.  
  72. //**************************************************************
  73. // Function: getHours
  74. //
  75. // Purpose: Obtains input from user, the number of hours worked
  76. // per employee and stores the result in a local variable
  77. // that is passed back to the calling function.
  78. //
  79. // Parameters: clockNumber - The unique employee ID
  80. //
  81. // Returns: hoursWorked - hours worked in a given week
  82. //
  83. //**************************************************************
  84.  
  85. float getHours(long int clockNumber)
  86. {
  87. float hoursWorked; // local variable to store hours worked in a week
  88.  
  89. // Prompt and read hours worked for the employee
  90. printf("\nEnter hours worked by emp # %06li: ", clockNumber);
  91. scanf("%f", &hoursWorked);
  92.  
  93. return hoursWorked;
  94. } // getHours
  95.  
  96. //**************************************************************
  97. // Function: calcOvertime
  98. //
  99. // Purpose: Calculates the overtime hours worked.
  100. //
  101. // Parameters:
  102. // hoursWorked - total hours worked by employee
  103. //
  104. // Returns: overtimeHours - hours over 40 worked
  105. //**************************************************************
  106. float calcOvertime(float hoursWorked)
  107. {
  108. float overtimeHours; // local variable to store calculated overtime hours
  109.  
  110. if (hoursWorked > STD_WORK_WEEK)
  111. overtimeHours = hoursWorked - STD_WORK_WEEK;
  112. else
  113. overtimeHours = 0.0f;
  114.  
  115. return overtimeHours;
  116. } // calcOvertime
  117.  
  118. //**************************************************************
  119. // Function: calcGross
  120. //
  121. // Purpose: Calculates the gross pay for an employee.
  122. //
  123. // Parameters:
  124. // wageRate - hourly wage
  125. // hoursWorked - total hours worked
  126. // overtimeHours - hours over 40 worked
  127. //
  128. // Returns: grossPay - total pay for the week
  129. //**************************************************************
  130. float calcGross(float wageRate, float hoursWorked, float overtimeHours)
  131. {
  132. float regularHours; // local variable to store regular (non-overtime) hours
  133. float grossPay; // local variable to store calculated gross pay
  134.  
  135. regularHours = hoursWorked - overtimeHours;
  136. grossPay = (regularHours * wageRate) + (overtimeHours * wageRate * OVERTIME_RATE);
  137.  
  138. return grossPay;
  139. } // calcGross
  140.  
  141. //**************************************************************
  142. // Function: printHeader
  143. //
  144. // Purpose: Prints the initial table header information.
  145. //
  146. // Parameters: none
  147. //
  148. // Returns: void
  149. //
  150. //**************************************************************
  151.  
  152. void printHeader(void)
  153. {
  154. printf("\n\n*** Pay Calculator ***\n");
  155.  
  156. // print the table header
  157. printf("\nClock# Wage Hours OT Gross\n");
  158. printf("------------------------------------------------\n");
  159. } // printHeader
  160.  
  161. //*************************************************************
  162. // Function: printEmp
  163. //
  164. // Purpose: Prints out all the information for an employee
  165. // in a nice and orderly table format.
  166. //
  167. // Parameters:
  168. //
  169. // clockNumber - unique employee ID
  170. // wageRate - hourly wage rate
  171. // hours - Hours worked for the week
  172. // overtimeHrs - overtime hours worked in a week
  173. // grossPay - gross pay for the week
  174. //
  175. // Returns: void
  176. //
  177. //**************************************************************
  178.  
  179. void printEmp(long int clockNumber, float wageRate, float hours,
  180. float overtimeHrs, float grossPay)
  181. {
  182. // print the employee's information
  183. printf("%06li %6.2f %6.1f %6.1f %9.2f\n",
  184. clockNumber, wageRate, hours, overtimeHrs, grossPay);
  185. } // printEmp
  186.  
Success #stdin #stdout 0.01s 5284KB
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