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