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