fork download
  1. //********************************************************
  2. //
  3. // Assignment 5 - Functions
  4. //
  5. // Name: Elissa Huang
  6. //
  7. // Class: C Programming, Spring 2025
  8. //
  9. // Date: March 1, 2025
  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.  
  33. // TODO: Add other function prototypes here as needed
  34. void calcOT (float hours[], float overtimeHrs[], int theSize);
  35. void calcGross (float wageRate[], float hours[], float overtimeHrs[], float grossPay[], int theSize);
  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. calcOT (hours, overtimeHrs, SIZE);
  58.  
  59. // TODO: Function call to calculate gross pay
  60. calcGross (wageRate, hours, overtimeHrs, grossPay, SIZE);
  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: printHeader
  104. //
  105. // Purpose: Prints the initial table header information.
  106. //
  107. // Parameters: none
  108. //
  109. // Returns: void
  110. //
  111. //**************************************************************
  112.  
  113. void printHeader (void)
  114. {
  115.  
  116. printf ("\n\n*** Pay Calculator ***\n");
  117.  
  118. // print the table header
  119. printf("\nClock# Wage Hours OT Gross\n");
  120. printf("------------------------------------------------\n");
  121.  
  122. } // printHeader
  123.  
  124. //*************************************************************
  125. // Function: printEmp
  126. //
  127. // Purpose: Prints out all the employee information in a
  128. // nice and orderly table format.
  129. //
  130. // Parameters:
  131. //
  132. // clockNumber - Array of employee clock numbers
  133. // wageRate - Array of employee wages per hour
  134. // hours - Array of number of hours worked by an employee
  135. // overtimeHrs - Array of overtime hours for each employee
  136. // grossPay - Array of gross pay calculations for each employee
  137. // theSize - Number of employees to process
  138. //
  139. // Returns: Nothing (call by reference)
  140. //
  141. //**************************************************************
  142.  
  143. void printEmp (long int clockNumber[], float wageRate[], float hours[],
  144. float overtimeHrs[], float grossPay[], int theSize)
  145. {
  146.  
  147. int i; // loop index and array index
  148.  
  149. // access and print each employee
  150. for (i = 0; i < theSize; ++i)
  151. {
  152. printf("%06li %5.2f %5.1f %5.1f %8.2f\n",
  153. clockNumber[i], wageRate[i], hours[i], overtimeHrs[i], grossPay[i]);
  154. }
  155. }
  156.  
  157.  
  158. // TODO: Add other functions here as needed
  159. // ... remember your comment block headers for each function
  160.  
  161. //**************************************************************
  162. // Function: calcOT
  163. //
  164. // Purpose: Uses the number of hours worked per employee
  165. // to calculate each of their hours of overtime and stores the
  166. // the result in an array called overtimeHrs.
  167. //
  168. // Parameters: hours[], theSize
  169. //
  170. // Returns: void
  171. //
  172. //**************************************************************
  173. // Function definition for calcOT (Call by Reference)
  174. void calcOT (float hours[], float overtimeHrs[], int theSize)
  175. {
  176. // declare local variable(s)
  177. int i;
  178. for (i = 0; i < theSize; i++)
  179. {
  180. if (hours[i] > STD_WORK_WEEK) // OT
  181. {
  182. overtimeHrs[i] = hours[i] -STD_WORK_WEEK;
  183. }
  184. else // no OT
  185. {
  186. overtimeHrs[i] = 0.0;
  187. }
  188. }
  189.  
  190. // add in loop and code
  191. }
  192.  
  193. //**************************************************************
  194. // Function: calcGross
  195. //
  196. // Purpose: Uses the wage rate, hours, and overtime hours
  197. // worked by each employee to calculate each of their gross
  198. // pay and stores the result in an array called grossPay.
  199. //
  200. // Parameters: wageRate[], hours[], overtimeHrs[], grossPay[], theSize
  201. //
  202. // Returns: void
  203. //
  204. //**************************************************************
  205.  
  206. // Function definition for calcGross (Call by Reference)
  207. void calcGross (float wageRate[], float hours[], float overtimeHrs[], float grossPay[], int theSize)
  208. {
  209. int i;
  210. for (i = 0; i < theSize; i++)
  211. {
  212. if (overtimeHrs[i] > 0)
  213. {
  214. grossPay[i] = (overtimeHrs[i] * wageRate[i] * OVERTIME_RATE) + (STD_WORK_WEEK * wageRate[i]);
  215. }
  216. else
  217. {
  218. grossPay[i] = hours[i] * wageRate[i];
  219. }
  220. }
  221. }
Success #stdin #stdout 0s 5288KB
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