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