fork(1) download
  1. //********************************************************
  2. //
  3. // Assignment 6 - Structures
  4. //
  5. // Name: John Eneh
  6. //
  7. // Class: C Programming, Fall 2025
  8. //
  9. // Date: 10/18/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. // Same prototypes as last week's assignment
  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. // calcOT for overtime. Adapted from last week
  73. employeeData[i].overtimeHrs = calcOT (employeeData[i].hours);
  74.  
  75. // calcGross for gross pay. Adapted from last week
  76. employeeData[i].grossPay = calcGross (employeeData[i].wageRate,
  77. 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: calcOT
  127. //
  128. // Purpose: takes the value of hours and determines if they
  129. // exceed the 40 hours of the stantdard work week
  130. // and then passes the overtime back to the calling function.
  131. //
  132. // Parameters:
  133. // hours - Hours worked for the week
  134. // STD_HOURS - 40 hours in the work week
  135. //
  136. // Returns:
  137. // overtime hours - the calculated hours beyond 40. Returns
  138. // 0 if hours do not exceed 40.
  139. //
  140. //**************************************************************
  141.  
  142. float calcOT (float hours)
  143. {
  144. if (hours > STD_HOURS)
  145. {
  146. return (hours - STD_HOURS);
  147. }
  148. else
  149. {
  150. return (0);
  151. }
  152.  
  153. } // calcOT
  154. //**************************************************************
  155. // Function: calcGross
  156. //
  157. // Purpose: calculates the gross pay using the employees'
  158. // wageRate, hours worked, and OT hours if any are available.
  159. // Passes the calculated the amount to main.
  160. //
  161. // Parameters:
  162. // wageRate - Employees' payrate
  163. // hours - Hours worked for the week
  164. // STD_HOURS - 40 hours in the work week
  165. // overtimeHrs - Hours worked beyond 40 hours
  166. //
  167. // Returns: gross pay - normal pay + available OT pay
  168. //
  169. //**************************************************************
  170.  
  171. float calcGross (float wageRate, float hours, float overtimeHrs)
  172. {
  173. if (hours > STD_HOURS)
  174. {
  175. return ((STD_HOURS * wageRate) + (overtimeHrs * wageRate * OT_RATE));
  176. }
  177. else
  178. {
  179. return (wageRate * hours);
  180. }
  181. } // calcGross
  182. //**************************************************************
  183. // Function: printHeader
  184. //
  185. // Purpose: Prints the initial table header information.
  186. //
  187. // Parameters: none
  188. //
  189. // Returns: void
  190. //
  191. //**************************************************************
  192.  
  193. void printHeader (void)
  194. {
  195.  
  196. printf ("\n\n*** Pay Calculator ***\n");
  197.  
  198. // print the table header
  199. printf("\nClock# Wage Hours OT Gross\n");
  200. printf("------------------------------------------------\n");
  201.  
  202. } // printHeader
  203.  
  204.  
  205. //*************************************************************
  206. // Function: printEmp
  207. //
  208. // Purpose: Prints out all the information for an employee
  209. // in a nice and orderly table format.
  210. //
  211. // Parameters:
  212. //
  213. // clockNumber - unique employee ID
  214. // wageRate - hourly wage rate
  215. // hours - Hours worked for the week
  216. // overtimeHrs - overtime hours worked in a week
  217. // grossPay - gross pay for the week
  218. //
  219. // Returns: void
  220. //
  221. //**************************************************************
  222.  
  223. void printEmp (long int clockNumber, float wageRate, float hours,
  224. float overtimeHrs, float grossPay)
  225. {
  226.  
  227. // Print out a single employee
  228. printf("\n %06li %5.2f %4.1f %4.1f %8.2f",
  229. clockNumber, wageRate, hours,
  230. overtimeHrs, grossPay);
  231.  
  232. } // printEmp
  233.  
Success #stdin #stdout 0s 5324KB
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