fork download
  1. //********************************************************
  2. //
  3. // Assignment 6 - Structures
  4. //
  5. // Name: Cooper Lefevra
  6. //
  7. // Class: C Programming, Spring 2025
  8. //
  9. // Date: 03/08/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 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 calcOT (float hours);
  50. float calcGross(float wageRate, float hours, float overtimeHrs);
  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. employeeData[i].overtimeHrs = calcOT(employeeData[i].hours); //calculate OT hours
  74.  
  75. employeeData[i].grossPay = calcGross(employeeData[i].wageRate, employeeData[i].hours,
  76. employeeData[i].overtimeHrs); //calculate gross pay
  77.  
  78.  
  79.  
  80. } // for
  81.  
  82. // Print the column headers
  83. void printHeader();
  84.  
  85. // print out each employee
  86. for (i = 0; i < SIZE; ++i)
  87. {
  88. printEmp (employeeData[i].clockNumber,
  89. employeeData[i].wageRate,
  90. employeeData[i].hours,
  91. employeeData[i].overtimeHrs,
  92. employeeData[i].grossPay);
  93. }
  94.  
  95. return(0); // success
  96.  
  97. } // main
  98.  
  99. //**************************************************************
  100. // Function: getHours
  101. //
  102. // Purpose: Obtains input from user, the number of hours worked
  103. // per employee and stores the result in a local variable
  104. // that is passed back to the calling function.
  105. //
  106. // Parameters: clockNumber - The unique employee ID
  107. //
  108. // Returns: hoursWorked - hours worked in a given week
  109. //
  110. //**************************************************************
  111.  
  112. float getHours (long int clockNumber)
  113. {
  114.  
  115. float hoursWorked; // hours worked in a given week
  116.  
  117. // Read in hours for employee
  118. printf("\nEnter hours worked by emp # %06li: ", clockNumber);
  119. scanf ("%f", &hoursWorked);
  120.  
  121. // return hours back to the calling function
  122. return (hoursWorked);
  123. } // getHours
  124.  
  125. //**************************************************************
  126. // Function: printHeader
  127. //
  128. // Purpose: Prints the initial table header information.
  129. //
  130. // Parameters: none
  131. //
  132. // Returns: void
  133. //
  134. //**************************************************************
  135. 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", clockNumber, wageRate, hours,
  171. overtimeHrs, grossPay);
  172.  
  173. } // printEmp
  174.  
  175. // TODO: Add your functions here
  176.  
  177. // ****************************************************************
  178. // Function calcOT
  179. //
  180. // Purpose: Calculate any overtime hours by scanning for any hours over 40
  181. // anything over 40 will get minused by 40 to determine overtime hrs
  182. //
  183. // Parameters: Hours - total hours worked
  184. // STD_HOURS - value of 40 to baseline normal pay hours
  185. //
  186. // Returns : overtimeHrs
  187. // ***************************************************************
  188.  
  189. float calcOT (float hours)
  190. {
  191. float overtimeHrs; //declare variable
  192.  
  193. if (hours > STD_HOURS) // initialize with if statement
  194. {
  195. overtimeHrs = hours - STD_HOURS; //calculate OT hours
  196. }
  197. return (overtimeHrs);
  198. }//calcOT
  199.  
  200. // ****************************************************************
  201. // Function calcGross
  202. //
  203. // Purpose : Calculate gross pay of the employees using wage rate hours worked, overtime hours
  204. //
  205. // Parameters : wagerate - the rate in $ per hour that employees make
  206. // hours - total hours employees worked
  207. // overtimeHrs - Hours in which wage rate will be calculated at 1.5x
  208. //
  209. // Returns - grossPay
  210. // ****************************************************************
  211.  
  212. float calcGross (float wageRate, float hours, float overtimeHrs) // function definition and parameters
  213. {
  214. float grossPay;
  215. float normalPay;
  216. float overtimePay;
  217. // variable declaration from parameters
  218.  
  219. if (hours > STD_HOURS) // if hours greater than 40 calculate for OT hrs
  220. {
  221. overtimeHrs = hours - STD_HOURS;
  222. overtimePay = overtimeHrs * (OT_RATE * wageRate);
  223. normalPay = STD_HOURS * wageRate;
  224. grossPay = normalPay + overtimePay;
  225. }
  226. else // no OT hours
  227. {
  228. overtimeHrs = 0;
  229. normalPay = hours * wageRate;
  230. grossPay = normalPay;
  231.  
  232. }
  233. return (grossPay);
  234. }//calcGross
  235.  
Success #stdin #stdout 0.01s 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: 
 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