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