fork download
  1. //********************************************************
  2. //
  3. // Assignment 6 - Structures
  4. //
  5. // Name: Hamza Naseer
  6. //
  7. // Class: C Programming, Spring 2025
  8. //
  9. // Date: 3/7/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.  
  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 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.  
  44. void getHours (struct employee employeeData[], int theSize );
  45. void printHeader (void);
  46. void printEmp (struct employee emp [ ], int theSize);
  47. void calculateOvertime(struct employee employeeData[], int theSize);
  48. void calculateGrossPay(struct employee employeeData[], int theSize);
  49.  
  50.  
  51. int main ()
  52. {
  53. // Set up a local variable and initialize the clock and wages of my employees
  54. struct employee employeeData [SIZE] = {
  55. { 98401, 10.60 },
  56. { 526488, 9.75 },
  57. { 765349, 10.50 },
  58. { 34645, 12.25 },
  59. { 127615, 8.35 }
  60. };
  61.  
  62. // Call function needed to read hours
  63. getHours (employeeData, SIZE);
  64.  
  65. // Call functions calculate ot hours and gross pay
  66. calculateOvertime(employeeData, SIZE);
  67. calculateGrossPay(employeeData, SIZE);
  68.  
  69.  
  70. // Print a table header
  71. printHeader();
  72.  
  73. // Function call to output results to the screen in table format
  74. printEmp (employeeData, SIZE);
  75.  
  76. return(0); // success
  77.  
  78. } // main
  79.  
  80. //**************************************************************
  81. // Function: getHours
  82. //
  83. // Purpose: Obtains input from user, the number of hours worked
  84. // per employee and stores the result in an array of structures
  85. // that is passed back to the calling function by reference.
  86. //
  87. // Parameters:
  88. //
  89. // employeeData - an array of structures containing Employees
  90. // theSize - number of employees to process
  91. //
  92. // Returns: Nothing (void)
  93. //
  94. //**************************************************************
  95.  
  96. void getHours (struct employee employeeData[], int theSize )
  97. {
  98.  
  99. int i; // loop and array index
  100.  
  101. // read hours in for each employee
  102. for (i = 0; i < theSize ; ++i)
  103. {
  104. printf("\nEnter hours worked by emp # %06li: ",
  105. employeeData[i].clockNumber);
  106. scanf ("%f", &employeeData[i].hours);
  107. } // for
  108.  
  109. } // getHours
  110.  
  111. //**************************************************************
  112. // Function: printHeader
  113. //
  114. // Purpose: Prints the initial table header information.
  115. //
  116. // Parameters: none
  117. //
  118. // Returns: void
  119. //
  120. //**************************************************************
  121.  
  122. void printHeader (void)
  123. {
  124.  
  125. printf ("\n\n*** Pay Calculator ***\n");
  126.  
  127. // print the table header
  128. printf("\nClock# Wage Hours OT Gross\n");
  129. printf("------------------------------------------------\n");
  130.  
  131. } // printHeader
  132.  
  133. // ********************************************************************
  134. // Function: printEmp
  135. //
  136. // Purpose: Outputs to screen in a table format the following
  137. // information about an employee: Clock, Wage,
  138. // Hours, Overtime Hours, and Gross Pay.
  139. //
  140. // Parameters:
  141. //
  142. // employeeData - an array of structures containing Employees
  143. // theSize - number of employees to process
  144. //
  145. // Returns: Nothing (void)
  146. //
  147. // *********************************************************************
  148.  
  149. void printEmp ( struct employee employeeData[], int theSize )
  150. {
  151. int i; // loop and array index
  152.  
  153. // print information about each employee
  154. for (i = 0; i < theSize ; ++i)
  155. {
  156. printf("\n %06li %5.2f %4.1f %4.1f %8.2f",
  157. employeeData[i].clockNumber, employeeData[i].wageRate, employeeData[i].hours,
  158. employeeData[i].overtimeHrs, employeeData[i].grossPay);
  159. } /* for */
  160.  
  161. } // printEmp
  162.  
  163. //********************************************************
  164. // Function: calculateOvertime
  165. //
  166. // Purpose: This function will calculate the overtime for each employee. Anything above 40 hours is overtime.
  167. //
  168. // Parameters:
  169. //
  170. // employeeData - an array of structures containing Employees
  171. // theSize - number of employees to process
  172. //
  173. // Returns: Nothing
  174. //
  175. //**************************************************************
  176. void calculateOvertime(struct employee employeeData[], int theSize)
  177. {
  178. int i; // loop and array index
  179.  
  180. for (i = 0; i < theSize; ++i)
  181. {
  182. // If hours worked are greater than standard hours, calculate overtime
  183. if (employeeData[i].hours > STD_HOURS)
  184. {
  185. employeeData[i].overtimeHrs = employeeData[i].hours - STD_HOURS;
  186. }
  187. else
  188. {
  189. employeeData[i].overtimeHrs = 0.0; // No overtime
  190. }
  191. }
  192. } // calculateOvertime
  193.  
  194.  
  195. //********************************************************
  196. // Function: calculateGrossPay
  197. //
  198. // Purpose: Calculates the total pay for each employee..
  199. //
  200. // Parameters:
  201. //
  202. // employeeData - an array of structures containing Employees
  203. // theSize - number of employees to process
  204. //
  205. // Returns: Nothing (void)
  206. //
  207. //**************************************************************
  208. void calculateGrossPay(struct employee employeeData[], int theSize)
  209. {
  210. int i;
  211.  
  212. for (i = 0; i < theSize; ++i)
  213. {
  214. //regular pay based off of standard 40 hours
  215. float regularPay = STD_HOURS * employeeData[i].wageRate;
  216.  
  217. // Overtime pay is based on the overtime hours and Overtime rate
  218. float overtimePay = employeeData[i].overtimeHrs * employeeData[i].wageRate * OT_RATE;
  219.  
  220. // Gross pay is the sum of regular pay and overtime pay
  221. employeeData[i].grossPay = regularPay + overtimePay;
  222. }
  223. } // calculateGrossPay
Success #stdin #stdout 0.01s 5292KB
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   420.00
 034645 12.25 45.0  5.0   581.88
 127615  8.35  0.0  0.0   334.00