fork(1) download
  1. //********************************************************
  2. //
  3. // Assignment 5 - Functions
  4. //
  5. // Name: Guido Grossmann
  6. //
  7. // Class: C Programming, Summer 2025
  8. //
  9. // Date: 2025-06-28
  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. // Functions called by a combination of by value and by
  16. // reference.
  17. //
  18. //********************************************************
  19.  
  20. #include <stdio.h>
  21.  
  22. // constants
  23. #define SIZE 5
  24. #define OVERTIME_RATE 1.5f
  25. #define STD_WORK_WEEK 40.0f
  26.  
  27. // function prototypes
  28. float getHours (long int clockNumber);
  29. void printHeader (void);
  30. void printEmp (long int clockNumber[], float wageRate[], float hours[],
  31. float overtimeHrs[], float grossPay[], int theSize);
  32. float calcOT (float hours);
  33. float calcGross (float hours, float overtimeHrs, float wageRate);
  34.  
  35.  
  36.  
  37. int main()
  38. {
  39.  
  40. // Variable Declarations
  41.  
  42. long int clockNumber[SIZE] = {98401,526488,765349,34645,127615}; // ID
  43. float grossPay[SIZE]; // gross pay
  44. float hours[SIZE]; // hours worked in a given week
  45. int i; // loop and array index
  46. float overtimeHrs[SIZE]; // overtime hours
  47. float wageRate[SIZE] = {10.60,9.75,10.50,12.25,8.35}; // hourly wage rate
  48.  
  49. // process each employee
  50. for (i = 0; i < SIZE; ++i)
  51. {
  52.  
  53. // read in hours for the current employee
  54. hours[i] = getHours (clockNumber[i]);
  55.  
  56. // TODO: Function call to calculate overtime hours
  57. overtimeHrs[i] = calcOT (hours[i]);
  58.  
  59. // TODO: Function call to calculate gross pay
  60. grossPay[i] = calcGross (hours[i], overtimeHrs[i], wageRate[i]);
  61.  
  62. }
  63.  
  64. // Print the header info
  65. printHeader();
  66.  
  67. // Print all the employees - call by reference
  68. printEmp (clockNumber, wageRate, hours,
  69. overtimeHrs, grossPay, SIZE);
  70.  
  71. return (0);
  72.  
  73. } // main
  74.  
  75. //**************************************************************
  76. // Function: getHours
  77. //
  78. // Purpose: Obtains input from user, the number of hours worked
  79. // per employee and stores the result in a local variable
  80. // that is passed back to the calling function.
  81. //
  82. // Parameters: clockNumber - The unique employee ID
  83. //
  84. // Returns: hoursWorked - hours worked in a given week
  85. //
  86. //**************************************************************
  87.  
  88. float getHours (long int clockNumber)
  89. {
  90.  
  91. float hoursWorked; // hours worked in a given week
  92.  
  93. // Read in hours for employee
  94. printf("\nEnter hours worked by emp # %06li: ", clockNumber);
  95. scanf ("%f", &hoursWorked);
  96.  
  97. // return hours back to the calling function
  98. return (hoursWorked);
  99.  
  100. } // getHours
  101.  
  102. //**************************************************************
  103. // Function: calcOT
  104. //
  105. // Purpose: Calculates overtime hours using the user input and constant and
  106. // stores it in an array
  107. //
  108. // Parameters: hours - the hours worked in a week
  109. //
  110. // Returns: weeklyOT - overtime hours worked in a given week
  111. //
  112. //**************************************************************
  113.  
  114. float calcOT (float hours)
  115. {
  116. float weeklyOT; // variable to store OT calculation
  117.  
  118. //calculate OT
  119. if (hours >= STD_WORK_WEEK)
  120. {
  121. weeklyOT = hours - STD_WORK_WEEK;
  122. }
  123. else
  124. {
  125. weeklyOT = 0;
  126. }
  127. //return result
  128. return (weeklyOT);
  129.  
  130. } //calcOT
  131.  
  132. //**************************************************************
  133. // Function: calcGross
  134. //
  135. // Purpose: Calculates gross pay using wageRate, hours and overtimeHrs and stores
  136. // the result in a array.
  137. //
  138. // Parameters: hours, overtimeHrs, wageRate - the hours and overtime hours worked in a week
  139. // the coresponding wage rates
  140. //
  141. // Returns: grossCalc - total pay due for a given week
  142. //
  143. //**************************************************************
  144.  
  145. float calcGross (float hours, float overtimeHrs, float wageRate)
  146. {
  147. float grossCalc; // variable to store gross pay calculation
  148.  
  149. //calculate gross pay
  150. grossCalc = ((hours-overtimeHrs) * wageRate) + (overtimeHrs * wageRate * OVERTIME_RATE);
  151.  
  152. //return result
  153. return (grossCalc);
  154.  
  155. } //calcGross
  156.  
  157. //**************************************************************
  158. // Function: printHeader
  159. //
  160. // Purpose: Prints the initial table header information.
  161. //
  162. // Parameters: none
  163. //
  164. // Returns: void
  165. //
  166. //**************************************************************
  167.  
  168. void printHeader (void)
  169. {
  170.  
  171. printf ("\n\n*** Pay Calculator ***\n");
  172.  
  173. // print the table header
  174. printf("\n\nClock# Wage Hours OT Gross\n");
  175. printf("-----------------------------------\n");
  176.  
  177. } // printHeader
  178.  
  179. //*************************************************************
  180. // Function: printEmp
  181. //
  182. // Purpose: Prints out all the employee information in a
  183. // nice and orderly table format.
  184. //
  185. // Parameters:
  186. //
  187. // clockNumber - Array of employee clock numbers
  188. // wageRate - Array of employee wages per hour
  189. // hours - Array of number of hours worked by an employee
  190. // overtimeHrs - Array of overtime hours for each employee
  191. // grossPay - Array of gross pay calculations for each employee
  192. // theSize - Number of employees to process
  193. //
  194. // Returns: Nothing (call by reference)
  195. //
  196. //**************************************************************
  197.  
  198. void printEmp (long int clockNumber[], float wageRate[], float hours[],
  199. float overtimeHrs[], float grossPay[], int theSize)
  200. {
  201.  
  202. int i; // loop index
  203.  
  204. // access and print each employee
  205. for (i = 0; i < theSize; ++i)
  206. {
  207. printf("%06d %7.2f %5.1f %5.1f %8.2f\n",
  208. clockNumber[i], wageRate[i], hours[i], overtimeHrs[i], grossPay[i]);
  209. } // end of loop
  210. } //print EMP
  211.  
  212.  
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
-----------------------------------
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