fork download
  1. //********************************************************
  2. //
  3. // Assignment 6 - Structures
  4. //
  5. // Name: Joy Akoso
  6. //
  7. // Class: C Programming, <Fall 2025>
  8. //
  9. // Date: <10/17/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 reference design
  16. //
  17. //********************************************************
  18.  
  19. // Define and Includes
  20. #include <stdio.h>
  21.  
  22. // Define Constants
  23. #define SIZE 5
  24. #define STD_HOURS 40.0
  25. #define OT_RATE 1.5
  26.  
  27. // Define a global structure to pass employee data between functions
  28. // Note that the structure type is global, but you don't want a variable
  29. // of that type to be global. Best to declare a variable of that type
  30. // in a function like main or another function and pass as needed.
  31.  
  32. struct employee
  33. {
  34. long clockNumber;
  35. float wageRate;
  36. float hours;
  37. float overtimeHrs;
  38. float grossPay;
  39. };
  40.  
  41. // define prototypes here for each function except main
  42. void getHours(struct employee employeeData[], int theSize);
  43. void printHeader(void);
  44. void printEmp(struct employee emp[], int theSize);
  45.  
  46. // TODO: add your function prototypes here
  47. void calcOvertime(struct employee employeeData[], int theSize);
  48. void calcGrossPay(struct employee employeeData[], int theSize);
  49.  
  50. int main()
  51. {
  52. // Set up a local variable and initialize the clock and wages of my employees
  53. struct employee employeeData[SIZE] = {
  54. {98401, 10.60},
  55. {526488, 9.75},
  56. {765349, 10.50},
  57. {34645, 12.25},
  58. {127615, 8.35}};
  59.  
  60. // Call function needed to read hours
  61. getHours(employeeData, SIZE);
  62.  
  63. // TODO: Call functions calculate ot hours and gross pay
  64. calcOvertime(employeeData, SIZE);
  65. calcGrossPay(employeeData, SIZE);
  66.  
  67. // TODO: Call function to print the table column headers
  68. printHeader();
  69.  
  70. // Function call to output results to the screen in table format
  71. printEmp(employeeData, SIZE);
  72.  
  73. return (0); // success
  74. } // main
  75.  
  76. //**************************************************************
  77. // Function: getHours
  78. //
  79. // Purpose: Obtains input from user, the number of hours worked
  80. // per employee and stores the result in an array of structures
  81. // that is passed back to the calling function by reference.
  82. //
  83. // Parameters:
  84. //
  85. // employeeData - an array of structures containing Employees
  86. // theSize - number of employees to process
  87. //
  88. // Returns: Nothing (void)
  89. //
  90. //**************************************************************
  91.  
  92. void getHours(struct employee employeeData[], int theSize)
  93. {
  94. int i; // loop and array index
  95.  
  96. // read hours in for each employee
  97. for (i = 0; i < theSize; ++i)
  98. {
  99. printf("\nEnter hours worked by emp # %06li: ",
  100. employeeData[i].clockNumber);
  101. scanf("%f", &employeeData[i].hours);
  102. } // for
  103.  
  104. } // getHours
  105.  
  106. //**************************************************************
  107. // Function: printHeader
  108. //
  109. // Purpose: Prints the initial table header information.
  110. //
  111. // Parameters: none
  112. //
  113. // Returns: void
  114. //
  115. //**************************************************************
  116.  
  117. void printHeader(void)
  118. {
  119. printf("\n\n*** Pay Calculator ***\n");
  120.  
  121. // print the table header
  122. printf("\nClock# Wage Hours OT Gross\n");
  123. printf("----------------------------------------\n");
  124.  
  125. } // printHeader
  126.  
  127. // ********************************************************************
  128. // Function: printEmp
  129. //
  130. // Purpose: Outputs to screen in a table format the following
  131. // information about an employee: Clock, Wage,
  132. // Hours, Overtime Hours, and Gross Pay.
  133. //
  134. // Parameters:
  135. //
  136. // employeeData - an array of structures containing Employees
  137. // theSize - number of employees to process
  138. //
  139. // Returns: Nothing (void)
  140. //
  141. // *********************************************************************
  142.  
  143. void printEmp(struct employee employeeData[], int theSize)
  144. {
  145. int i; // loop and array index
  146.  
  147. // print information about each employee
  148. for (i = 0; i < theSize; ++i)
  149. {
  150. printf("%06li %5.2f %6.1f %5.1f %8.2f\n",
  151. employeeData[i].clockNumber,
  152. employeeData[i].wageRate,
  153. employeeData[i].hours,
  154. employeeData[i].overtimeHrs,
  155. employeeData[i].grossPay);
  156. }
  157.  
  158. } // printEmp
  159.  
  160. //**************************************************************
  161. // Function: calcOvertime
  162. //
  163. // Purpose: Calculates the overtime hours for each employee
  164. //
  165. // Parameters:
  166. // employeeData - array of employee structures
  167. // theSize - number of employees
  168. //
  169. // Returns: Nothing (updates structure by reference)
  170. //**************************************************************
  171.  
  172. void calcOvertime(struct employee employeeData[], int theSize)
  173. {
  174. int i;
  175. for (i = 0; i < theSize; ++i)
  176. {
  177. if (employeeData[i].hours > STD_HOURS)
  178. employeeData[i].overtimeHrs = employeeData[i].hours - STD_HOURS;
  179. else
  180. employeeData[i].overtimeHrs = 0.0;
  181. }
  182. } // calcOvertime
  183.  
  184. //**************************************************************
  185. // Function: calcGrossPay
  186. //
  187. // Purpose: Calculates the total gross pay (normal + overtime pay)
  188. //
  189. // Parameters:
  190. // employeeData - array of employee structures
  191. // theSize - number of employees
  192. //
  193. // Returns: Nothing (updates structure by reference)
  194. //**************************************************************
  195.  
  196. void calcGrossPay(struct employee employeeData[], int theSize)
  197. {
  198. int i;
  199. for (i = 0; i < theSize; ++i)
  200. {
  201. if (employeeData[i].hours > STD_HOURS)
  202. {
  203. employeeData[i].grossPay = (STD_HOURS * employeeData[i].wageRate) +
  204. (employeeData[i].overtimeHrs * employeeData[i].wageRate * OT_RATE);
  205. }
  206. else
  207. {
  208. employeeData[i].grossPay = employeeData[i].hours * employeeData[i].wageRate;
  209. }
  210. }
  211. } // calcGrossPay
  212.  
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