fork download
  1. //********************************************************
  2. //
  3. // Assignment 6 - Structures
  4. //
  5. // Name: Christopher J. Golebiowski
  6. //
  7. // Class: C Programming, Spring 2026
  8. //
  9. // Date: 08-Mar-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. // Functions called by a combination of by value and by
  16. // reference.
  17. //
  18. //********************************************************
  19.  
  20. // Define and Includes
  21.  
  22. #include <stdio.h>
  23.  
  24. // Define Constants
  25. #define SIZE 5
  26. #define STD_HOURS 40.0
  27. #define OT_RATE 1.5
  28.  
  29. // Define a global structure to pass employee data between functions
  30. // Note that the structure type is global, but you don't want a variable
  31. // of that type to be global. Best to declare a variable of that type
  32. // in a function like main or another function and pass as needed.
  33.  
  34. struct employee
  35. {
  36. long int clockNumber;
  37. float wageRate;
  38. float hours;
  39. float overtimeHrs;
  40. float grossPay;
  41. };
  42.  
  43. // define prototypes here for each function except main
  44. float getHours (long int clockNumber);
  45. void printHeader (void);
  46. void printEmp (struct employee emp [ ], int theSize);
  47. float calculateOvertimeHours (float hours, float standardWorkWeekHours);
  48. float calculateGrossPay (float hours, float overtimeHours, float wage, float otWageRate);
  49.  
  50. int main ()
  51. {
  52. // Set up a local variable to store the employee information
  53. struct employee employeeData[SIZE] = {
  54. { 98401, 10.60 },
  55. { 526488, 9.75 },
  56. { 765349, 10.50 }, // initialize clock and wage values
  57. { 34645, 12.25 },
  58. { 127615, 8.35 }
  59. };
  60.  
  61. int i; // loop and array index
  62.  
  63. // Call functions as needed to read and calculate information
  64. for (i = 0; i < SIZE; ++i)
  65. {
  66.  
  67. // Prompt for the number of hours worked by the employee
  68. employeeData[i].hours = getHours (employeeData[i].clockNumber);
  69.  
  70. // calculate overtime hours
  71. employeeData[i].overtimeHrs = calculateOvertimeHours (employeeData[i].hours, STD_HOURS);
  72.  
  73. // calculate gross pay
  74. employeeData[i].grossPay = calculateGrossPay (employeeData[i].hours, employeeData[i].overtimeHrs, employeeData[i].wageRate, OT_RATE);
  75.  
  76. } // for
  77.  
  78. // Print the column headers
  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 a local variable
  93. // that is passed back to the calling function.
  94. //
  95. // Parameters: clockNumber - The unique employee ID
  96. //
  97. // Returns: hoursWorked - hours worked in a given week
  98. //
  99. //**************************************************************
  100.  
  101. float getHours (long int clockNumber)
  102. {
  103.  
  104. float hoursWorked; // hours worked in a given week
  105.  
  106. // Read in hours for employee
  107. printf("\nEnter hours worked by emp # %06li: ", clockNumber);
  108. scanf ("%f", &hoursWorked);
  109.  
  110. // return hours back to the calling function
  111. return (hoursWorked);
  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, and Gross Pay.
  143. //
  144. // Parameters:
  145. //
  146. // employeeData - an array of structures containing
  147. // employee information
  148. // theSize - number of employees to process
  149. //
  150. // Returns: Nothing (void)
  151. //
  152. // *******************************************************************
  153.  
  154. void printEmp ( struct employee employeeData[], int theSize )
  155. {
  156. int i; // loop index
  157.  
  158. // print information about each employee
  159. for (i = 0; i < theSize ; ++i)
  160. {
  161. printf("\n %06li %5.2f %4.1f %4.1f %8.2f",
  162. employeeData[i].clockNumber, employeeData[i].wageRate, employeeData[i].hours,
  163. employeeData[i].overtimeHrs, employeeData[i].grossPay);
  164. } // for
  165.  
  166. } // printEmp
  167.  
  168.  
  169.  
  170. //**************************************************************
  171. // Function: calculateOvertimeHours
  172. //
  173. // Purpose: Calculates the number of overtime hours worked
  174. // based on total hours worked by the employee and the number
  175. // of hours in a standard work week. The result is
  176. // passed back to the calling function.
  177. //
  178. // Parameters:
  179. // hours - number of hours worked by employee
  180. // standardWorkWeekHours - number of hours in a standard work week
  181. //
  182. // Returns: overtimeHours - overtime hours worked in a given week
  183. //
  184. //**************************************************************
  185.  
  186. float calculateOvertimeHours (float hours, float standardWorkWeekHours)
  187. {
  188.  
  189. float overtimeHours; // overtime hours worked in a given week
  190.  
  191. // Calculate overtime for employee
  192. if (hours >= standardWorkWeekHours)
  193. {
  194. overtimeHours = hours - standardWorkWeekHours;
  195. }
  196. else // no OT
  197. {
  198. overtimeHours = 0;
  199. }
  200.  
  201. // return overtime hours back to the calling function
  202. return (overtimeHours);
  203.  
  204. } // calculateOvertimeHours
  205.  
  206.  
  207. //**************************************************************
  208. // Function: calculateGrossPay
  209. //
  210. // Purpose: Calculates the total gross pay for an employee
  211. // based on hours worked by the employee wage rate. Includes
  212. // overtime rate calculated via a wage rate multiplier. The result is
  213. // passed back to the calling function.
  214. //
  215. // Parameters:
  216. // hours - number of hours worked by employee
  217. // overtimeHours - number of overtime hours worked by employee
  218. // wage - individual wage rate for the employee
  219. // otWageRate - wage rate multiplier for overtime calculation
  220. //
  221. // Returns: overtimeHours - overtime hours worked in a given week
  222. //
  223. //**************************************************************
  224.  
  225. float calculateGrossPay (float hours, float overtimeHours, float wage, float otWageRate)
  226. {
  227.  
  228. float grossPay; // gross pay for employee in given week
  229.  
  230. // Calculate gross pay for employee
  231. grossPay = ((hours - overtimeHours) * wage) + (overtimeHours * (wage * otWageRate));
  232.  
  233. // return gross pay back to the calling function
  234. return (grossPay);
  235.  
  236. } // calculateGrossPay
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: 

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