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