fork download
  1. //********************************************************
  2. //
  3. // Assignment 6 - Structures
  4. //
  5. // Name: Elissa Huang
  6. //
  7. // Class: C Programming, Spring 2025
  8. //
  9. // Date: March 9, 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 reference 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 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.  
  44. void getHours (struct employee employeeData[], int theSize );
  45. void printHeader (void);
  46.  
  47. void printEmp (struct employee emp [ ], int theSize);
  48. void calcOT (struct employee employeeData[], int theSize);
  49. void calcGross (struct employee employeeData[], int theSize);
  50. void printEmp (struct employee employeeData[], int theSize);
  51.  
  52. // TODO: add your function prototypes here
  53.  
  54. int main ()
  55. {
  56. // Set up a local variable and initialize the clock and wages of my employees
  57. struct employee employeeData [SIZE] = {
  58. { 98401, 10.60 },
  59. { 526488, 9.75 },
  60. { 765349, 10.50 },
  61. { 34645, 12.25 },
  62. { 127615, 8.35 }
  63. };
  64.  
  65. // Call function needed to read hours
  66. getHours (employeeData, SIZE);
  67.  
  68. // TODO: Call functions calculate ot hours and gross pay
  69. calcOT (employeeData, SIZE);
  70. calcGross (employeeData, SIZE);
  71.  
  72. // TODO: Call function to print the table column headers
  73.  
  74. // Print a table header
  75. printHeader();
  76.  
  77. // Function call to output results to the screen in table format
  78. printEmp (employeeData, SIZE);
  79.  
  80. return(0); // success
  81.  
  82. } // main
  83.  
  84. //**************************************************************
  85. // Function: getHours
  86. //
  87. // Purpose: Obtains input from user, the number of hours worked
  88. // per employee and stores the result in an array of structures
  89. // that is passed back to the calling function by reference.
  90. //
  91. // Parameters:
  92. //
  93. // employeeData - an array of structures containing Employees
  94. // theSize - number of employees to process
  95. //
  96. // Returns: Nothing (void)
  97. //
  98. //**************************************************************
  99.  
  100. void getHours (struct employee employeeData[], int theSize )
  101. {
  102.  
  103. int i; // loop and array index
  104.  
  105. // read hours in for each employee
  106. for (i = 0; i < theSize ; ++i)
  107. {
  108. printf("\nEnter hours worked by emp # %06li: ",
  109. employeeData[i].clockNumber);
  110. scanf ("%f", &employeeData[i].hours);
  111. } // for
  112.  
  113. } // getHours
  114.  
  115. //**************************************************************
  116. // Function: printHeader
  117. //
  118. // Purpose: Prints the initial table header information.
  119. //
  120. // Parameters: none
  121. //
  122. // Returns: void
  123. //
  124. //**************************************************************
  125.  
  126. void printHeader (void)
  127. {
  128.  
  129. printf ("\n\n*** Pay Calculator ***\n");
  130.  
  131. // print the table header
  132. printf("\nClock# Wage Hours OT Gross\n");
  133. printf("------------------------------------------------\n");
  134.  
  135. } // printHeader
  136.  
  137. // ********************************************************************
  138. // Function: printEmp
  139. //
  140. // Purpose: Outputs to screen in a table format the following
  141. // information about an employee: Clock, Wage,
  142. // Hours, Overtime Hours, and Gross Pay.
  143. //
  144. // Parameters:
  145. //
  146. // employeeData - an array of structures containing Employees
  147. // theSize - number of employees to process
  148. //
  149. // Returns: Nothing (void)
  150. //
  151. // *********************************************************************
  152.  
  153. void printEmp ( struct employee employeeData[], int theSize )
  154. {
  155. int i; // loop and array index
  156.  
  157. // print information about each employee
  158. for (i = 0; i < theSize ; ++i)
  159. {
  160. printf("\n %06li %5.2f %4.1f %4.1f %8.2f",
  161. employeeData[i].clockNumber, employeeData[i].wageRate, employeeData[i].hours,
  162. employeeData[i].overtimeHrs, employeeData[i].grossPay);
  163. } /* for */
  164.  
  165. } // printEmp
  166.  
  167. // TODO: add your functions here
  168.  
  169. // ********************************************************************
  170. // Function: calcOT
  171. //
  172. // Purpose: Uses the input (hours worked) to determined overtime hours
  173. //
  174. // Parameters:
  175. //
  176. // employeeData - an array of structures containing Employees
  177. // theSize - number of employees to process
  178. //
  179. // Returns: Nothing (void)
  180. //
  181. // *********************************************************************
  182. void calcOT (struct employee employeeData[], int theSize)
  183. {
  184. // declare local variable(s)
  185. int i; // loop and array index
  186.  
  187. // add a loop and process each employee
  188. for (i = 0 ; i < theSize; ++i)
  189. {
  190. if (employeeData[i].hours > STD_HOURS)
  191. {
  192. employeeData[i].overtimeHrs = employeeData[i].hours - STD_HOURS;
  193. }
  194. else
  195. {
  196. employeeData[i].overtimeHrs = 0;
  197. }
  198. }
  199.  
  200. }
  201.  
  202. // ********************************************************************
  203. // Function: calcGross
  204. //
  205. // Purpose: Uses the input (wage rate, hours worked, and ot hours)
  206. // to determine the gross pay
  207. //
  208. // Parameters:
  209. //
  210. // employeeData - an array of structures containing Employees
  211. // theSize - number of employees to process
  212. //
  213. // Returns: Nothing (void)
  214. //
  215. // *********************************************************************
  216. void calcGross (struct employee employeeData[], int theSize)
  217. {
  218. // declare local variable(s)
  219. int i; // loop and array index
  220.  
  221. // add a loop and process each employee
  222. for (i = 0 ; i < theSize; ++i)
  223. {
  224. if (employeeData[i].overtimeHrs > 0)
  225. {
  226. employeeData[i].grossPay = (employeeData[i].overtimeHrs * employeeData[i].wageRate * OT_RATE) + (STD_HOURS * employeeData[i].wageRate);
  227. }
  228. else
  229. {
  230. employeeData[i].grossPay = (employeeData[i].hours * employeeData[i].wageRate);
  231. }
  232. }
  233. }
Success #stdin #stdout 0.01s 5292KB
stdin
51 42.5 37 45 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