fork download
  1. //********************************************************
  2. //
  3. // Assignment 6 - Structures
  4. //
  5. // Name: Maya Mahin
  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 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 overtimePay;
  40. float normalPay;
  41. float grossPay;
  42. };
  43.  
  44. // define prototypes here for each function except main
  45.  
  46. void getHours (struct employee employeeData[], int theSize );
  47. void getOvertimeHours (struct employee employeeData[], int theSize );
  48. void getOvertimePay (struct employee employeeData[], int theSize );
  49. void getNormalPay (struct employee employeeData[], int theSize );
  50. void getGrossPay (struct employee employeeData[], int theSize );
  51. void printHeader (void);
  52. void printEmp (struct employee emp [ ], int theSize);
  53.  
  54. // TODO: add your function prototypes here
  55.  
  56. int main ()
  57. {
  58. // Set up a local variable and initialize the clock and wages of my employees
  59. struct employee employeeData [SIZE] = {
  60. { 98401, 10.60 },
  61. { 526488, 9.75 },
  62. { 765349, 10.50 },
  63. { 34645, 12.25 },
  64. { 127615, 8.35 }
  65. };
  66.  
  67. // Call function needed to read hours
  68. getHours (employeeData, SIZE);
  69.  
  70. // TODO: Call functions calculate ot hours and gross pay
  71. getOvertimeHours (employeeData, SIZE);
  72. getOvertimePay (employeeData, SIZE);
  73. getNormalPay (employeeData, SIZE);
  74. getGrossPay (employeeData, SIZE);
  75.  
  76. // TODO: Call function to print the table column headers
  77.  
  78. // Print a table header
  79. printHeader();
  80.  
  81. // Function call to output results to the screen in table format
  82. printEmp (employeeData, SIZE);
  83.  
  84. return(0); // success
  85.  
  86. } // main
  87.  
  88. //**************************************************************
  89. // Function: getHours
  90. //
  91. // Purpose: Obtains input from user, the number of hours worked
  92. // per employee and stores the result in an array of structures
  93. // that is passed back to the calling function by reference.
  94. //
  95. // Parameters:
  96. //
  97. // employeeData - an array of structures containing Employees
  98. // theSize - number of employees to process
  99. //
  100. // Returns: Nothing (void)
  101. //
  102. //**************************************************************
  103.  
  104. void getHours (struct employee employeeData[], int theSize )
  105. {
  106.  
  107. int i; // loop and array index
  108.  
  109. // read hours in for each employee
  110. for (i = 0; i < theSize ; ++i)
  111. {
  112. printf("\nEnter hours worked by emp # %06li: ",
  113. employeeData[i].clockNumber);
  114. scanf ("%f", &employeeData[i].hours);
  115. } // for
  116.  
  117. } // getHours
  118.  
  119. //**************************************************************
  120. // Function: getOvertimeHours
  121. //
  122. // Purpose: Obtains the number of hours worked
  123. // per employee, calculates overtime hours, and stores the result in an array of structures
  124. // that is passed back to the calling function by reference.
  125. //
  126. // Parameters:
  127. // employeeData - an array of structures containing Employees
  128. // theSize - number of employees to process
  129. //
  130. //
  131. // Returns: Nothing (void)
  132. //
  133. //**************************************************************
  134. void getOvertimeHours (struct employee employeeData[], int theSize)
  135. {
  136. int i; // loop and array index
  137.  
  138. // read hours in for each employee
  139. for (i = 0; i < theSize ; ++i)
  140. {
  141.  
  142. if (employeeData[i].hours>STD_HOURS){
  143. employeeData[i].overtimeHrs= employeeData[i].hours - STD_HOURS;
  144. }
  145. else{
  146. employeeData[i].overtimeHrs=0;
  147. }
  148. }
  149. }
  150. //**************************************************************
  151. // Function: getOvertimePay
  152. //
  153. // Purpose: Obtains the number of overtime hours worked and wage rate for a specific employee
  154. // calculates the overtime pay for that employee and stores the result in an array of structures
  155. // that is passed back to the calling function by reference.
  156. //
  157. // Parameters:
  158. // employeeData - an array of structures containing Employees
  159. // theSize - number of employees to process
  160. //
  161. //
  162. // Returns: Nothing (void)
  163. //
  164. //**************************************************************
  165. void getOvertimePay (struct employee employeeData[], int theSize){
  166. int i; // loop and array index
  167.  
  168. // read hours in for each employee
  169. for (i = 0; i < theSize ; ++i) {
  170.  
  171. employeeData[i].overtimePay=(OT_RATE * employeeData[i].wageRate) * employeeData[i].overtimeHrs;
  172.  
  173. }
  174. }
  175. //**************************************************************
  176. // Function: getNormalPay
  177. //
  178. // Purpose: Uses the hours worked and the wage rate
  179. // for a specific employee, calculates the number of normal (i.e., non-overtime) hours worked
  180. // to calculate normal pay and stores the result in an array of structures
  181. // that is passed back to the calling function by reference.
  182. //
  183. // Parameters:
  184. // employeeData - an array of structures containing Employees
  185. // theSize - number of employees to process
  186. //
  187. //
  188. // Returns: Nothing (void)
  189. //
  190. //**************************************************************
  191. void getNormalPay (struct employee employeeData[], int theSize)
  192. {
  193. int i; // loop and array index
  194.  
  195. // read hours in for each employee
  196. for (i = 0; i < theSize ; ++i) {
  197.  
  198. float normalHours;
  199.  
  200. //calculates the number of normal (i.e., non-overtime) hours worked
  201. if (employeeData[i].hours >= STD_HOURS) {
  202. normalHours=STD_HOURS;
  203. }else if (employeeData[i].hours<STD_HOURS){
  204. normalHours=employeeData[i].hours;
  205. }
  206. //calculates normal pay
  207. employeeData[i].normalPay=(employeeData[i].wageRate * normalHours);
  208.  
  209. }
  210. }
  211. //**************************************************************
  212. // Function: getGrossPay
  213. //
  214. // Purpose: Uses the normal pay and overtime pay for a specific employee in a specific week
  215. // to calculate gross pay for a specific employee in a specific week and stores the result in an array of structures
  216. // that is passed back to the calling function by reference.
  217. //
  218. // Parameters:
  219. // employeeData - an array of structures containing Employees
  220. // theSize - number of employees to process
  221. //
  222. //
  223. // Returns: Nothing (void)
  224. //
  225. //**************************************************************
  226. void getGrossPay (struct employee employeeData[], int theSize)
  227. {
  228. int i; // loop and array index
  229.  
  230. // read hours in for each employee
  231. for (i = 0; i < theSize ; ++i) {
  232. // Calculate Gross Pay
  233. employeeData[i].grossPay= employeeData[i].normalPay + employeeData[i].overtimePay;
  234. }
  235. }
  236.  
  237. //**************************************************************
  238. // Function: printHeader
  239. //
  240. // Purpose: Prints the initial table header information.
  241. //
  242. // Parameters: none
  243. //
  244. // Returns: void
  245. //
  246. //**************************************************************
  247.  
  248. void printHeader (void)
  249. {
  250.  
  251. printf ("\n\n*** Pay Calculator ***\n");
  252.  
  253. // print the table header
  254. printf("\nClock# Wage Hours OT Gross\n");
  255. printf("------------------------------------------------\n");
  256.  
  257. } // printHeader
  258.  
  259. // ********************************************************************
  260. // Function: printEmp
  261. //
  262. // Purpose: Outputs to screen in a table format the following
  263. // information about an employee: Clock, Wage,
  264. // Hours, Overtime Hours, and Gross Pay.
  265. //
  266. // Parameters:
  267. //
  268. // employeeData - an array of structures containing Employees
  269. // theSize - number of employees to process
  270. //
  271. // Returns: Nothing (void)
  272. //
  273. // *********************************************************************
  274.  
  275. void printEmp ( struct employee employeeData[], int theSize )
  276. {
  277. int i; // loop and array index
  278.  
  279. // print information about each employee
  280. for (i = 0; i < theSize ; ++i)
  281. {
  282. printf("\n %06li %5.2f %4.1f %4.1f %8.2f",
  283. employeeData[i].clockNumber, employeeData[i].wageRate, employeeData[i].hours,
  284. employeeData[i].overtimeHrs, employeeData[i].grossPay);
  285. } /* for */
  286.  
  287. } // printEmp
  288.  
  289. // TODO: add your functions here
Success #stdin #stdout 0s 5276KB
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