fork download
  1. //********************************************************
  2. //
  3. // Assignment 6 - Structures
  4. //
  5. // Name: Matthew Chen
  6. //
  7. // Class: C Programming, Fall 2026
  8. //
  9. // Date: 10/12/2026
  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. float calculateOvertime(float hours);
  49. float calculateGrossPay(float wageRate, float hours, float overtimeHrs);
  50. int main ()
  51. {
  52. // Set up a local variable to store the employee information
  53. struct employee employeeData[SIZE] = {
  54. { 98401, 10.60 },
  55. { 526488, 9.75 },
  56. { 765349, 10.50 }, // initialize clock and wage values
  57. { 34645, 12.25 },
  58. { 127615, 8.35 }
  59. };
  60.  
  61. int i; // loop and array index
  62.  
  63. // Call functions as needed to read and calculate information
  64. for (i = 0; i < SIZE; ++i)
  65. {
  66.  
  67. // Prompt for the number of hours worked by the employee
  68. employeeData[i].hours = getHours (employeeData[i].clockNumber);
  69.  
  70. employeeData[i].overtimeHrs =
  71. calculateOvertime(employeeData[i].hours);
  72.  
  73. employeeData[i].grossPay =
  74. calculateGrossPay(
  75. employeeData[i].wageRate,
  76. employeeData[i].hours,
  77. 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: printHeader
  127. //
  128. // Purpose: Prints the initial table header information.
  129. //
  130. // Parameters: none
  131. //
  132. // Returns: void
  133. //
  134. //**************************************************************
  135.  
  136. void printHeader (void)
  137. {
  138.  
  139. printf ("\n\n*** Pay Calculator ***\n");
  140.  
  141. // print the table header
  142. printf("\nClock# Wage Hours OT Gross\n");
  143. printf("------------------------------------------------\n");
  144.  
  145. } // printHeader
  146.  
  147.  
  148. //*************************************************************
  149. // Function: printEmp
  150. //
  151. // Purpose: Prints out all the information for an employee
  152. // in a nice and orderly table format.
  153. //
  154. // Parameters:
  155. //
  156. // clockNumber - unique employee ID
  157. // wageRate - hourly wage rate
  158. // hours - Hours worked for the week
  159. // overtimeHrs - overtime hours worked in a week
  160. // grossPay - gross pay for the week
  161. //
  162. // Returns: void
  163. //
  164. //**************************************************************
  165.  
  166. void printEmp (long int clockNumber, float wageRate, float hours,
  167. float overtimeHrs, float grossPay)
  168. {
  169.  
  170. // Print out a single employee
  171. printf("\n %06li %5.2f %4.1f %4.1f %8.2f",
  172. clockNumber, wageRate, hours,
  173. overtimeHrs, grossPay);
  174.  
  175. } // printEmp
  176.  
  177. float calculateOvertime(float hours)
  178. {
  179. if (hours > STD_HOURS)
  180. return hours - STD_HOURS;
  181. else
  182. return 0.0;
  183. }
  184.  
  185. // Calculate gross pay, including overtime
  186. float calculateGrossPay(float wageRate, float hours,
  187. float overtimeHrs)
  188. {
  189. float normalHours;
  190. float normalPay;
  191. float overtimePay;
  192.  
  193. if (hours > STD_HOURS)
  194. normalHours = STD_HOURS;
  195. else
  196. normalHours = hours;
  197.  
  198. normalPay = normalHours * wageRate;
  199. overtimePay = overtimeHrs * wageRate * OT_RATE;
  200.  
  201. return normalPay + overtimePay;
  202. }
Success #stdin #stdout 0.01s 5304KB
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