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

--------------------------------------------------------------------------
    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