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