fork download
  1. //********************************************************
  2. //
  3. // Assignment 6 - Structures
  4. //
  5. // Name: Mamadou Koita
  6. //
  7. // Class: C Programming, Spring 2025
  8. //
  9. // Date: March 08,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. struct employee
  30. {
  31. long int clockNumber;
  32. float wageRate;
  33. float hours;
  34. float overtimeHrs;
  35. float grossPay;
  36. };
  37.  
  38. // Function prototypes
  39. float getHours(long int clockNumber);
  40. void printHeader(void);
  41. void printEmp(long int clockNumber, float wageRate, float hours, float overtimeHrs, float grossPay);
  42. void calculateOvertimeAndPay(struct employee *emp);
  43.  
  44. int main()
  45. {
  46. // Set up a local variable to store the employee information
  47. struct employee employeeData[SIZE] = {
  48. {98401, 10.60},
  49. {526488, 9.75},
  50. {765349, 10.50},
  51. {34645, 12.25},
  52. {127615, 8.35}
  53. };
  54.  
  55. int i;
  56.  
  57. // Call functions as needed to read and calculate information
  58. for (i = 0; i < SIZE; ++i)
  59. {
  60. // Prompt for the number of hours worked by the employee
  61. employeeData[i].hours = getHours(employeeData[i].clockNumber);
  62.  
  63. // Calculate overtime and gross pay for the employee
  64. calculateOvertimeAndPay(&employeeData[i]);
  65. }
  66.  
  67. // Print the column headers
  68. printHeader();
  69.  
  70. // Print out each employee's details
  71. for (i = 0; i < SIZE; ++i)
  72. {
  73. printEmp(employeeData[i].clockNumber,
  74. employeeData[i].wageRate,
  75. employeeData[i].hours,
  76. employeeData[i].overtimeHrs,
  77. employeeData[i].grossPay);
  78. }
  79.  
  80. return 0; // success
  81. } // main
  82.  
  83. //**************************************************************
  84. // Function: getHours
  85. // Purpose: Obtains input from user, the number of hours worked
  86. // per employee and stores the result in a local variable
  87. // that is passed back to the calling function.
  88. // Parameters: clockNumber - The unique employee ID
  89. // Returns: hoursWorked - hours worked in a given week
  90. //**************************************************************
  91.  
  92. float getHours(long int clockNumber)
  93. {
  94. float hoursWorked; // hours worked in a given week
  95.  
  96. // Read in hours for employee
  97. printf("\nEnter hours worked by emp # %06li: ", clockNumber);
  98. scanf("%f", &hoursWorked);
  99.  
  100. // return hours back to the calling function
  101. return hoursWorked;
  102. }
  103.  
  104. //**************************************************************
  105. // Function: printHeader
  106. // Purpose: Prints the initial table header information.
  107. // Parameters: none
  108. // Returns: void
  109. //**************************************************************
  110.  
  111. void printHeader(void)
  112. {
  113. printf("\n\n*** Pay Calculator ***\n");
  114.  
  115. // print the table header
  116. printf("\nClock# Wage Hours OT Gross\n");
  117. printf("------------------------------------------------\n");
  118. }
  119.  
  120. //*************************************************************
  121. // Function: printEmp
  122. // Purpose: Prints out all the information for an employee
  123. // in a nice and orderly table format.
  124. // Parameters:
  125. // clockNumber - unique employee ID
  126. // wageRate - hourly wage rate
  127. // hours - Hours worked for the week
  128. // overtimeHrs - overtime hours worked in a week
  129. // grossPay - gross pay for the week
  130. // Returns: void
  131. //*************************************************************
  132.  
  133. void printEmp(long int clockNumber, float wageRate, float hours,
  134. float overtimeHrs, float grossPay)
  135. {
  136. // Print out a single employee
  137. printf("\n %06li %5.2f %4.1f %4.1f %8.2f",
  138. clockNumber, wageRate, hours,
  139. overtimeHrs, grossPay);
  140. }
  141.  
  142. //*************************************************************
  143. // Function: calculateOvertimeAndPay
  144. // Purpose: Calculates the overtime hours worked and gross pay
  145. // for an employee. Updates the overtimeHrs and grossPay fields.
  146. // Parameters: emp - Pointer to employee data structure
  147. // Returns: void
  148. //*************************************************************
  149.  
  150. void calculateOvertimeAndPay(struct employee *emp)
  151. {
  152. if (emp->hours > STD_HOURS)
  153. {
  154. // Calculate overtime hours
  155. emp->overtimeHrs = emp->hours - STD_HOURS;
  156. // Calculate gross pay
  157. emp->grossPay = (STD_HOURS * emp->wageRate) + (emp->overtimeHrs * emp->wageRate * OT_RATE);
  158. }
  159. else
  160. {
  161. emp->overtimeHrs = 0;
  162. emp->grossPay = emp->hours * emp->wageRate;
  163. }
  164. }
Success #stdin #stdout 0s 5280KB
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