fork download
  1. //********************************************************
  2. //
  3. // Assignment 6 - Structures
  4. //
  5. // Name: K. Asha Vigilante-Pini
  6. //
  7. // Class: C Programming, <replace with Semester and Year>
  8. //
  9. // Date: 6/07/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. // Functions called by a combination of by value and by
  16. // reference.
  17. //
  18. //********************************************************
  19.  
  20. // Define and Includes
  21.  
  22. #include <stdio.h>
  23.  
  24. // Define Constants
  25. #define SIZE 5
  26. #define STD_HOURS 40.0
  27. #define OT_RATE 1.5
  28.  
  29. // Define a global structure to pass employee data between functions
  30. // Note that the structure type is global, but you don't want a variable
  31. // of that type to be global. Best to declare a variable of that type
  32. // in a function like main or another function and pass as needed.
  33.  
  34. struct employee
  35. {
  36. long int clockNumber;
  37. float wageRate;
  38. float hours;
  39. float overtimeHrs;
  40. float grossPay;
  41. float totalSum; // TODO: total of Wage, Hours, Overtime, and Gross for one employee
  42. };
  43.  
  44. // define prototypes here for each function except main
  45. float getHours (long int clockNumber);
  46. void printHeader (void);
  47. void printEmp (struct employee emp [ ], int theSize);
  48.  
  49. // TODO: Add your other function prototypes here
  50.  
  51. float calOvertimeHrs (float hours); // function to calculate overtime hours
  52. float calGrossPay (float hours, float overtimeHrs, float wageRate); //function to calculate gross pay
  53.  
  54.  
  55. int main ()
  56. {
  57. // Set up a local variable to store the employee information
  58. struct employee employeeData[SIZE] = {
  59. { 98401, 10.60 },
  60. { 526488, 9.75 },
  61. { 765349, 10.50 }, // initialize clock and wage values
  62. { 34645, 12.25 },
  63. { 127615, 8.35 }
  64. };
  65.  
  66. int i; // loop and array index
  67.  
  68. // Call functions as needed to read and calculate information
  69. for (i = 0; i < SIZE; ++i)
  70. {
  71.  
  72. // Prompt for the number of hours worked by the employee
  73. employeeData[i].hours = getHours (employeeData[i].clockNumber);
  74.  
  75. // TODO: Add other function calls as needed to calculate overtime and gross
  76.  
  77. employeeData[i].overtimeHrs = calOvertimeHrs(employeeData[i].hours);
  78. employeeData[i].grossPay = calGrossPay(employeeData[i].hours,employeeData[i].overtimeHrs, employeeData[i].wageRate);
  79.  
  80.  
  81. // TODO: Calculate the total sum of Wage, Hours, Overtime, and Gross values
  82.  
  83. employeeData[i].totalSum = employeeData[i].wageRate
  84. + employeeData[i].hours
  85. + employeeData[i].overtimeHrs
  86. + employeeData[i].grossPay;
  87.  
  88. } // for
  89.  
  90. // Print the column headers
  91. printHeader();
  92.  
  93. // Function call to output results to the screen in table format.
  94. printEmp (employeeData, SIZE);
  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 Total\n");
  145. printf("------------------------------------------------\n");
  146.  
  147. } // printHeader
  148.  
  149. // ******************************************************************
  150. // Function: printEmp
  151. //
  152. // Purpose: Outputs to screen in a table format the following
  153. // information about an employee: Clock, Wage,
  154. // Hours, Overtime, and Gross Pay.
  155. //
  156. // Parameters:
  157. //
  158. // employeeData - an array of structures containing
  159. // employee information
  160. // theSize - number of employees to process
  161. //
  162. // Returns: Nothing (void)
  163. //
  164. // *******************************************************************
  165.  
  166. void printEmp ( struct employee employeeData[], int theSize )
  167. {
  168. int i; // loop index
  169.  
  170. // print information about each employee
  171. for (i = 0; i < theSize ; ++i)
  172. {
  173. printf("\n %06li %5.2f %4.1f %4.1f %8.2f %9.2f",
  174. employeeData[i].clockNumber, employeeData[i].wageRate, employeeData[i].hours,
  175. employeeData[i].overtimeHrs, employeeData[i].grossPay,employeeData[i].totalSum );
  176. } // for
  177.  
  178. } // printEmp
  179.  
  180. // TODO: Add your functions here
  181.  
  182. //**************************************************************
  183. // Function: calOvertimeHrs
  184. //
  185. // Purpose: Calculate Overtime Hours for Each Employee
  186. //
  187. // Parameters:
  188. //
  189. // hours - number of hours worked
  190. //
  191. // Returns: 0.0 if hours <= 40, otherwise hours - 40.0
  192. //
  193. //**************************************************************
  194.  
  195. float calOvertimeHrs (float hours)
  196. {
  197. if (hours > STD_HOURS)
  198. {
  199. return hours - STD_HOURS;
  200. }
  201. else
  202. {
  203. return 0.0f; // return 0
  204. }
  205. }
  206.  
  207. //**************************************************************
  208. // Function: calGrossPay
  209. //
  210. // Purpose: Calculate Gross Pay for Each Employee
  211. //
  212. // Parameters:
  213. //
  214. // hours - number of hours worked
  215. // overtime hours - overtime hours for each employee
  216. // wage rate - hourly rate of each employee
  217. //
  218. // Returns: grossPay - Gross Pay calculated for each employee
  219. //
  220. //**************************************************************
  221.  
  222. float calGrossPay (float hours, float overtimeHrs, float wageRate)
  223. {
  224. float normalHours = hours > STD_HOURS ? STD_HOURS : hours; // calculate normal hours
  225. float normalPay = normalHours * wageRate; // calculate normal pay
  226. float overtimePay = overtimeHrs * wageRate * OT_RATE; // calculate overtime pay
  227. float grossPay = normalPay + overtimePay; //calculate gross pay
  228.  
  229. return grossPay; // return grossPay
  230. }
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   Total
------------------------------------------------

 098401 10.60 51.0 11.0   598.90    671.50
 526488  9.75 42.5  2.5   426.56    481.31
 765349 10.50 37.0  0.0   388.50    436.00
 034645 12.25 45.0  5.0   581.88    644.12
 127615  8.35  0.0  0.0     0.00      8.35