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


*** 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