fork download
  1. //********************************************************
  2. //
  3. // Assignment 6 - Structures
  4. //
  5. // Name: Megan Tetreault
  6. //
  7. // Class: C Programming, Spring 2025
  8. //
  9. // Date: March 4, 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. #include <stdio.h>
  20.  
  21. // Define Constants
  22. #define SIZE 5
  23. #define STD_HOURS 40.0
  24. #define OT_RATE 1.5
  25.  
  26. // Define a global structure to pass employee data between functions
  27. // Note that the structure type is global, but you don't want a variable
  28. // of that type to be global. Best to declare a variable of that type
  29. // in a function like main or another function and pass as needed.
  30. struct employee {
  31. long int clockNumber;
  32. float wageRate;
  33. float hours;
  34. float overtimeHrs;
  35. float grossPay;
  36. };
  37.  
  38. // define prototypes here for each function except main
  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. // TODO: add your other function prototypes here
  43. float calcOT(float hours);
  44. float calcGross(float wageRate, float hours, float overtimeHrs);
  45.  
  46. int main()
  47. {
  48. //st up a local variable to store the employee information
  49. struct employee employeeData[SIZE] = {
  50. { 98401, 10.60 },
  51. { 526488, 9.75 },
  52. { 765349, 10.50 }, // initialize clock and wage values
  53. { 34645, 12.25 },
  54. { 127615, 8.35 }
  55. };
  56.  
  57. int i; // loop and array index
  58.  
  59. //Call functions as needed to read and calculate information
  60. for (i = 0; i < SIZE; ++i)
  61. {
  62.  
  63. //Prompt for the number hours worked by the employee
  64. employeeData[i].hours = getHours(employeeData[i].clockNumber);
  65. //Calculate Overtime
  66. employeeData[i].overtimeHrs = calcOT(employeeData[i].hours);
  67. //Calculate Grosspay
  68. employeeData[i].grossPay = calcGross(employeeData[i].wageRate, employeeData[i].hours, employeeData[i].overtimeHrs);
  69. }// for
  70.  
  71. //Print the column headers
  72. printHeader();
  73.  
  74. //print out each employee
  75. for (i = 0; i < SIZE; ++i) {
  76. printEmp(employeeData[i].clockNumber, employeeData[i].wageRate, employeeData[i].hours, employeeData[i].overtimeHrs, employeeData[i].grossPay);
  77. }
  78.  
  79. return 0; //success
  80. } //main
  81.  
  82. //**************************************************************
  83. // Function: getHours
  84. //
  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. //
  89. // Parameters: clockNumber - The unique employee ID
  90. //
  91. // Returns: hoursWorked - hours worked in a given week
  92. //
  93. //**************************************************************
  94.  
  95. // Function to get hours worked
  96. float getHours(long int clockNumber) {
  97. float hoursWorked;
  98. printf("\nEnter hours worked by emp # %06li: ", clockNumber);
  99. if (scanf("%f", &hoursWorked) != 1) {
  100. printf("Invalid input. Please enter a number.\n");
  101. hoursWorked = 0.0;
  102. }
  103. return hoursWorked;
  104.  
  105. } //getHours
  106.  
  107. //**************************************************************
  108. // Function: printHeader
  109. //
  110. // Purpose: Prints the initial table header information.
  111. //
  112. // Parameters: none
  113. //
  114. // Returns: void
  115. //
  116. //**************************************************************
  117.  
  118.  
  119. // Function to print header
  120. void printHeader(void) {
  121. printf("\n\n*** Pay Calculator ***\n");
  122. printf("\nClock# Wage Hours OT Gross\n");
  123. printf("------------------------------------------------\n");
  124.  
  125. } //printHeader
  126.  
  127. //*************************************************************
  128. // Function: printEmp
  129. //
  130. // Purpose: Prints out all the information for an employee
  131. // in a nice and orderly table format.
  132. //
  133. // Parameters:
  134. //
  135. // clockNumber - unique employee ID
  136. // wageRate - hourly wage rate
  137. // hours - Hours worked for the week
  138. // overtimeHrs - overtime hours worked in a week
  139. // grossPay - gross pay for the week
  140. //
  141. // Returns: void
  142. //
  143. //**************************************************************
  144.  
  145. // Function to print employee data
  146. void printEmp(long int clockNumber, float wageRate, float hours, float overtimeHrs, float grossPay) {
  147. printf("\n %06li %5.2f %4.1f %4.1f %8.2f", clockNumber, wageRate, hours, overtimeHrs, grossPay);
  148. }
  149.  
  150. // Function to calculate overtime hours
  151. float calcOT(float hours) {
  152. return (hours > STD_HOURS) ? (hours - STD_HOURS) : 0.0;
  153. }
  154.  
  155. // Function to calculate gross pay
  156. float calcGross(float wageRate, float hours, float overtimeHrs) {
  157. return (hours > STD_HOURS) ? (STD_HOURS * wageRate) + (overtimeHrs * wageRate * OT_RATE) : (hours * wageRate);
  158. }
Success #stdin #stdout 0.01s 5296KB
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