fork(1) 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, employeeData[i].hours, employeeData[i].overtimeHrs);
  78.  
  79.  
  80. } // for
  81.  
  82. // Print the column headers
  83. printHeader();
  84.  
  85. // print out each employee
  86. for (i = 0; i < SIZE; ++i)
  87. {
  88. printEmp (employeeData[i].clockNumber,
  89. employeeData[i].wageRate,
  90. employeeData[i].hours,
  91. employeeData[i].overtimeHrs,
  92. employeeData[i].grossPay);
  93. }
  94.  
  95. return(0); // success
  96.  
  97. } // main
  98.  
  99. //**************************************************************
  100. // Function: getHours
  101. //
  102. // Purpose: Obtains input from user, the number of hours worked
  103. // per employee and stores the result in a local variable
  104. // that is passed back to the calling function.
  105. //
  106. // Parameters: clockNumber - The unique employee ID
  107. //
  108. // Returns: hoursWorked - hours worked in a given week
  109. //
  110. //**************************************************************
  111.  
  112. float getHours (long int clockNumber)
  113. {
  114.  
  115. float hoursWorked; // hours worked in a given week
  116.  
  117. // Read in hours for employee
  118. printf("\nEnter hours worked by emp # %06li: ", clockNumber);
  119. scanf ("%f", &hoursWorked);
  120.  
  121. // return hours back to the calling function
  122. return (hoursWorked);
  123.  
  124. } // getHours
  125.  
  126. //**************************************************************
  127. // Function: printHeader
  128. //
  129. // Purpose: Prints the initial table header information.
  130. //
  131. // Parameters: none
  132. //
  133. // Returns: void
  134. //
  135. //**************************************************************
  136.  
  137. void printHeader (void)
  138. {
  139.  
  140. printf ("\n\n*** Pay Calculator ***\n");
  141.  
  142. // print the table header
  143. printf("\nClock# Wage Hours OT Gross\n");
  144. printf("------------------------------------------------\n");
  145.  
  146. } // printHeader
  147.  
  148.  
  149. //*************************************************************
  150. // Function: printEmp
  151. //
  152. // Purpose: Prints out all the information for an employee
  153. // in a nice and orderly table format.
  154. //
  155. // Parameters:
  156. //
  157. // clockNumber - unique employee ID
  158. // wageRate - hourly wage rate
  159. // hours - Hours worked for the week
  160. // overtimeHrs - overtime hours worked in a week
  161. // grossPay - gross pay for the week
  162. //
  163. // Returns: void
  164. //
  165. //**************************************************************
  166.  
  167. void printEmp (long int clockNumber, float wageRate, float hours,
  168. float overtimeHrs, float grossPay)
  169. {
  170.  
  171. // Print out a single employee
  172. printf("\n %06li %5.2f %4.1f %4.1f %8.2f",
  173. clockNumber, wageRate, hours,
  174. overtimeHrs, grossPay);
  175.  
  176. } // printEmp
  177.  
  178. // TODO: Add your functions here
  179.  
  180. //***************************************************************
  181. //
  182. //Function: calcOT
  183. //
  184. //Purpose: calculates overtime hours of employee using hours and
  185. //STD_HOURS
  186. //
  187. //Parameters: hours // number of hours employee worked
  188. //
  189. //returns: overtimeHrs
  190. //
  191. //
  192. //**************************************************************
  193. float calcOT (float hours)
  194. {
  195.  
  196. float overtimeHrs; // local variable for overtimeHrs
  197.  
  198. if (hours > STD_HOURS)
  199. {
  200.  
  201. overtimeHrs = (hours - STD_HOURS); // calc overtimeHrs
  202.  
  203. }// for if
  204. else
  205. {
  206.  
  207. overtimeHrs = 0;
  208.  
  209. }// for else
  210. return (overtimeHrs); // retunrs overtimeHrs
  211.  
  212. } // calcOT
  213.  
  214. //**************************************************************
  215. //Function: calcGross
  216. //
  217. //Description: uses if statment to check if employee has worked
  218. // overtime or not, then adds normal pay to overtime pay to get
  219. // gross pay which is called back to the calling function.
  220. //
  221. //Parameters: wageRate - amount paid per hour
  222. // hours - number of hours worked
  223. // overtimeHrs - hours of overtime
  224. //
  225. //Returns: grossPay - nomrmal pay and overtime pay added together
  226. //
  227. //**************************************************************
  228. float calcGross (float wageRate, float hours, float overtimeHrs)
  229. {
  230.  
  231. float grossPay; // gross pay to be calculated
  232. float normalPay; // normal pay earned
  233. float overtimePay; // overtime pay earned
  234.  
  235. if (hours > STD_HOURS)
  236. {
  237. // calculate overtime hours
  238. overtimeHrs = hours - STD_HOURS;
  239.  
  240. // calculate normal pay
  241. normalPay = STD_HOURS * wageRate;
  242.  
  243. // calculate overtime pay
  244. overtimePay = overtimeHrs * wageRate * OT_RATE;
  245.  
  246. }
  247. else
  248. {
  249. //they have no overtime hours
  250. overtimeHrs = 0;
  251. overtimePay = 0;
  252.  
  253. // calculate normal pay
  254. normalPay = hours * wageRate;
  255.  
  256. }// for if
  257.  
  258.  
  259. grossPay = normalPay + overtimePay; // calculate gross pay
  260.  
  261. return (grossPay);
  262. } // calcGross
Success #stdin #stdout 0s 5280KB
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