fork download
  1. //********************************************************
  2. //
  3. // Assignment 6 - Structures
  4. //
  5. // Name: <Jacob Gersh>
  6. //
  7. // Class: C Programming, <Spring 2026>
  8. //
  9. // Date: <14 March 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. // TODO: Add your other function prototypes here
  49.  
  50. float calcOT (float hours);
  51. float calcGross (float hours, float overtimeHrs, float wageRate);
  52.  
  53.  
  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. // Call functions as needed to read and calculate information
  69. for (i = 0; i < SIZE; ++i)
  70. {
  71.  
  72. // Prompt for the number of hours worked by the employee
  73. employeeData[i].hours = getHours (employeeData[i].clockNumber);
  74.  
  75. employeeData[i].overtimeHrs = calcOT(employeeData[i].hours); // added this, need to first calculate overtime hours
  76.  
  77. employeeData[i].grossPay = calcGross(
  78. employeeData[i].hours,
  79. employeeData[i].overtimeHrs,
  80. employeeData[i].wageRate);
  81.  
  82. // TODO: Add other function calls as needed to calculate overtime and gross
  83.  
  84. } // for
  85.  
  86. // Print the column headers
  87. printHeader();
  88.  
  89. // print out each employee
  90. for (i = 0; i < SIZE; ++i)
  91. {
  92. printEmp (employeeData[i].clockNumber,
  93. employeeData[i].wageRate,
  94. employeeData[i].hours,
  95. employeeData[i].overtimeHrs,
  96. employeeData[i].grossPay);
  97. }
  98.  
  99. return(0); // success
  100.  
  101. } // main
  102.  
  103. //**************************************************************
  104. // Function: getHours
  105. //
  106. // Purpose: Obtains input from user, the number of hours worked
  107. // per employee and stores the result in a local variable
  108. // that is passed back to the calling function.
  109. //
  110. // Parameters: clockNumber - The unique employee ID
  111. //
  112. // Returns: hoursWorked - hours worked in a given week
  113. //
  114. //**************************************************************
  115.  
  116. float getHours (long int clockNumber)
  117. {
  118.  
  119. float hoursWorked; // hours worked in a given week
  120.  
  121. // Read in hours for employee
  122. printf("\nEnter hours worked by emp # %06li: ", clockNumber);
  123. scanf ("%f", &hoursWorked);
  124.  
  125. // return hours back to the calling function
  126. return (hoursWorked);
  127.  
  128. } // getHours
  129.  
  130. //**************************************************************
  131. // Function: printHeader
  132. //
  133. // Purpose: Prints the initial table header information.
  134. //
  135. // Parameters: none
  136. //
  137. // Returns: void
  138. //
  139. //**************************************************************
  140.  
  141. void printHeader (void)
  142. {
  143.  
  144. printf ("\n\n*** Pay Calculator ***\n");
  145.  
  146. // print the table header
  147. printf("\nClock# Wage Hours OT Gross\n");
  148. printf("------------------------------------------------\n");
  149.  
  150. } // printHeader
  151.  
  152.  
  153. //*************************************************************
  154. // Function: printEmp
  155. //
  156. // Purpose: Prints out all the information for an employee
  157. // in a nice and orderly table format.
  158. //
  159. // Parameters:
  160. //
  161. // clockNumber - unique employee ID
  162. // wageRate - hourly wage rate
  163. // hours - Hours worked for the week
  164. // overtimeHrs - overtime hours worked in a week
  165. // grossPay - gross pay for the week
  166. //
  167. // Returns: void
  168. //
  169. //**************************************************************
  170.  
  171. void printEmp (long int clockNumber, float wageRate, float hours,
  172. float overtimeHrs, float grossPay)
  173. {
  174.  
  175. // Print out a single employee
  176. printf("\n %06li %5.2f %4.1f %4.1f %8.2f",
  177. clockNumber, wageRate, hours,
  178. overtimeHrs, grossPay);
  179.  
  180. } // printEmp
  181.  
  182. // TODO: Add your functions here
  183.  
  184.  
  185. //*************************************************************
  186. // Function: calcOT
  187. //
  188. // Purpose: calculates hours over 40
  189. //
  190. //**************************************************************
  191.  
  192. float calcOT(float hours) {
  193. if (hours > STD_HOURS)
  194. return hours - STD_HOURS;
  195. else return 0.0f;}
  196.  
  197. //**************************************************************
  198. // Function: calcGross //
  199. // Purpose: Calculates gross pay including overtime
  200. //**************************************************************
  201. float calcGross(float hours, float overtimeHrs, float wageRate) {
  202. float regularHrs = hours - overtimeHrs;
  203. float gross = (regularHrs * wageRate) + (overtimeHrs * wageRate * OT_RATE);
  204. return gross;
  205. }
Success #stdin #stdout 0s 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