fork download
  1. //********************************************************
  2. //
  3. // Assignment 6 - Structures
  4. //
  5. // Name: <Azan Butt>
  6. //
  7. // Class: C Programming, <Spring 2025>
  8. //
  9. // Date: <3/9/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 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. float calculateOvertime(float hours);
  48. float calculateGrossPay(float wageRate, float hours, float overtimeHrs);
  49.  
  50. // complete: Add your other function prototypes here
  51.  
  52. int main ()
  53. {
  54. // Set up a local variable to store the employee information
  55. struct employee employeeData[SIZE] = {
  56. { 98401, 10.60 },
  57. { 526488, 9.75 },
  58. { 765349, 10.50 }, // initialize clock and wage values
  59. { 34645, 12.25 },
  60. { 127615, 8.35 }
  61. };
  62.  
  63. int i; // loop and array index
  64.  
  65. // Call functions as needed to read and calculate information
  66. for (i = 0; i < SIZE; ++i)
  67. {
  68.  
  69. // Prompt for the number of hours worked by the employee
  70. employeeData[i].hours = getHours (employeeData[i].clockNumber);
  71.  
  72. // calculate the overtime hours
  73. employeeData[i].overtimeHrs = calculateOvertime(employeeData[i].hours);
  74.  
  75. //calculate the gross pay
  76. employeeData[i].grossPay = calculateGrossPay(employeeData[i].wageRate,
  77. employeeData[i].hours,
  78. employeeData[i].overtimeHrs);
  79. // complete: Add other function calls as needed to calculate overtime and gross
  80.  
  81. } // for
  82.  
  83. // Print the column headers
  84. printHeader();
  85.  
  86. // print out each employee
  87. for (i = 0; i < SIZE; ++i)
  88. {
  89. printEmp (employeeData[i].clockNumber,
  90. employeeData[i].wageRate,
  91. employeeData[i].hours,
  92. employeeData[i].overtimeHrs,
  93. employeeData[i].grossPay);
  94. }
  95.  
  96. return(0); // success
  97.  
  98. } // main
  99.  
  100. //**************************************************************
  101. // Function: getHours
  102. //
  103. // Purpose: Obtains input from user, the number of hours worked
  104. // per employee and stores the result in a local variable
  105. // that is passed back to the calling function.
  106. //
  107. // Parameters: clockNumber - The unique employee ID
  108. //
  109. // Returns: hoursWorked - hours worked in a given week
  110. //
  111. //**************************************************************
  112.  
  113. float getHours (long int clockNumber)
  114. {
  115.  
  116. float hoursWorked; // hours worked in a given week
  117.  
  118. // Read in hours for employee
  119. printf("\nEnter hours worked by emp # %06li: ", clockNumber);
  120. scanf ("%f", &hoursWorked);
  121.  
  122. // return hours back to the calling function
  123. return (hoursWorked);
  124.  
  125. } // getHours
  126.  
  127. //**************************************************************
  128. // Function: printHeader
  129. //
  130. // Purpose: Prints the initial table header information.
  131. //
  132. // Parameters: none
  133. //
  134. // Returns: void
  135. //
  136. //**************************************************************
  137.  
  138. void printHeader (void)
  139. {
  140.  
  141. printf ("\n\n*** Pay Calculator ***\n");
  142.  
  143. // print the table header
  144. printf("\nClock# Wage Hours OT Gross\n");
  145. printf("------------------------------------------------\n");
  146.  
  147. } // printHeader
  148.  
  149.  
  150. //*************************************************************
  151. // Function: printEmp
  152. //
  153. // Purpose: Prints out all the information for an employee
  154. // in a nice and orderly table format.
  155. //
  156. // Parameters:
  157. //
  158. // clockNumber - unique employee ID
  159. // wageRate - hourly wage rate
  160. // hours - Hours worked for the week
  161. // overtimeHrs - overtime hours worked in a week
  162. // grossPay - gross pay for the week
  163. //
  164. // Returns: void
  165. //
  166. //**************************************************************
  167.  
  168. void printEmp (long int clockNumber, float wageRate, float hours,
  169. float overtimeHrs, float grossPay)
  170. {
  171.  
  172. // Print out a single employee
  173. printf("\n %06li %5.2f %4.1f %4.1f %8.2f",
  174. clockNumber, wageRate, hours,overtimeHrs, grossPay);
  175.  
  176. } // printEmp
  177.  
  178. // complete: Add your functions here
  179. //**************************************************************
  180. // Function: calculateOvertime
  181. //
  182. //Purpose: This calculates the overtime hours needed. hours above the standard 40 hours is Overtime
  183. //
  184. //Parameters: hours - Total hours that is worked by an employee
  185. //
  186. //returns the overtime hours
  187. //**************************************************************
  188.  
  189. float calculateOvertime(float hours)
  190. {
  191. if (hours > STD_HOURS)
  192. return hours - STD_HOURS; // this is overtime hours
  193. return 0.0;
  194. }
  195.  
  196.  
  197. //**************************************************************
  198. //Function: calculate grosspay
  199. //
  200. //purpose Calculates the GrossPay which is Normal pay + overtime pay
  201. //
  202. //Parameters: wagerate - Employee's hourly wage rate'
  203. // hours- the total hours worked
  204. // overtimeHrs - the overtime hours worked
  205. //
  206. //returns: grosspay (normal pay + overtime pay)
  207. //**************************************************************
  208.  
  209. float calculateGrossPay(float wageRate, float hours, float overtimeHrs)
  210. {
  211. float normalPay = wageRate * (hours - overtimeHrs);
  212. float overtimePay = overtimeHrs * wageRate * OT_RATE;
  213. return normalPay + overtimePay;
  214. }
  215.  
  216. //
  217.  
  218.  
Success #stdin #stdout 0.01s 5296KB
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