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