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