fork download
  1. //********************************************************
  2. //
  3. // Assignment 6 - Structures
  4. //
  5. // Name: <Isaac Boahndao>
  6. //
  7. // Class: C Programming, <Spring Semester and 2025>
  8. //
  9. // Date: <03_08_25>
  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. #include <stdio.h> // Standard input-output header
  20.  
  21. // Define Constants
  22. #define SIZE 5
  23. #define STD_HOURS 40.0
  24. #define OT_RATE 1.5
  25.  
  26. // Define a structure to hold employee data
  27. struct employee {
  28. long int clockNumber;
  29. float wageRate;
  30. float hours;
  31. float overtimeHrs;
  32. float grossPay;
  33. };
  34.  
  35. // Function Prototypes
  36. float getHours(long int clockNumber);
  37. void printHeader(void);
  38. void printEmp(long int clockNumber, float wageRate, float hours, float overtimeHrs, float grossPay);
  39. void calculateOvertime(struct employee *emp);
  40. void calculateGrossPay(struct employee *emp);
  41.  
  42. int main() {
  43. // Initialize employee data with clock number and wage rate
  44. struct employee employeeData[SIZE] = {
  45. {98401, 10.60, 0, 0, 0},
  46. {526488, 9.75, 0, 0, 0},
  47. {765349, 10.50, 0, 0, 0},
  48. {34645, 12.25, 0, 0, 0},
  49. {127615, 8.35, 0, 0, 0}
  50. };
  51.  
  52. int i; // Loop index
  53.  
  54. // Loop through employees to get hours, calculate overtime & gross pay
  55. for (i = 0; i < SIZE; ++i) {
  56. employeeData[i].hours = getHours(employeeData[i].clockNumber);
  57. calculateOvertime(&employeeData[i]);
  58. calculateGrossPay(&employeeData[i]);
  59. }
  60.  
  61. // Print the column headers
  62. printHeader();
  63.  
  64. // Print employee details
  65. for (i = 0; i < SIZE; ++i) {
  66. printEmp(employeeData[i].clockNumber, employeeData[i].wageRate, employeeData[i].hours, employeeData[i].overtimeHrs, employeeData[i].grossPay);
  67. }
  68.  
  69. return 0; // Program executed successfully
  70. }
  71.  
  72. //**************************************************************
  73. // Function: getHours
  74. // Purpose: Gets user input for hours worked per employee
  75. // Parameters: clockNumber - Unique employee ID
  76. // Returns: hoursWorked - Hours worked in a given week
  77. //**************************************************************
  78. float getHours(long int clockNumber) {
  79. float hoursWorked;
  80. printf("Enter hours worked by emp #%06li: ", clockNumber);
  81. scanf("%f", &hoursWorked);
  82. return hoursWorked;
  83. }
  84.  
  85. //**************************************************************
  86. // Function: calculateOvertime
  87. // Purpose: Calculates overtime hours worked
  88. // Parameters: emp - Pointer to an employee structure
  89. // Returns: void (modifies structure directly)
  90. //**************************************************************
  91. void calculateOvertime(struct employee *emp) {
  92. if (emp->hours > STD_HOURS) {
  93. emp->overtimeHrs = emp->hours - STD_HOURS;
  94. } else {
  95. emp->overtimeHrs = 0.0;
  96. }
  97. }
  98.  
  99. //**************************************************************
  100. // Function: calculateGrossPay
  101. // Purpose: Calculates gross pay for an employee
  102. // Parameters: emp - Pointer to an employee structure
  103. // Returns: void (modifies structure directly)
  104. //**************************************************************
  105. void calculateGrossPay(struct employee *emp) {
  106. float regularPay, overtimePay;
  107.  
  108. if (emp->hours > STD_HOURS) {
  109. regularPay = STD_HOURS * emp->wageRate;
  110. overtimePay = emp->overtimeHrs * emp->wageRate * OT_RATE;
  111. } else {
  112. regularPay = emp->hours * emp->wageRate;
  113. overtimePay = 0.0;
  114. }
  115.  
  116. emp->grossPay = regularPay + overtimePay;
  117. }
  118.  
  119. //**************************************************************
  120. // Function: printHeader
  121. // Purpose: Prints the table header
  122. // Parameters: none
  123. // Returns: void
  124. //**************************************************************
  125. void printHeader(void) {
  126. printf("\n*** Pay Calculator ***\n\n");
  127. printf("Clock# Wage Hours OT Gross\n");
  128. printf("----------------------------------\n");
  129. }
  130.  
  131. //**************************************************************
  132. // Function: printEmp
  133. // Purpose: Prints employee details in a tabular format
  134. // Parameters:
  135. // - clockNumber: Unique employee ID
  136. // - wageRate: Hourly wage rate
  137. // - hours: Hours worked in the week
  138. // - overtimeHrs: Overtime hours worked
  139. // - grossPay: Gross pay for the week
  140. // Returns: void
  141. //**************************************************************
  142. void printEmp(long int clockNumber, float wageRate, float hours, float overtimeHrs, float grossPay) {
  143. printf("%06li %5.2f %5.1f %4.1f %8.2f\n", clockNumber, wageRate, hours, overtimeHrs, grossPay);
  144. }
  145.  
Success #stdin #stdout 0s 5228KB
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