fork download
  1. //********************************************************
  2. //
  3. // Assignment 6 - Structures
  4. //
  5. // Name: Matt Smith
  6. //
  7. // Class: C Programming, Spring 2025
  8. //
  9. // Date:
  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. #define OVERTIME_RATE 1.5f
  28. #define STD_WORK_WEEK 40.0f
  29.  
  30. // Define a global structure to pass employee data between functions
  31. // Note that the structure type is global, but you don't want a variable
  32. // of that type to be global. Best to declare a variable of that type
  33. // in a function like main or another function and pass as needed.
  34.  
  35. struct employee
  36. {
  37. long int clockNumber;
  38. float wageRate;
  39. float hours;
  40. float overtimeHrs;
  41. float grossPay;
  42. };
  43.  
  44. // define prototypes here for each function except main
  45. float getHours (long int clockNumber);
  46. void printHeader (void);
  47. void printEmp (long int clockNumber, float wageRate, float hours,
  48. float overtimeHrs, float grossPay);
  49.  
  50. // TODO: Add your other function prototypes here
  51. float calcOvertimeHrs (float hoursWorked, float stdWorkWeek);
  52. float calcGrossPay (float normalPay, float overtimeHours, float wageRate, float OTRate,
  53. float stdWorkWeek);
  54.  
  55. int main ()
  56. {
  57. // Set up a local variable to store the employee information
  58. struct employee employeeData[SIZE] = {
  59. { 98401, 10.60 },
  60. { 526488, 9.75 },
  61. { 765349, 10.50 }, // initialize clock and wage values
  62. { 34645, 12.25 },
  63. { 127615, 8.35 }
  64. };
  65.  
  66. int i; // loop and array index
  67.  
  68. float overtimeHrs[SIZE]; // overtime hours
  69. float hours[SIZE]; // hours worked in a given week
  70. float grossPay[SIZE]; // gross pay
  71. float wageRate[SIZE] = {10.60,9.75,10.50,12.25,8.35}; // hourly wage rate
  72.  
  73. // Call functions as needed to read and calculate information
  74. for (i = 0; i < SIZE; ++i)
  75. {
  76.  
  77. // Prompt for the number of hours worked by the employee
  78. employeeData[i].hours = getHours (employeeData[i].clockNumber);
  79.  
  80. // Function call to calculate overtime hours
  81. // Calculates number of hours, not overtime pay
  82. employeeData[i].overtimeHrs = calcOvertimeHrs(employeeData[i].hours, STD_WORK_WEEK);
  83.  
  84. // Function call to calculate gross pay
  85. // (float normalPay, float overtimeHours, float wageRate, float OTRate)
  86. employeeData[i].grossPay = calcGrossPay (employeeData[i].hours, employeeData[i].overtimeHrs, employeeData[i].wageRate, OVERTIME_RATE, STD_WORK_WEEK);
  87.  
  88. } // for
  89.  
  90. // Print the column headers
  91. printHeader();
  92.  
  93. // print out each employee
  94. for (i = 0; i < SIZE; ++i)
  95. {
  96. printEmp (employeeData[i].clockNumber,
  97. employeeData[i].wageRate,
  98. employeeData[i].hours,
  99. employeeData[i].overtimeHrs,
  100. employeeData[i].grossPay);
  101. }
  102.  
  103. return(0); // success
  104.  
  105. } // main
  106.  
  107. //**************************************************************
  108. // Function: getHours
  109. //
  110. // Purpose: Obtains input from user, the number of hours worked
  111. // per employee and stores the result in a local variable
  112. // that is passed back to the calling function.
  113. //
  114. // Parameters: clockNumber - The unique employee ID
  115. //
  116. // Returns: hoursWorked - hours worked in a given week
  117. //
  118. //**************************************************************
  119.  
  120. float getHours (long int clockNumber)
  121. {
  122.  
  123. float hoursWorked; // hours worked in a given week
  124.  
  125. // Read in hours for employee
  126. printf("\nEnter hours worked by emp # %06li: ", clockNumber);
  127. scanf ("%f", &hoursWorked);
  128.  
  129. // return hours back to the calling function
  130. return (hoursWorked);
  131.  
  132. } // getHours
  133.  
  134. //**************************************************************
  135. // Function: printHeader
  136. //
  137. // Purpose: Prints the initial table header information.
  138. //
  139. // Parameters: none
  140. //
  141. // Returns: void
  142. //
  143. //**************************************************************
  144.  
  145. void printHeader (void)
  146. {
  147.  
  148. printf ("\n\n*** Pay Calculator ***\n");
  149.  
  150. // print the table header
  151. printf("\nClock# Wage Hours OT Gross\n");
  152. printf("------------------------------------------------\n");
  153.  
  154. } // printHeader
  155.  
  156. //*************************************************************
  157. // Function: printEmp
  158. //
  159. // Purpose: Prints out all the information for an employee
  160. // in a nice and orderly table format.
  161. //
  162. // Parameters:
  163. //
  164. // clockNumber - unique employee ID
  165. // wageRate - hourly wage rate
  166. // hours - Hours worked for the week
  167. // overtimeHrs - overtime hours worked in a week
  168. // grossPay - gross pay for the week
  169. //
  170. // Returns: void
  171. //
  172. //**************************************************************
  173.  
  174. void printEmp (long int clockNumber, float wageRate, float hours,
  175. float overtimeHrs, float grossPay)
  176. {
  177.  
  178. // Print out a single employee
  179. printf("\n %06li %5.2f %4.1f %4.1f %8.2f",
  180. clockNumber, wageRate, hours,
  181. overtimeHrs, grossPay);
  182.  
  183. } // printEmp
  184.  
  185. //**************************************************************
  186. // Function: calcOvertimeHrs
  187. //
  188. // Purpose: Calculates overtime hours for an employee. This returns
  189. // the amount of hours, not the pay.
  190. //
  191. // Parameters:
  192. // hoursWorked - number of hours worked by employee
  193. // stdWorkWeek - hours in a normal workweek
  194. //
  195. // Returns: float total hours of overtime worked
  196. //
  197. //**************************************************************
  198. float calcOvertimeHrs (float hoursWorked, float stdWorkWeek) {
  199. // return calculation without writing a variable
  200. // if more than the standard workweek, return the hours
  201. // if less, then just return zero
  202.  
  203. if (hoursWorked > stdWorkWeek){
  204. return (hoursWorked - stdWorkWeek);
  205. }
  206.  
  207. else {
  208. return (0);
  209. }
  210. }
  211.  
  212. //**************************************************************
  213. // Function: calcGrossPay
  214. //
  215. // Purpose: calculates gross pay for an employee
  216. //
  217. // Parameters:
  218. // normalHours - hours worked in a workweek
  219. // overtimeHours - the hours over the normal hours worked
  220. // wageRate - the normal hourly rate
  221. // OTRate - the overtime hourly rate
  222. // stdWorkWeek - amount of hours in a normal workweek
  223. //
  224. // Returns: float - gross pay amount
  225. //
  226. //**************************************************************
  227. float calcGrossPay (float normalHours, float overtimeHours, float wageRate,
  228. float OTRate, float stdWorkWeek) {
  229. if (normalHours > stdWorkWeek) {
  230. return ((stdWorkWeek * wageRate) + (overtimeHours * wageRate * OTRate));
  231. }
  232. else {
  233. return (normalHours * wageRate);
  234. }
  235. }
  236.  
  237.  
Success #stdin #stdout 0s 5280KB
stdin
Standard input is empty
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  0.0  0.0     0.00
 526488  9.75  0.0  0.0     0.00
 765349 10.50  0.0  0.0     0.00
 034645 12.25  0.0  0.0     0.00
 127615  8.35  0.0  0.0     0.00