fork download
  1. //********************************************************
  2. //
  3. // Assignment 5 - Functions
  4. //
  5. // Name: Larson Klipic
  6. //
  7. // Class: C Programming, SUMMER 26
  8. //
  9. // Date: JULY 5th
  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. // Call by Value design
  16. //
  17. //********************************************************
  18.  
  19.  
  20. // Define and Includes
  21.  
  22. #include <stdio.h>
  23.  
  24. // Define Constants
  25. #define SIZE 5
  26. #define STD_HOURS 40.0
  27. #define OT_RATE 1.5
  28.  
  29. // structure for employee data
  30. struct employee
  31. {
  32. long int clockNumber;
  33. float wageRate;
  34. float hours;
  35. float overtimeHrs;
  36. float grossPay;
  37. };
  38.  
  39. // prototypes
  40. float getHours (long int clockNumber);
  41. void printHeader (void);
  42. void printEmp (long int clockNumber, float wageRate, float hours,
  43. float overtimeHrs, float grossPay);
  44. float OTwork (float hours);
  45. float pay (float wageRate, float hours, float overtimeHrs);
  46.  
  47. int main ()
  48. {
  49. // Set up a local variable to store the employee information
  50. struct employee employeeData[SIZE] = {
  51. { 98401, 10.60 },
  52. { 526488, 9.75 },
  53. { 765349, 10.50 }, // initialize clock and wage values
  54. { 34645, 12.25 },
  55. { 127615, 8.35 }
  56. };
  57.  
  58. int i; // loop and array index
  59.  
  60. // Call functions as needed to read and calculate information
  61. for (i = 0; i < SIZE; ++i)
  62. {
  63.  
  64. // Prompt for the number of hours worked by the employee
  65. employeeData[i].hours = getHours (employeeData[i].clockNumber);
  66.  
  67. // calculate overtime hopurs for a employee
  68. employeeData[i].overtimeHrs = OTwork(employeeData[i].hours);
  69.  
  70. // calculate gross pay for a employee
  71. employeeData[i].grossPay = pay(employeeData[i].wageRate,
  72. employeeData[i].hours,
  73. employeeData[i].overtimeHrs);
  74.  
  75. } // for
  76.  
  77. // Print the column headers
  78. printHeader();
  79.  
  80. // print out each employee
  81. for (i = 0; i < SIZE; ++i)
  82. {
  83. printEmp (employeeData[i].clockNumber,
  84. employeeData[i].wageRate,
  85. employeeData[i].hours,
  86. employeeData[i].overtimeHrs,
  87. employeeData[i].grossPay);
  88. }
  89.  
  90. return(0); // success
  91.  
  92. } // main
  93.  
  94. //**************************************************************
  95. // Function: getHours
  96. //
  97. // Purpose: Obtains input from user, the number of hours worked
  98. // per employee and stores the result in a local variable
  99. // that is passed back to the calling function.
  100. //
  101. // Parameters: clockNumber - The unique employee ID
  102. //
  103. // Returns: hoursWorked - hours worked in a given week
  104. //
  105. //**************************************************************
  106.  
  107. float getHours (long int clockNumber)
  108. {
  109.  
  110. float hoursWorked; // hours worked in a given week
  111.  
  112. // Read in hours for employee
  113. printf("\nEnter hours worked by emp # %06li: ", clockNumber);
  114. scanf ("%f", &hoursWorked);
  115.  
  116. // return hours back to the calling function
  117. return (hoursWorked);
  118.  
  119. } // getHours
  120.  
  121. //**************************************************************
  122. // Function: printHeader
  123. //
  124. // Purpose: Prints the initial table header information.
  125. //
  126. // Parameters: none
  127. //
  128. // Returns: void
  129. //
  130. //**************************************************************
  131.  
  132. void printHeader (void)
  133. {
  134.  
  135. printf ("\n\n*** Pay Calculator ***\n");
  136.  
  137. // print the table header
  138. printf("\nClock# Wage Hours OT Gross\n");
  139. printf("------------------------------------------------\n");
  140.  
  141. } // printHeader
  142.  
  143.  
  144. //*************************************************************
  145. // Function: printEmp
  146. //
  147. // Purpose: Prints out all the information for an employee
  148. // in a nice and orderly table format.
  149. //
  150. // Parameters:
  151. //
  152. // clockNumber - unique employee ID
  153. // wageRate - hourly wage rate
  154. // hours - Hours worked for the week
  155. // overtimeHrs - overtime hours worked in a week
  156. // grossPay - gross pay for the week
  157. //
  158. // Returns: void
  159. //
  160. //**************************************************************
  161.  
  162. void printEmp (long int clockNumber, float wageRate, float hours,
  163. float overtimeHrs, float grossPay)
  164. {
  165.  
  166. // Print out a single employee
  167. printf("\n %06li %5.2f %4.1f %4.1f %8.2f",
  168. clockNumber, wageRate, hours,
  169. overtimeHrs, grossPay);
  170.  
  171. } // printEmp
  172.  
  173.  
  174. //**************************************************************
  175. // Function: OTwork
  176. //
  177. // Purpose: calculate the overtime for an employee.
  178. //
  179. // Parameters: hours - an array of time the employee worked
  180. //
  181. // Returns: hrsWork - overtime hours
  182. //
  183. //**************************************************************
  184.  
  185. float OTwork (float hours)
  186. {
  187. float hrsWork; // hours worked
  188.  
  189. // overtime
  190. if (hours > STD_HOURS )
  191. {
  192. hrsWork = hours - STD_HOURS;
  193. }
  194. else // no OT
  195. {
  196. hrsWork = 0.0;
  197. }
  198.  
  199. return(hrsWork);
  200. } // OTwork
  201.  
  202.  
  203. //**************************************************************
  204. // Function: pay
  205. //
  206. // Purpose: calculate grosspay for an employee.
  207. //
  208. // Parameters: wageRate - hourly wage of a employee
  209. // hours - time worked from a employee
  210. // overtimeHrs - overtime hours of a empoyee
  211. //
  212. // Returns: gross - calculated grosspay of the employee
  213. //
  214. //**************************************************************
  215.  
  216. float pay (float wageRate, float hours, float overtimeHrs)
  217. {
  218. float gross; // gross pay
  219. float normalPay; // pay without overtime
  220. float overtimePay; // only overtime pay
  221.  
  222. // calculate gross with normal pay and overtime pay
  223. if (overtimeHrs > 0)
  224. {
  225. // overtime
  226. normalPay = wageRate * STD_HOURS;
  227. overtimePay = overtimeHrs * (wageRate * OT_RATE);
  228. gross = normalPay + overtimePay;
  229. }
  230. else
  231. {
  232. // No overtime
  233. gross = hours * wageRate;
  234. }
  235.  
  236. return (gross);
  237. } // pay
Success #stdin #stdout 0s 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