fork download
  1. //********************************************************
  2. //
  3. // Assignment 5 - Functions
  4. //
  5. // Name: <replace with your name>
  6. //
  7. // Class: C Programming, <replace with Semester and Year>
  8. //
  9. // Date: <replace with the current date>
  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. float calculateOvertimeHours(float hours);
  34. float calculateGrossPay(float wageRate, float hours, float overtimeHrs);
  35.  
  36.  
  37. // TODO: Add other function prototypes here as needed
  38.  
  39. int main()
  40. {
  41.  
  42. // Variable Declarations
  43.  
  44. long int clockNumber[SIZE] = {98401,526488,765349,34645,127615}; // ID
  45. float grossPay[SIZE]; // gross pay
  46. float hours[SIZE]; // hours worked in a given week
  47. int i; // loop and array index
  48. float overtimeHrs[SIZE]; // overtime hours
  49. float wageRate[SIZE] = {10.60,9.75,10.50,12.25,8.35}; // hourly wage rate
  50.  
  51. // process each employee
  52. for (i = 0; i < SIZE; ++i)
  53. {
  54.  
  55. // read in hours for the current employee
  56. hours[i] = getHours (clockNumber[i]);
  57.  
  58. overtimeHrs[i] = calculateOvertimeHours(hours[i]);
  59.  
  60. // TODO: Function call to calculate overtime hours
  61.  
  62. grossPay[i] = calculateGrossPay(wageRate[i], hours[i], overtimeHrs[i]);
  63.  
  64. // TODO: Function call to calculate gross pay
  65.  
  66. }
  67.  
  68. // Print the header info
  69. printHeader();
  70.  
  71. // Print all the employees - call by reference
  72. printEmp (clockNumber, wageRate, hours,
  73. overtimeHrs, grossPay, SIZE);
  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 emp # %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("\nClock# Wage Hours OT Gross\n");
  124. printf("------------------------------------------------\n");
  125.  
  126. } // printHeader
  127.  
  128. //*************************************************************
  129. // Function: printEmp
  130. //
  131. // Purpose: Prints out all the employee information in a
  132. // nice and orderly table format.
  133. //
  134. // Parameters:
  135. //
  136. // clockNumber - Array of employee clock numbers
  137. // wageRate - Array of employee wages per hour
  138. // hours - Array of number of hours worked by an employee
  139. // overtimeHrs - Array of overtime hours for each employee
  140. // grossPay - Array of gross pay calculations for each employee
  141. // theSize - Number of employees to process
  142. //
  143. // Returns: Nothing (call by reference)
  144. //
  145. //**************************************************************
  146.  
  147. void printEmp (long int clockNumber[], float wageRate[], float hours[],
  148. float overtimeHrs[], float grossPay[], int theSize)
  149. {
  150.  
  151. int i; // loop index
  152.  
  153. // access and print each employee
  154. for (i = 0; i < theSize; ++i)
  155. {
  156. printf("%06d %5.2f %5.1f %5.1f %8.2f\n",
  157. clockNumber[i], wageRate[i], hours[i], overtimeHrs[i], grossPay[i]);
  158. // TODO: add code to print out each employee one at a time
  159. }
  160. }
  161.  
  162.  
  163.  
  164. float calculateOvertimeHours(float hours){
  165. float overtimeHrs;
  166. if (hours > STD_WORK_WEEK) {
  167. overtimeHrs = hours - STD_WORK_WEEK;
  168. } else {
  169. overtimeHrs = 0;
  170. }
  171. return overtimeHrs;
  172.  
  173. }
  174.  
  175. float calculateGrossPay(float wageRate, float hours, float overtimeHrs){
  176. float grossPay;
  177. float overtimePay;
  178. float normalPay;
  179.  
  180. overtimePay = overtimeHrs * wageRate * OVERTIME_RATE;
  181. normalPay = (hours - overtimeHrs) * wageRate;
  182. grossPay = normalPay + overtimePay;
  183. return grossPay;
  184.  
  185.  
  186. }
  187.  
  188.  
  189.  
  190. // TODO: Add other functions here as needed
  191. // ... remember your comment block headers for each function
Success #stdin #stdout 0.01s 5308KB
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