fork download
  1. //********************************************************
  2. //
  3. // Assignment 5 - Functions
  4. //
  5. // Name: Morgan Card-Gimpelman
  6. //
  7. // Class: C Programming, Spring 2025
  8. //
  9. // Date: 3/2/25
  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. float calcOT (float hours);
  32. float calcGross (float wageRate,
  33. float hours,
  34. float overtimeHrs);
  35.  
  36. int main()
  37. {
  38.  
  39. // Variable Declarations
  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. // Function call to calculate overtime hours
  55. overtimeHrs[i] = calcOT (hours[i]);
  56.  
  57. // Function call to calculate gross pay
  58. grossPay[i] = calcGross (wageRate[i], hours[i], overtimeHrs[i]);
  59.  
  60. }
  61.  
  62. // print the header info
  63. printHeader();
  64.  
  65. // print out each employee
  66. for (i = 0; i < SIZE; ++i)
  67. {
  68.  
  69. // Print all the employees - call by value
  70. printEmp (clockNumber[i], wageRate[i], hours[i],
  71. overtimeHrs[i], grossPay[i]);
  72.  
  73. }
  74.  
  75. return (0);
  76.  
  77. } // main
  78.  
  79. //**************************************************************
  80. // Function: getHours
  81. //
  82. // Purpose: Obtains input from user, the number of hours worked
  83. // per employee and stores the result in a local variable
  84. // that is passed back to the calling function.
  85. //
  86. // Parameters: clockNumber - The unique employee ID
  87. //
  88. // Returns: hoursWorked - hours worked in a given week
  89. //
  90. //**************************************************************
  91.  
  92. float getHours (long int clockNumber)
  93. {
  94.  
  95. float hoursWorked; // hours worked in a given week
  96.  
  97. // Read in hours for employee
  98. printf("\nEnter hours worked by employee # %06li: ", clockNumber);
  99. scanf ("%f", &hoursWorked);
  100.  
  101. // return hours back to the calling function
  102. return (hoursWorked);
  103.  
  104. } // getHours
  105.  
  106. //**************************************************************
  107. // Function: printHeader
  108. //
  109. // Purpose: Prints the initial table header information.
  110. //
  111. // Parameters: none
  112. //
  113. // Returns: void
  114. //
  115. //**************************************************************
  116.  
  117. void printHeader (void)
  118. {
  119.  
  120. printf ("\n\n*** Pay Calculator ***\n");
  121.  
  122. // print the table header
  123. printf("\n Clock# Wage Hours OT Gross\n");
  124. printf("------------------------------------------------\n");
  125.  
  126. } // printHeader
  127.  
  128. //*************************************************************
  129. // Function: printEmp
  130. //
  131. // Purpose: Prints out all the information for an employee
  132. // in a nice and orderly table format.
  133. //
  134. // Parameters:
  135. //
  136. // clockNumber - unique employee ID
  137. // wageRate - hourly wage rate
  138. // hours - hours worked for the week
  139. // overtimeHrs - overtime hours worked in a week
  140. // grossPay - gross pay for the week
  141. //
  142. // Returns: void
  143. //
  144. //**************************************************************
  145.  
  146. void printEmp (long int clockNumber, float wageRate, float hours,
  147. float overtimeHrs, float grossPay)
  148. {
  149.  
  150. printf ("\t%06ld %5.2f %5.1f %5.1f %7.2f\n",
  151. clockNumber, wageRate, hours, overtimeHrs, grossPay);
  152.  
  153. } //PrintEmp
  154.  
  155. //**************************************************************
  156. // Function: calcOT
  157. //
  158. // Purpose: Calculates the number of overtime hours an
  159. //employee works.
  160. //
  161. // Parameters: hours - hours worked for the week
  162. //
  163. // Returns: overtimeHrs - overtime hours worked in a week
  164. //
  165. //**************************************************************
  166.  
  167. float calcOT (float hours)
  168. {
  169. //Declare local variables
  170. float overtimeHrs; //Overtime hours
  171.  
  172. if (hours >= STD_WORK_WEEK) //Hours over forty
  173. {
  174. overtimeHrs = hours - STD_WORK_WEEK; //Overtime hours calculation
  175. }
  176.  
  177. else //Hours under forty
  178. {
  179. overtimeHrs = 0;
  180. }
  181.  
  182. //return overtime hours back to calling function
  183. return (overtimeHrs);
  184.  
  185. } //calcOT
  186.  
  187. //**************************************************************
  188. // Function: calcGross
  189. //
  190. // Purpose: Calculates the gross pay of an employee
  191. //
  192. // Parameters:
  193. //
  194. // hours - hours worked for the week
  195. // wageRate - hourly wage rate
  196. // overtimeHrs - overtime hours worked in a week
  197. //
  198. // Returns: grossPay
  199. //
  200. //**************************************************************
  201.  
  202. float calcGross (float wageRate, float hours, float overtimeHrs)
  203. {
  204. //Declare local variables
  205. float grossPay; //Total pay of an employee
  206. float normalPay; //Normal pay of an employee without overtime
  207. float overtimePay; //Overtime pay for a given week
  208.  
  209. if (hours >= STD_WORK_WEEK) //Hours over forty
  210. {
  211. normalPay = STD_WORK_WEEK * wageRate; //Normal pay calculation
  212. overtimePay = overtimeHrs * (wageRate * OVERTIME_RATE); //Overtime pay calculation
  213. grossPay = normalPay + overtimePay; //Gross pay calculation over forty hours
  214. }
  215.  
  216. else //Hours under forty
  217. {
  218. overtimePay = 0;
  219. grossPay = hours * wageRate; //Gross pay calculation under forty hours
  220. }
  221.  
  222. //return gross pay to calling function
  223. return(grossPay);
  224.  
  225. } //calcGross
  226.  
Success #stdin #stdout 0.01s 5268KB
stdin
51.0
42.5
37.0
45.0
0.0
stdout
Enter hours worked by employee # 098401: 
Enter hours worked by employee # 526488: 
Enter hours worked by employee # 765349: 
Enter hours worked by employee # 034645: 
Enter hours worked by employee # 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