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