fork download
  1. //*******************************************************
  2. //
  3. // Homework: Assignment 4 (Chapter 7)
  4. //
  5. // Name: Brandon Shea
  6. //
  7. // Class: C Programming, Spring 2019
  8. //
  9. // Date: 2/20/19
  10. //
  11. // Description: Program which determines gross pay and outputs
  12. // be sent to a designated file. It will add any overtime pay,
  13. // if necessary, to the gross pay. This version uses arrays
  14. // and does not use file pointers.
  15. //
  16. //********************************************************
  17.  
  18. #include <stdio.h>
  19.  
  20. // constants to be used
  21.  
  22. #define NUM_EMPL 5
  23. #define OVERTIME_RATE 1.5f
  24. #define STD_WORK_WEEK 40.0f
  25.  
  26. // function prototypes
  27.  
  28. float getHours (long clockNumber);
  29. float getOvertimeHours(float hours);
  30. float getOvertimePay(float overtime, float wage);
  31. float getGross(float hours, float overtimePay, float wage);
  32. void printData (long int clockNumber[], float wageRate[], float hours[],
  33. float overtime[], float gross[], int size);
  34.  
  35. // main function
  36.  
  37. int main()
  38.  
  39. {
  40.  
  41. // Variable Declarations
  42.  
  43. long int clockNumber[NUM_EMPL] = {98401,526488,765349,34645,127615}; // employee clock number
  44. float gross[NUM_EMPL]; // gross pay for a given week
  45. float hours[NUM_EMPL]; // hours worked in a given week
  46. int i; // loop and array index
  47. float overtime[NUM_EMPL]; // number of overtime hours worked in a given week
  48. float overtimePay[NUM_EMPL]; // overtime pay for a given week
  49. float wage[NUM_EMPL] = {10.60,9.75,10.50,12.25,8.35}; // hourly wage
  50.  
  51. // process each employee
  52.  
  53. for (i = 0; i < NUM_EMPL; ++i)
  54.  
  55. {
  56.  
  57. // Function call to get hours worked input from user
  58. hours[i] = getHours (clockNumber[i]);
  59.  
  60. // Function call to calculate overtime hours
  61. overtime[i] = getOvertimeHours (hours[i]);
  62.  
  63. //function call to calculate overtime pay
  64. overtimePay[i] = getOvertimePay (overtime[i], wage[i]);
  65.  
  66. // Function call to calculate gross pay
  67. gross[i] = getGross (hours[i], overtimePay[i], wage[i]);
  68.  
  69. }
  70.  
  71. // Print the header info
  72.  
  73. printf ("\n\n\tTim Niesen, C Programming, Fourth Homework Assignment");
  74. printf("\n\t-------------------------------------------------------\n");
  75. printf("\tClock# Wage Hours OT Gross\n");
  76. printf("\t-------------------------------------------------------\n");
  77.  
  78. // Print all the employees - call by reference
  79.  
  80. printData (clockNumber, wage, hours,
  81. overtime, gross, NUM_EMPL);
  82.  
  83. return (0);
  84.  
  85. } // end main
  86.  
  87. //**************************************************************
  88. // Function: getHours
  89. //
  90. // Purpose: Obtains input from user, the number of hours worked
  91. // per employee and stores the result in a local variable
  92. // that is passed back to the calling function.
  93. //
  94. // Parameters: clockNumber - The clock number of the employee
  95. //
  96. // Returns: hoursWorked - hours worked by the employee in a given week
  97. //
  98. //**************************************************************
  99.  
  100. float getHours (long int clockNumber)
  101.  
  102. {
  103.  
  104. float hoursWorked; // hours worked in a given week
  105.  
  106. // Get Hours for each employee
  107.  
  108. printf("\nEnter the number of hours the employee worked for employee #: %06li: ", clockNumber);
  109. scanf ("%f", &hoursWorked);
  110.  
  111. return (hoursWorked);
  112.  
  113. } // end function
  114.  
  115. //**************************************************************
  116. // Function: getOvertimeHours
  117. //
  118. // Purpose: Calculates whether or not an employee worked
  119. // overtime and how many hours of overtime they worked.
  120. // Stores the result in a local variable that is passed back to
  121. // the calling function.
  122. //
  123. // Parameters: hours - hours worked by employee in a given week
  124. //
  125. // Returns: otHours - overtime hours worked in a given week
  126. //
  127. //**************************************************************
  128.  
  129. float getOvertimeHours (float hours)
  130.  
  131. {
  132.  
  133. float otHours; // overtime hours worked in a given week
  134.  
  135. // if else statement to determine if overtime hours were worked
  136.  
  137. if (hours >= STD_WORK_WEEK)
  138.  
  139. {
  140.  
  141. otHours = hours - STD_WORK_WEEK;
  142.  
  143. }
  144.  
  145. else
  146.  
  147. {
  148.  
  149. otHours = 0;
  150.  
  151. }
  152.  
  153. return (otHours);
  154.  
  155. } // end function
  156.  
  157. //**************************************************************
  158. // Function: getOvertimePay
  159. //
  160. // Purpose: Calculates how much an employee should be paid at
  161. // the overtime rate. Stores the result in a local variable that
  162. // is passed back to the calling function.
  163. //
  164. // Parameters:
  165. //
  166. // overtime - number of overtime hours worked in a given week
  167. // wage - hourly wage
  168. //
  169. // Returns: otPay - overtime pay for a given week
  170. //
  171. //**************************************************************
  172.  
  173. float getOvertimePay (float overtime, float wage)
  174.  
  175. {
  176.  
  177. float otPay; // overtime pay for a given week
  178.  
  179. // if else statement to determine overtime pay, if applicable
  180.  
  181. if (overtime > 0)
  182.  
  183. {
  184.  
  185. otPay = overtime * (OVERTIME_RATE * wage);
  186.  
  187. }
  188.  
  189. else
  190.  
  191. {
  192.  
  193. otPay = 0;
  194.  
  195. }
  196.  
  197. return (otPay);
  198.  
  199. } // end function
  200.  
  201. //**************************************************************
  202. // Function: getGross
  203. //
  204. // Purpose: Calculates what the gross pay for an employee will
  205. // be for a given week. Stores the result in a local variable
  206. // that is passed back to the calling function.
  207. //
  208. // Parameters:
  209. //
  210. // hours - hours worked in a given week
  211. // overtimePay - overtime pay for week
  212. // wage - hourly wage
  213. //
  214. // Returns: grossPay - gross pay for a given week
  215. //
  216. //**************************************************************
  217.  
  218. float getGross(float hours, float overtimePay, float wage)
  219.  
  220. {
  221.  
  222. float grossPay; // gross pay for a given week
  223.  
  224. // if else statement to determine how to calculate gross pay
  225.  
  226. if (overtimePay == 0)
  227.  
  228. {
  229.  
  230. grossPay = hours * wage;
  231.  
  232. }
  233.  
  234. else
  235.  
  236. {
  237.  
  238. grossPay = STD_WORK_WEEK * wage + overtimePay;
  239.  
  240. }
  241.  
  242. return (grossPay);
  243.  
  244. } // end function
  245.  
  246. //**************************************************************
  247. // Function: printData
  248. //
  249. // Purpose: Prints out all the employee information in a
  250. // nice and orderly table format.
  251. //
  252. // Parameters:
  253. //
  254. // clockNumber - Array of employee clock numbers
  255. // wageRate - Array of employee wages per hour
  256. // hrs - Array of number of hours worked by an employee
  257. // overtime - Array of overtime hours for each employee
  258. // gross - Array of gross pay calculations for each employee
  259. // size - Number of employees to process
  260. //
  261. // Returns: Nothing (call by reference)
  262. //
  263. //**************************************************************
  264.  
  265. void printData (long int clockNumber[], float wageRate[], float hours[],
  266. float overtime[], float gross[], int size)
  267.  
  268. {
  269.  
  270. for (int i = 0; i < NUM_EMPL; i++)
  271.  
  272. {
  273.  
  274. // print employee information from arrays
  275.  
  276. printf("\t%06i %10.2f %10.1f %10.1f %10.2f\n", clockNumber[i], wageRate[i], hours[i], overtime[i], gross[i]);
  277.  
  278. }
  279.  
  280. return;
  281.  
  282. } // end function
Success #stdin #stdout 0s 15232KB
stdin
51.0 
42.5 
37.0 
45.0 
0.00
stdout
Enter the number of hours the employee worked for employee #: 098401: 
Enter the number of hours the employee worked for employee #: 526488: 
Enter the number of hours the employee worked for employee #: 765349: 
Enter the number of hours the employee worked for employee #: 034645: 
Enter the number of hours the employee worked for employee #: 127615: 

	Tim Niesen, C Programming, Fourth Homework Assignment
	-------------------------------------------------------
	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