fork download
  1. //********************************************************
  2. //
  3. // Assignment 6 - Structures
  4. //
  5. // Name: <Matthew Tracy>
  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
  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 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.  
  44. void calcOT(struct employee employeeData[], int theSize);
  45.  
  46. void calcGross(struct employee employeeData[], int theSize);
  47.  
  48. void printTable();
  49.  
  50. void getHours (struct employee employeeData[], int theSize );
  51. void printHeader (void);
  52. void printEmp (struct employee emp [ ], int theSize);
  53.  
  54. // TODO: add your function prototypes here
  55. //***********************************************************************
  56. //Function: calcOT
  57. //
  58. //Purpose: Obtains input and calculates overtime pay for employees
  59. //
  60. //Parameters: struct employee employeeData[] - employee structure
  61. //
  62. //Returns: Nothing
  63. //************************************************************************
  64.  
  65. void calcOT(struct employee employeeData[], int theSize)
  66. {
  67.  
  68. for ( int i = 0; i < theSize; i++)
  69. {
  70. if (employeeData[i].hours > STD_HOURS)
  71. {
  72. employeeData[i].overtimeHrs = employeeData[i].hours - STD_HOURS;
  73. }
  74. else
  75. {
  76. employeeData[i].overtimeHrs = 0.0;
  77. }
  78. }
  79. }
  80. //************************************************************************************
  81. //Function: calcGross
  82. //
  83. //Purpose: Obtains input and data to calculat overtime pay and regular pay combined
  84. //
  85. //Parameters: struct employee employeeData[]
  86. //
  87. //Returns: Nothing
  88. //
  89. //************************************************************************************
  90. void calcGross(struct employee employeeData[], int theSize)
  91. {
  92. for (int i = 0; i< theSize; i++)
  93. {
  94. float regularPay = 0.0;
  95. float overtimePay = 0.0;
  96.  
  97. if (employeeData[i].overtimeHrs > 0.0)
  98. {
  99. regularPay = STD_HOURS * employeeData[i].wageRate;
  100.  
  101. overtimePay = employeeData[i].overtimeHrs * OT_RATE;
  102. }
  103. else
  104. {
  105. regularPay = employeeData[i].hours * employeeData[i].wageRate;
  106.  
  107. }
  108. employeeData[i].grossPay = regularPay + overtimePay;
  109.  
  110. }
  111.  
  112. }
  113.  
  114. int main ()
  115. {
  116. // Set up a local variable and initialize the clock and wages of my employees
  117. struct employee employeeData [SIZE] = {
  118. { 98401, 10.60 },
  119. { 526488, 9.75 },
  120. { 765349, 10.50 },
  121. { 34645, 12.25 },
  122. { 127615, 8.35 }
  123. };
  124.  
  125. // Call function needed to read hours
  126. getHours (employeeData, SIZE);
  127.  
  128. // TODO: Call functions calculate ot hours and gross pay
  129. calcOT( employeeData,SIZE);
  130. calcGross(employeeData,SIZE);
  131.  
  132. // TODO: Call function to print the table column headers
  133.  
  134. // Print a table header
  135. printHeader();
  136.  
  137. // Function call to output results to the screen in table format
  138. printEmp (employeeData, SIZE);
  139.  
  140. return(0); // success
  141.  
  142. } // main
  143.  
  144. //**************************************************************
  145. // Function: getHours
  146. //
  147. // Purpose: Obtains input from user, the number of hours worked
  148. // per employee and stores the result in an array of structures
  149. // that is passed back to the calling function by reference.
  150. //
  151. // Parameters:
  152. //
  153. // employeeData - an array of structures containing Employees
  154. // theSize - number of employees to process
  155. //
  156. // Returns: Nothing (void)
  157. //
  158. //**************************************************************
  159.  
  160. void getHours (struct employee employeeData[], int theSize )
  161. {
  162.  
  163. int i; // loop and array index
  164.  
  165. // read hours in for each employee
  166. for (i = 0; i < theSize ; ++i)
  167. {
  168. printf("\nEnter hours worked by emp # %06li: ",
  169. employeeData[i].clockNumber);
  170. scanf ("%f", &employeeData[i].hours);
  171. } // for
  172.  
  173. } // getHours
  174.  
  175. //**************************************************************
  176. // Function: printHeader
  177. //
  178. // Purpose: Prints the initial table header information.
  179. //
  180. // Parameters: none
  181. //
  182. // Returns: void
  183. //
  184. //**************************************************************
  185.  
  186. void printHeader (void)
  187. {
  188.  
  189. printf ("\n\n*** Pay Calculator ***\n");
  190.  
  191. // print the table header
  192. printf("\nClock# Wage Hours OT Gross\n");
  193. printf("------------------------------------------------\n");
  194.  
  195. } // printHeader
  196.  
  197. // ********************************************************************
  198. // Function: printEmp
  199. //
  200. // Purpose: Outputs to screen in a table format the following
  201. // information about an employee: Clock, Wage,
  202. // Hours, Overtime Hours, and Gross Pay.
  203. //
  204. // Parameters:
  205. //
  206. // employeeData - an array of structures containing Employees
  207. // theSize - number of employees to process
  208. //
  209. // Returns: Nothing (void)
  210. //
  211. // *********************************************************************
  212.  
  213. void printEmp ( struct employee employeeData[], int theSize )
  214. {
  215. int i; // loop and array index
  216.  
  217. // print information about each employee
  218. for (i = 0; i < theSize ; ++i)
  219. {
  220. printf("\n %06li %5.2f %4.1f %4.1f %8.2f",
  221. employeeData[i].clockNumber, employeeData[i].wageRate, employeeData[i].hours,
  222. employeeData[i].overtimeHrs, employeeData[i].grossPay);
  223. } /* for */
  224.  
  225. } // printEmp
  226.  
  227. // TODO: add your functions here
  228.  
Success #stdin #stdout 0.01s 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   440.50
 526488  9.75 42.5  2.5   393.75
 765349 10.50 37.0  0.0   388.50
 034645 12.25 45.0  5.0   497.50
 127615  8.35  0.0  0.0     0.00