fork download
  1. //********************************************************
  2. //
  3. // Assignment 6 - Structures
  4. //
  5. // Name: Heather Grothe
  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 Value design
  16. //
  17. //********************************************************
  18.  
  19. // Define and Includes
  20. #include <stdio.h>
  21.  
  22. // Define Constants
  23. #define SIZE 5
  24. #define STD_HOURS 40.0
  25. #define OT_RATE 1.5
  26.  
  27. // Define a global structure to pass employee data between functions
  28. // Note that the structure type is global, but you don't want a variable
  29. // of that type to be global. Best to declare a variable of that type
  30. // in a function like main or another function and pass as needed.
  31.  
  32. struct employee
  33. {
  34. long int clockNumber; // Employees unique clock number
  35. float wageRate; // Employees hourly rate
  36. float hours; // Employees number of hours worked in a week
  37. float overtimeHrs; // Employees number of hour over the standard work week
  38. float grossPay; // Employees weekly pay
  39. };
  40.  
  41. // Define prototypes here for each function except main
  42. float getHours (long int clockNumber);
  43. void printHeader (void);
  44. void printEmp (long int clockNumber, float wageRate, float hours,
  45. float overtimeHrs, float grossPay);
  46. float calculateOvertimeHours(float hours);
  47. float calculateGrossPay(float wageRate, float hours, float overtimeHrs);
  48.  
  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.  
  65. for (i = 0; i < SIZE; ++i)
  66. {
  67.  
  68. // Prompt for the number of hours worked by the employee
  69. employeeData[i].hours = getHours (employeeData[i].clockNumber);
  70.  
  71. // Calculate overtime hours
  72. employeeData[i].overtimeHrs = calculateOvertimeHours(employeeData[i].hours);
  73.  
  74. // Calculate gross pay
  75. employeeData[i].grossPay = calculateGrossPay(employeeData[i].wageRate,
  76. employeeData[i].hours, employeeData[i].overtimeHrs);
  77.  
  78. } // for
  79.  
  80. // Print the column headers
  81. printHeader();
  82.  
  83. // Print out each employee
  84. for (i = 0; i < SIZE; ++i)
  85. {
  86. printEmp (employeeData[i].clockNumber,
  87. employeeData[i].wageRate,
  88. employeeData[i].hours,
  89. employeeData[i].overtimeHrs,
  90. employeeData[i].grossPay);
  91. }
  92.  
  93. return(0); // success
  94.  
  95. } // main
  96.  
  97. //**************************************************************
  98. // Function: getHours
  99. //
  100. // Purpose: Obtains input from user, the number of hours worked
  101. // per employee and stores the result in a local variable
  102. // that is passed back to the calling function.
  103. //
  104. // Parameters: clockNumber - The unique employee ID
  105. //
  106. // Returns: hoursWorked - hours worked in a given week
  107. //
  108. //**************************************************************
  109.  
  110. float getHours (long int clockNumber)
  111. {
  112.  
  113. float hoursWorked; // hours worked in a given week
  114.  
  115. // Read in hours for employee
  116. printf("\nEnter hours worked by emp # %06li: ", clockNumber);
  117. scanf ("%f", &hoursWorked);
  118.  
  119. // Return hours back to the calling function
  120. return (hoursWorked);
  121.  
  122. } // getHours
  123.  
  124. //**************************************************************
  125. // Function: printHeader
  126. //
  127. // Purpose: Prints the initial table header information.
  128. //
  129. // Parameters: none
  130. //
  131. // Returns: void
  132. //
  133. //**************************************************************
  134.  
  135. void printHeader (void)
  136. {
  137.  
  138. printf ("\n\n*** Pay Calculator ***\n");
  139.  
  140. // print the table header
  141. printf("\nClock# Wage Hours OT Gross\n");
  142. printf("------------------------------------------------\n");
  143.  
  144. } // printHeader
  145.  
  146.  
  147. //*************************************************************
  148. // Function: printEmp
  149. //
  150. // Purpose: Prints out all the information for an employee
  151. // in a nice and orderly table format.
  152. //
  153. // Parameters:
  154. //
  155. // clockNumber - unique employee ID
  156. // wageRate - hourly wage rate
  157. // hours - Hours worked for the week
  158. // overtimeHrs - overtime hours worked in a week
  159. // grossPay - gross pay for the week
  160. //
  161. // Returns: void
  162. //
  163. //**************************************************************
  164.  
  165. void printEmp (long int clockNumber, float wageRate, float hours,
  166. float overtimeHrs, float grossPay)
  167. {
  168.  
  169. // Print out a single employee
  170. printf("\n %06li %6.2f %5.1f %5.1f %8.2f",
  171. clockNumber, wageRate, hours,
  172. overtimeHrs, grossPay);
  173.  
  174. } // printEmp
  175.  
  176. //**************************************************************
  177. // Function: calculateOvertimeHours
  178. //
  179. // Purpose: Calculate the number of hours of overtime worked.
  180. //
  181. // Parameters: hours - hours worked by the employee.
  182. //
  183. // Returns: overtimeHrs - number of hours worked above the STD_WORK_WEEK constant.
  184. //
  185. //**************************************************************
  186.  
  187.  
  188. float calculateOvertimeHours(float hours){
  189. float overtimeHrs; // Stored variable to calculate overtime hours
  190.  
  191. // If else statement to determine possible overtime hours
  192. if (hours > STD_HOURS) {
  193. overtimeHrs = hours - STD_HOURS;
  194. } else {
  195. overtimeHrs = 0;
  196. } // If else
  197.  
  198. // return overtimeHrs to the calling function
  199. return overtimeHrs;
  200.  
  201. } // calculateOvertimeHours
  202.  
  203.  
  204. //**************************************************************
  205. // Function: calculateGrossPay
  206. //
  207. // Purpose: Calculate the gross pay for the employee.
  208. //
  209. // Parameters: wageRate - employee wages per hour
  210. // hours - hours worked by the employee.
  211. // overtimeHrs - overtime hours worked by the employee
  212. //
  213. // Returns: grossPay - total pay earned by employee for all hours worked including overtime.
  214. //
  215. //**************************************************************
  216.  
  217. float calculateGrossPay(float wageRate, float hours, float overtimeHrs){
  218. float grossPay; // variable to store gross pay
  219. float overtimePay; // variable to store overtime pay
  220. float normalPay; // variable to store normal pay
  221.  
  222. overtimePay = overtimeHrs * wageRate * OT_RATE;
  223. normalPay = (hours - overtimeHrs) * wageRate;
  224. grossPay = normalPay + overtimePay;
  225.  
  226. // returns gross pay to the calling function
  227. return grossPay;
  228.  
  229.  
  230. } // calculateGrossPay
Success #stdin #stdout 0s 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
------------------------------------------------

 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