fork download
  1. //********************************************************
  2. //
  3. // Assignment 6 - Structures
  4. //
  5. // Name: <Mario Morkus>
  6. //
  7. // Class: C Programming, <Spring 2026>
  8. //
  9. // Date: <03/08/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 /* number of employees to process */
  25. #define STD_HOURS 40.0 /* standard hours in a normal work week */
  26. #define OT_RATE 1.5 /* overtime pay rate multiplier */
  27.  
  28. // Define a global structure to pass employee data between functions.
  29. // Note that the structure type is global, but the variable of that
  30. // type is declared locally in main and passed as needed.
  31. struct employee
  32. {
  33. long int clockNumber; /* unique employee clock/ID number */
  34. float wageRate; /* hourly wage rate */
  35. float hours; /* hours worked in a given week */
  36. float overtimeHrs; /* overtime hours worked in a week */
  37. float grossPay; /* gross pay for the week */
  38. };
  39.  
  40. // Function prototypes
  41. void getHours (struct employee employeeData[], int theSize);
  42. void calcOT (struct employee employeeData[], int theSize);
  43. void calcGross (struct employee employeeData[], int theSize);
  44. void printHeader (void);
  45. void printEmp (struct employee employeeData[], int theSize);
  46.  
  47. //**************************************************************
  48. // Function: main
  49. //
  50. // Purpose: Entry point of the program. Initializes an array
  51. // of employee structures with clock numbers and wage rates,
  52. // then calls functions to collect hours, compute overtime
  53. // and gross pay, and display the results in a table.
  54. //
  55. // Parameters: none
  56. //
  57. // Returns: 0 on successful completion
  58. //
  59. //**************************************************************
  60.  
  61. int main ()
  62. {
  63. // Initialize array of structures with clock numbers and wage rates.
  64. // Hours, overtime, and gross pay default to 0.0.
  65. struct employee employeeData[SIZE] = {
  66. { 98401, 10.60 },
  67. { 526488, 9.75 },
  68. { 765349, 10.50 },
  69. { 34645, 12.25 },
  70. { 127615, 8.35 }
  71. };
  72.  
  73. // Prompt for and store hours worked for each employee
  74. getHours (employeeData, SIZE);
  75.  
  76. // Calculate overtime hours for each employee
  77. calcOT (employeeData, SIZE);
  78.  
  79. // Calculate gross pay for each employee
  80. calcGross (employeeData, SIZE);
  81.  
  82. // Print the column header for the output table
  83. printHeader ();
  84.  
  85. // Print all employee data in table format
  86. printEmp (employeeData, SIZE);
  87.  
  88. return (0); /* success */
  89.  
  90. } /* main */
  91.  
  92. //**************************************************************
  93. // Function: getHours
  94. //
  95. // Purpose: Obtains input from the user -- the number of hours
  96. // worked per employee -- and stores each result directly
  97. // into the hours member of the corresponding array element.
  98. // The array of structures is passed by reference so the
  99. // calling function sees the updated hours values.
  100. //
  101. // Parameters:
  102. //
  103. // employeeData - array of structures containing employee data
  104. // theSize - number of employees to process
  105. //
  106. // Returns: Nothing (void)
  107. //
  108. //**************************************************************
  109.  
  110. void getHours (struct employee employeeData[], int theSize)
  111. {
  112. int i; /* loop and array index */
  113.  
  114. // Prompt for and read hours worked for each employee
  115. for (i = 0; i < theSize; ++i)
  116. {
  117. printf ("\nEnter hours worked by emp # %06li: ",
  118. employeeData[i].clockNumber);
  119. scanf ("%f", &employeeData[i].hours);
  120. } /* for */
  121.  
  122. } /* getHours */
  123.  
  124. //**************************************************************
  125. // Function: calcOT
  126. //
  127. // Purpose: Calculates the overtime hours worked for each
  128. // employee. Any hours beyond STD_HOURS (40.0) are counted
  129. // as overtime. If the employee worked 40 hours or fewer,
  130. // overtime is set to 0.0. Results are stored directly in
  131. // the overtimeHrs member of each array element.
  132. //
  133. // Parameters:
  134. //
  135. // employeeData - array of structures containing employee data
  136. // theSize - number of employees to process
  137. //
  138. // Returns: Nothing (void)
  139. //
  140. //**************************************************************
  141.  
  142. void calcOT (struct employee employeeData[], int theSize)
  143. {
  144. int i; /* loop and array index */
  145.  
  146. // Determine overtime hours for each employee
  147. for (i = 0; i < theSize; ++i)
  148. {
  149. // Any hours beyond the standard work week are overtime
  150. if (employeeData[i].hours > STD_HOURS)
  151. {
  152. employeeData[i].overtimeHrs = employeeData[i].hours - STD_HOURS;
  153. }
  154. else
  155. {
  156. employeeData[i].overtimeHrs = 0.0; /* no overtime earned */
  157. } /* if */
  158.  
  159. } /* for */
  160.  
  161. } /* calcOT */
  162.  
  163. //**************************************************************
  164. // Function: calcGross
  165. //
  166. // Purpose: Calculates the gross pay for each employee using
  167. // their wage rate, hours worked, and overtime hours.
  168. // Normal pay covers hours up to STD_HOURS at the standard
  169. // wage. Overtime pay covers any extra hours at OT_RATE
  170. // times the standard wage. Results are stored directly in
  171. // the grossPay member of each array element.
  172. //
  173. // Parameters:
  174. //
  175. // employeeData - array of structures containing employee data
  176. // theSize - number of employees to process
  177. //
  178. // Returns: Nothing (void)
  179. //
  180. //**************************************************************
  181.  
  182. void calcGross (struct employee employeeData[], int theSize)
  183. {
  184. int i; /* loop and array index */
  185. float normalPay; /* pay earned for non-overtime hours */
  186. float overtimePay; /* additional pay earned for overtime hours */
  187.  
  188. // Calculate gross pay for each employee
  189. for (i = 0; i < theSize; ++i)
  190. {
  191. // Normal pay: standard hours (or fewer) at the regular wage rate
  192. normalPay = (employeeData[i].hours - employeeData[i].overtimeHrs)
  193. * employeeData[i].wageRate;
  194.  
  195. // Overtime pay: overtime hours at time-and-a-half
  196. overtimePay = employeeData[i].overtimeHrs
  197. * employeeData[i].wageRate
  198. * OT_RATE;
  199.  
  200. // Gross pay is the sum of normal and overtime pay
  201. employeeData[i].grossPay = normalPay + overtimePay;
  202.  
  203. } /* for */
  204.  
  205. } /* calcGross */
  206.  
  207. //**************************************************************
  208. // Function: printHeader
  209. //
  210. // Purpose: Prints the table title and column header labels
  211. // that appear above the employee data rows.
  212. //
  213. // Parameters: none
  214. //
  215. // Returns: Nothing (void)
  216. //
  217. //**************************************************************
  218.  
  219. void printHeader (void)
  220. {
  221. printf ("\n\n*** Pay Calculator ***\n");
  222.  
  223. // Print column labels and separator line
  224. printf ("\n%-8s %-5s %-5s %-5s %-8s\n",
  225. "Clock#", "Wage", "Hours", "OT", "Gross");
  226. printf ("--------------------------------------------\n");
  227.  
  228. } /* printHeader */
  229.  
  230. //**************************************************************
  231. // Function: printEmp
  232. //
  233. // Purpose: Outputs a single formatted row to the screen for
  234. // each employee showing clock number, wage rate, hours
  235. // worked, overtime hours, and gross pay.
  236. //
  237. // Parameters:
  238. //
  239. // employeeData - array of structures containing employee data
  240. // theSize - number of employees to process
  241. //
  242. // Returns: Nothing (void)
  243. //
  244. //**************************************************************
  245.  
  246. void printEmp (struct employee employeeData[], int theSize)
  247. {
  248. int i; /* loop and array index */
  249.  
  250. // Print one row per employee
  251. for (i = 0; i < theSize; ++i)
  252. {
  253. printf ("%06li %5.2f %5.1f %5.1f %8.2f\n",
  254. employeeData[i].clockNumber,
  255. employeeData[i].wageRate,
  256. employeeData[i].hours,
  257. employeeData[i].overtimeHrs,
  258. employeeData[i].grossPay);
  259. } /* for */
  260.  
  261. } /* printEmp */
Success #stdin #stdout 0.01s 5292KB
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