fork download
  1. //********************************************************
  2. //
  3. // Assignment 6 - Structures
  4. //
  5. // Name: Brian Fallon
  6. //
  7. // Class: C Programming, Spring 2025
  8. //
  9. // Date: March 4, 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; // added for computation of gross pay
  41. float overtimePay; // added for computation of gross pay
  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.  
  76. // Overtime Hours Call
  77. employeeData[i].overtimeHrs = calcOT(employeeData[i].hoursTotal);
  78. // Gross Pay Call
  79. employeeData[i].grossPay = calcGross(employeeData[i].wageRate, employeeData[i].hoursTotal,
  80. employeeData[i].overtimeHrs);
  81.  
  82. } // End for
  83.  
  84. // Print the column headers
  85. printHeader();
  86.  
  87. // Print out each employee
  88. for (i = 0; i < SIZE; ++i)
  89. {
  90. printEmp (employeeData[i].clockNumber,
  91. employeeData[i].wageRate,
  92. employeeData[i].hoursTotal,
  93. employeeData[i].overtimeHrs,
  94. employeeData[i].grossPay);
  95. }
  96.  
  97. return(0); // success
  98.  
  99. } // End main
  100.  
  101. //**************************************************************
  102. // Function: getHours
  103. //
  104. // Purpose: Obtains input from user, the number of hours worked
  105. // per employee and stores the result in a local variable
  106. // that is passed back to the calling function.
  107. //
  108. // Parameters: clockNumber - The unique employee ID
  109. //
  110. // Returns: hoursWorked - hours worked in a given week
  111. //
  112. //**************************************************************
  113.  
  114. float getHours (long int clockNumber)
  115. {
  116.  
  117. float hoursWorked; // hours worked in a given week
  118.  
  119. // Read in hours for employee
  120. printf("\nEnter hours worked by emp # %06li: ", clockNumber);
  121. scanf ("%f", &hoursWorked);
  122.  
  123. // Return hours back to the calling function
  124. return (hoursWorked);
  125.  
  126. } // getHours
  127.  
  128. //**************************************************************
  129. // Function: printHeader
  130. //
  131. // Purpose: Prints the initial table header information.
  132. //
  133. // Parameters: none
  134. //
  135. // Returns: void
  136. //
  137. //**************************************************************
  138.  
  139. void printHeader (void)
  140. {
  141.  
  142. printf ("\n\n*** Pay Calculator ***\n");
  143.  
  144. // Print the table header
  145. printf("\nClock# Wage Hours OT Gross\n");
  146. printf("------------------------------------------------\n");
  147.  
  148. } // printHeader
  149.  
  150. //*************************************************************
  151. // Function: printEmp
  152. //
  153. // Purpose: Prints out all the information for an employee
  154. // in a nice and orderly table format.
  155. //
  156. // Parameters:
  157. //
  158. // clockNumber - unique employee ID
  159. // wageRate - hourly wage rate
  160. // hours - Hours worked for the week
  161. // overtimeHrs - overtime hours worked in a week
  162. // grossPay - gross pay for the week
  163. //
  164. // Returns: void
  165. //
  166. //**************************************************************
  167.  
  168. void printEmp (long int clockNumber, float wageRate, float hours,
  169. float overtimeHrs, float grossPay)
  170. {
  171.  
  172. // Print out a single employee
  173. printf("\n %06li %5.2f %4.1f %4.1f %8.2f",
  174. clockNumber, wageRate, hours,
  175. overtimeHrs, grossPay);
  176.  
  177. } // printEmp
  178.  
  179. // TODO: Add your functions here
  180. //*************************************************************
  181. // Function: calcOT
  182. //
  183. // Purpose: Calculate the overtime hours of employees if logged
  184. // more than the standard week
  185. //
  186. // Parameters:
  187. // hoursTotal- total logged hours per employee
  188. // STD_HOURS - the standard amount of 40 hours per week
  189. //
  190. // Returns: overtimeHrs
  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.  
  206. } // calcOT
  207.  
  208.  
  209. //*************************************************************
  210. // Function: calcGross
  211. //
  212. // Purpose: Calculate the Gross Pay of employees considering
  213. // any overtime hours logged
  214. //
  215. // Parameters:
  216. // wageRate - hourly wage of employees
  217. // hoursTotal- the total hours logged for employees
  218. // overtimeHrs - any overtime hours for any applicable employee logs
  219. //
  220. // Returns: grossPay
  221. //
  222. //**************************************************************
  223.  
  224. float calcGross (float wageRate, float hoursTotal, float overtimeHrs) // Function Definition
  225. {
  226. //local variable
  227. float grossPay;
  228. float normalPay;
  229. float overtimePay;
  230. //computation
  231. if (hoursTotal > STD_HOURS)
  232. {
  233. overtimeHrs = hoursTotal - STD_HOURS;
  234. overtimePay = overtimeHrs * (OT_RATE*wageRate);
  235. normalPay = STD_HOURS*wageRate;
  236. grossPay = normalPay + overtimePay;
  237. } // End if
  238.  
  239. else // No Overtime
  240. {
  241. overtimeHrs=0;
  242. normalPay=wageRate*hoursTotal;
  243. grossPay=normalPay;
  244. } // End else
  245. //Return computation
  246. return (grossPay);
  247.  
  248. } // calcGross
  249. //**************************************************************
Success #stdin #stdout 0s 5284KB
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