fork download
  1. //********************************************************
  2. //
  3. // Assignment 5 - Functions
  4. //
  5. // Name: Carlos Dominguez
  6. //
  7. // Class: C Programming, Spring 2026
  8. //
  9. // Date: 03/01/2026
  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);
  31.  
  32. // --- Added helper prototypes ---
  33. float calcOvertime(float hours);
  34. float calcGrossPay(float wageRate, float hours);
  35.  
  36. int main(void)
  37. {
  38. /* Variable Declarations */
  39. long int clockNumber[SIZE] = {98401, 526488, 765349, 34645, 127615}; // ID
  40. float grossPay[SIZE]; // gross pay
  41. float hours[SIZE]; // hours worked in a given week
  42. float overtimeHrs[SIZE]; // overtime hours
  43. float wageRate[SIZE] = {10.60f, 9.75f, 10.50f, 12.25f, 8.35f}; // hourly wage rate
  44. int i; // loop and array index
  45.  
  46. // process each employee
  47. for (i = 0; i < SIZE; ++i)
  48. {
  49. // Read in hours for employee
  50. hours[i] = getHours(clockNumber[i]);
  51.  
  52. // Calculate overtime hours
  53. overtimeHrs[i] = calcOvertime(hours[i]);
  54.  
  55. // Calculate gross pay
  56. grossPay[i] = calcGrossPay(wageRate[i], hours[i]);
  57. }
  58.  
  59. // print the header info
  60. printHeader();
  61.  
  62. // print out each employee
  63. for (i = 0; i < SIZE; ++i)
  64. {
  65. // Print all the employees - call by value
  66. printEmp(clockNumber[i], wageRate[i], hours[i],
  67. overtimeHrs[i], grossPay[i]);
  68. }
  69.  
  70. return 0;
  71. } // main
  72.  
  73. //**************************************************************
  74. // Function: getHours
  75. //
  76. // Purpose: Obtains input from user, the number of hours worked
  77. // per employee and stores the result in a local variable
  78. // that is passed back to the calling function.
  79. //
  80. // Parameters: clockNumber - The unique employee ID
  81. //
  82. // Returns: hoursWorked - hours worked in a given week
  83. //
  84. //**************************************************************
  85.  
  86. float getHours (long int clockNumber)
  87. {
  88. float hoursWorked = 0.0f; // hours worked in a given week
  89.  
  90. // Read in hours for employee
  91. printf("\nEnter hours worked by employee # %06li: ", clockNumber);
  92. scanf("%f", &hoursWorked);
  93.  
  94. // return hours back to the calling function
  95. return hoursWorked;
  96. } // getHours
  97.  
  98. //**************************************************************
  99. // Function: printHeader
  100. //
  101. // Purpose: Prints the initial table header information.
  102. //
  103. // Parameters: none
  104. //
  105. // Returns: void
  106. //
  107. //**************************************************************
  108.  
  109. void printHeader (void)
  110. {
  111. printf("\n\n*** Pay Calculator ***\n");
  112. printf("\nClock# Wage Hours OT Gross\n");
  113. printf("-----------------------------------------------\n");
  114. } // printHeader
  115.  
  116. //*************************************************************
  117. // Function: printEmp
  118. //
  119. // Purpose: Prints out all the information for an employee
  120. // in a nice and orderly table format.
  121. //
  122. // Parameters:
  123. // clockNumber - unique employee ID
  124. // wageRate - hourly wage rate
  125. // hours - hours worked for the week
  126. // overtimeHrs - overtime hours worked in a week
  127. // grossPay - gross pay for the week
  128. //
  129. // Returns: void
  130. //
  131. //**************************************************************
  132.  
  133. void printEmp (long int clockNumber, float wageRate, float hours,
  134. float overtimeHrs, float grossPay)
  135. {
  136. // Print one row with neat alignment:
  137. // Clock# as 6 digits with leading zeros
  138. // Wage shown with 2 decimals
  139. // Hours and OT with 2 decimals
  140. // Gross with 2 decimals
  141. printf("%06li %6.2f %6.2f %6.2f %8.2f\n",
  142. clockNumber, wageRate, hours, overtimeHrs, grossPay);
  143. } // printEmp
  144.  
  145. //**************************************************************
  146. // Function: calcOvertime
  147. //
  148. // Purpose: Computes overtime hours (> 40 hours).
  149. //
  150. // Parameters:
  151. // hours - hours worked for the week
  152. //
  153. // Returns: overtime hours (0 if hours <= 40)
  154. //
  155. //**************************************************************
  156.  
  157. float calcOvertime(float hours)
  158. {
  159. if (hours > STD_WORK_WEEK)
  160. return hours - STD_WORK_WEEK;
  161. else
  162. return 0.0f;
  163. } // calcOvertime
  164.  
  165. //**************************************************************
  166. // Function: calcGrossPay
  167. //
  168. // Purpose: Computes gross pay given wage rate and hours,
  169. // applying time-and-a-half for hours beyond 40.
  170. //
  171. // Parameters:
  172. // wageRate - hourly wage rate
  173. // hours - hours worked for the week
  174. //
  175. // Returns: gross pay for the week
  176. //
  177. //**************************************************************
  178.  
  179. float calcGrossPay(float wageRate, float hours)
  180. {
  181. if (hours <= STD_WORK_WEEK)
  182. {
  183. return wageRate * hours;
  184. }
  185. else
  186. {
  187. float overtime = hours - STD_WORK_WEEK;
  188. float regularPay = wageRate * STD_WORK_WEEK;
  189. float overtimePay = wageRate * OVERTIME_RATE * overtime;
  190. return regularPay + overtimePay;
  191. }
  192. } // calcGrossPay
Success #stdin #stdout 0.01s 5292KB
stdin
51.0
42.5
37.0
45.0
0.0
stdout
Enter hours worked by employee # 098401: 
Enter hours worked by employee # 526488: 
Enter hours worked by employee # 765349: 
Enter hours worked by employee # 034645: 
Enter hours worked by employee # 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