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