fork download
  1. //********************************************************
  2. //
  3. // Assignment 6 - Structures
  4. //
  5. // Name: Amanda Connors
  6. //
  7. // Class: C Programming, Fall 2026
  8. //
  9. // Date: October 18, 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.  
  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 int 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. float getHours (long int clockNumber);
  44. void printHeader (void);
  45. void printEmp (long int clockNumber, float wageRate, float hours,
  46. float overtimeHrs, float grossPay);
  47.  
  48. // TODO: Add your other function prototypes here
  49. float calcOT (float hours);
  50. float calcGross (float hours, float overtimeHrs, float wageRate);
  51.  
  52.  
  53. int main ()
  54. {
  55. // Set up a local variable to store the employee information
  56. struct employee employeeData[SIZE] = {
  57. { 98401, 10.60 },
  58. { 526488, 9.75 },
  59. { 765349, 10.50 }, // initialize clock and wage values
  60. { 34645, 12.25 },
  61. { 127615, 8.35 }
  62. };
  63.  
  64. int i; // loop and array index
  65.  
  66. // Call functions as needed to read and calculate information
  67. for (i = 0; i < SIZE; ++i)
  68. {
  69.  
  70. // Prompt for the number of hours worked by the employee
  71. employeeData[i].hours = getHours (employeeData[i].clockNumber);
  72.  
  73. // TODO: Add other function calls as needed to calculate overtime and gross
  74. employeeData[i].overtimeHrs = calcOT (employeeData[i].hours);
  75.  
  76. employeeData[i].grossPay = calcGross (employeeData[i].hours, employeeData[i].overtimeHrs, employeeData[i].wageRate);
  77.  
  78.  
  79. } // for
  80.  
  81. // Print the column headers
  82. printHeader();
  83.  
  84. // print out each employee
  85. for (i = 0; i < SIZE; ++i)
  86. {
  87. printEmp (employeeData[i].clockNumber,
  88. employeeData[i].wageRate,
  89. employeeData[i].hours,
  90. employeeData[i].overtimeHrs,
  91. employeeData[i].grossPay);
  92. }
  93.  
  94. return(0); // success
  95.  
  96. } // main
  97.  
  98. //**************************************************************
  99. // Function: getHours
  100. //
  101. // Purpose: Obtains input from user, the number of hours worked
  102. // per employee and stores the result in a local variable
  103. // that is passed back to the calling function.
  104. //
  105. // Parameters: clockNumber - The unique employee ID
  106. //
  107. // Returns: hoursWorked - hours worked in a given week
  108. //
  109. //**************************************************************
  110.  
  111. float getHours (long int clockNumber)
  112. {
  113.  
  114. float hoursWorked; // hours worked in a given week
  115.  
  116. // Read in hours for employee
  117. printf("\nEnter hours worked by emp # %06li: ", clockNumber);
  118. scanf ("%f", &hoursWorked);
  119.  
  120. // return hours back to the calling function
  121. return (hoursWorked);
  122.  
  123. } // getHours
  124.  
  125. //**************************************************************
  126. // Function: printHeader
  127. //
  128. // Purpose: Prints the initial table header information.
  129. //
  130. // Parameters: none
  131. //
  132. // Returns: void
  133. //
  134. //**************************************************************
  135.  
  136. void printHeader (void)
  137. {
  138.  
  139. printf ("\n\n*** Pay Calculator ***\n");
  140.  
  141. // print the table header
  142. printf("\nClock# Wage Hours OT Gross\n");
  143. printf("------------------------------------------------\n");
  144.  
  145. } // printHeader
  146.  
  147.  
  148. //*************************************************************
  149. // Function: printEmp
  150. //
  151. // Purpose: Prints out all the information for an employee
  152. // in a nice and orderly table format.
  153. //
  154. // Parameters:
  155. //
  156. // clockNumber - unique employee ID
  157. // wageRate - hourly wage rate
  158. // hours - Hours worked for the week
  159. // overtimeHrs - overtime hours worked in a week
  160. // grossPay - gross pay for the week
  161. //
  162. // Returns: void
  163. //
  164. //**************************************************************
  165.  
  166. void printEmp (long int clockNumber, float wageRate, float hours,
  167. float overtimeHrs, float grossPay)
  168. {
  169.  
  170. // Print out a single employee
  171. printf("\n %06li %5.2f %4.1f %4.1f %8.2f",
  172. clockNumber, wageRate, hours,
  173. overtimeHrs, grossPay);
  174.  
  175. } // printEmp
  176.  
  177. // TODO: Add your functions here
  178. //*************************************************************
  179. // Function: calcOT
  180. //
  181. // Purpose: calculates the overtime an employee worked
  182. //
  183. // Parameters:
  184. //
  185. // hours - Hours worked for the week
  186. //
  187. // Returns: overtimeHrs - the number of hours an employee worked beyond the normal workweek
  188. //
  189. //**************************************************************
  190.  
  191. float calcOT (float hours)
  192. {
  193. float overtimeHrs = 0.0; // init overtime hours variable
  194. if (hours >= STD_HOURS)
  195. {
  196. overtimeHrs = hours - STD_HOURS;
  197. } //end if
  198.  
  199. else // no OT
  200. {
  201. overtimeHrs = 0.0;
  202.  
  203. } // end else
  204.  
  205. return (overtimeHrs);
  206. } // end calcOT
  207.  
  208. //*************************************************************
  209. // Function: calcGross
  210. //
  211. // Purpose: calculates the gross pay for an employee given
  212. // the overtime hours, wage and regular hours
  213. //
  214. // Parameters:
  215. //
  216. // hours - Hours worked for the week
  217. // overtimeHrs - the number of hours an employee worked beyond the normal workweek
  218. // wageRate - employee's hourly wage
  219. //
  220. // Returns: grossPay
  221. //
  222. //**************************************************************
  223.  
  224. float calcGross (float hours, float overtimeHrs, float wageRate)
  225. {
  226. float overtimePay = 0.0; // init variable to hold overtime pay value
  227. float normalPay = 0.0; // init variable to hold normal pay value
  228. float grossPay = 0.0; // init variable for gross pay
  229.  
  230. overtimePay = overtimeHrs * OT_RATE * wageRate;
  231.  
  232. normalPay = wageRate * (hours - overtimeHrs);
  233.  
  234. grossPay = overtimePay + normalPay;
  235.  
  236. return (grossPay);
  237. } // end calcGross
Success #stdin #stdout 0.01s 5288KB
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