fork download
  1. //********************************************************
  2. //
  3. // Assignment 6 - Structures
  4. //
  5. // Name: Robert Liszka
  6. //
  7. // Class: C Programming, Spring, 2025
  8. //
  9. // Date: March 8. 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 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.  
  50. float calcOT (float hours);
  51.  
  52. float calcGross (float wageRate, float hours, float overtimeHrs);
  53.  
  54. int main ()
  55. {
  56. // Set up a local variable to store the employee information
  57. struct employee employeeData[SIZE] = {
  58. { 98401, 10.60 },
  59. { 526488, 9.75 },
  60. { 765349, 10.50 }, // initialize clock and wage values
  61. { 34645, 12.25 },
  62. { 127615, 8.35 }
  63. };
  64.  
  65. int i; // loop and array index
  66.  
  67. // Call functions as needed to read and calculate information
  68. for (i = 0; i < SIZE; ++i)
  69. {
  70.  
  71. // Prompt for the number of hours worked by the employee
  72. employeeData[i].hours = getHours (employeeData[i].clockNumber);
  73.  
  74. // TODO: Add other function calls as needed to calculate overtime and gross
  75. employeeData[i].overtimeHrs = calcOT (employeeData[i].hours);
  76.  
  77. employeeData[i].grossPay = calcGross (employeeData[i].wageRate,
  78. employeeData[i].hours,
  79. employeeData[i].overtimeHrs);
  80.  
  81. } // for
  82.  
  83. // Print the column headers
  84. printHeader();
  85.  
  86. // print out each employee
  87. for (i = 0; i < SIZE; ++i)
  88. {
  89. printEmp (employeeData[i].clockNumber,
  90. employeeData[i].wageRate,
  91. employeeData[i].hours,
  92. employeeData[i].overtimeHrs,
  93. employeeData[i].grossPay);
  94. }
  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\n");
  145. printf("------------------------------------------------\n");
  146.  
  147. } // printHeader
  148.  
  149.  
  150. //*************************************************************
  151. // Function: printEmp
  152. //
  153. // Purpose: Prints out all the information for an employee
  154. // in a nice and orderly table format.
  155. //
  156. // Parameters:
  157. //
  158. // clockNumber - unique employee ID
  159. // wageRate - hourly wage rate
  160. // hours - Hours worked for the week
  161. // overtimeHrs - overtime hours worked in a week
  162. // grossPay - gross pay for the week
  163. //
  164. // Returns: void
  165. //
  166. //**************************************************************
  167.  
  168. void printEmp (long int clockNumber, float wageRate, float hours,
  169. float overtimeHrs, float grossPay)
  170. {
  171.  
  172. // Print out a single employee
  173. printf("\n %06li %5.2f %4.1f %4.1f %8.2f",
  174. clockNumber, wageRate, hours,
  175. overtimeHrs, grossPay);
  176.  
  177. } // printEmp
  178.  
  179. // TODO: Add your functions here
  180.  
  181. //***************************************************************
  182. //
  183. //Function: calcOT
  184. //
  185. //Purpose: calculates overtime hours of employee using hours and
  186. //STD_HOURS
  187. //
  188. //Parameters: hours // number of hours employee worked
  189. //
  190. //returns: overtimeHrs
  191. //
  192. //
  193. //**************************************************************
  194. float calcOT (float hours)
  195. {
  196.  
  197. float overtimeHrs; // local variable for overtimeHrs
  198.  
  199.  
  200. overtimeHrs = hours - STD_HOURS; // calc overtimeHrs
  201.  
  202.  
  203. return (overtimeHrs); // retunrs overtimeHrs
  204.  
  205. } // calcOT
  206.  
  207. //**************************************************************
  208. //Function: calcGross
  209. //
  210. //Description: uses if statment to check if employee has worked
  211. // overtime or not, then adds normal pay to overtime pay to get
  212. // gross pay which is called back to the calling function.
  213. //
  214. //Parameters: wageRate - amount paid per hour
  215. // hours - number of hours worked
  216. // overtimeHrs - hours of overtime
  217. //
  218. //Returns: grossPay - nomrmal pay and overtime pay added together
  219. //
  220. //**************************************************************
  221. float calcGross (float wageRate, float hours, float overtimeHrs)
  222. {
  223.  
  224. float grossPay; // gross pay to be calculated
  225. float normalPay; // normal pay earned
  226. float overtimePay; // overtime pay earned
  227.  
  228. if (hours > STD_HOURS)
  229. {
  230. // calculate overtime hours
  231. overtimeHrs = hours - STD_HOURS;
  232.  
  233. // calculate normal pay
  234. normalPay = STD_HOURS * wageRate;
  235.  
  236. // calculate overtime pay
  237. overtimePay = overtimeHrs * wageRate * OT_RATE;
  238.  
  239. }
  240. else
  241. {
  242. //they have no overtime hours
  243. overtimeHrs = 0;
  244. overtimePay = 0;
  245.  
  246. // calculate normal pay
  247. normalPay = hours * wageRate;
  248.  
  249. }// for if
  250.  
  251.  
  252. grossPay = normalPay + overtimePay;
  253.  
  254. return (grossPay);
  255. } // calcGross
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 -3.0   388.50
 034645 12.25 45.0  5.0   581.88
 127615  8.35  0.0 -40.0     0.00