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