fork download
  1. //********************************************************
  2. //
  3. // Assignment 6 - Structures
  4. //
  5. // Name: Rose Samedi
  6. //
  7. // Class: C Programming, Fall 2025
  8. //
  9. // Date: 10/26/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. // Functions called by a combination of by value and by
  16. // reference.
  17. //
  18. //********************************************************
  19. // Define and Includes
  20. #include <stdio.h>
  21.  
  22. // Define Constants
  23. #define SIZE 5
  24. #define STD_HOURS 40.0
  25. #define OT_RATE 1.5
  26.  
  27. // Define a global structure to pass employee data between functions
  28. // Note that the structure type is global, but you don't want a variable
  29. // of that type to be global. Best to declare a variable of that type
  30. // in a function like main or another function and pass as needed.
  31. struct employee {
  32. long int clockNumber;
  33. float wageRate;
  34. float hours;
  35. float overtimeHrs;
  36. float grossPay;
  37. };
  38.  
  39. // define prototypes here for each function except main
  40. float getHours(long int clockNumber);
  41. void printHeader(void);
  42. void printEmp(struct employee emp[], int theSize);
  43.  
  44. // TODO: Add your other function prototypes here
  45. void calculatePay(struct employee *emp);
  46.  
  47. int main() {
  48. // Set 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. int i; // loop and array index
  56.  
  57. // Call functions as needed to read and calculate information
  58. for (i = 0; i < SIZE; ++i) {
  59. // Prompt for the number of hours worked by the employee
  60. employeeData[i].hours = getHours(employeeData[i].clockNumber);
  61. // TODO: Add other function calls as needed to calculate overtime and gross
  62. calculatePay(&employeeData[i]);
  63. } // for
  64.  
  65. // Print the column headers
  66. printHeader();
  67.  
  68. // Function call to output results to the screen in table format.
  69. printEmp(employeeData, SIZE);
  70.  
  71. return (0); // success
  72. } // main
  73.  
  74. //**************************************************************
  75. // Function: getHours
  76. //
  77. // Purpose: Obtains input from user, the number of hours worked
  78. // per employee and stores the result in a local variable
  79. // that is passed back to the calling function.
  80. //
  81. // Parameters: clockNumber - The unique employee ID
  82. //
  83. // Returns: hoursWorked - hours worked in a given week
  84. //
  85. //**************************************************************
  86. float getHours(long int clockNumber) {
  87. float hoursWorked; // hours worked in a given week
  88. // Read in hours for employee
  89. printf("\nEnter hours worked by emp # %06li: ", clockNumber);
  90. scanf("%f", &hoursWorked);
  91. // return hours back to the calling function
  92. return (hoursWorked);
  93. } // getHours
  94.  
  95. //**************************************************************
  96. // Function: printHeader
  97. //
  98. // Purpose: Prints the initial table header information.
  99. //
  100. // Parameters: none
  101. //
  102. // Returns: void
  103. //
  104. //**************************************************************
  105. void printHeader(void) {
  106. printf("\n\n*** Pay Calculator ***\n"); // print the table header
  107. printf("\nClock# Wage Hours OT Gross\n");
  108. printf("------------------------------------------------\n");
  109. } // printHeader
  110.  
  111. // ******************************************************************
  112. // Function: printEmp
  113. //
  114. // Purpose: Outputs to screen in a table format the following
  115. // information about an employee: Clock, Wage,
  116. // Hours, Overtime, and Gross Pay.
  117. //
  118. // Parameters:
  119. //
  120. // employeeData - an array of structures containing
  121. // employee information
  122. // theSize - number of employees to process
  123. //
  124. // Returns: Nothing (void)
  125. //
  126. // *******************************************************************
  127. void printEmp(struct employee employeeData[], int theSize) {
  128. int i; // loop index
  129. // print information about each employee
  130. for (i = 0; i < theSize; ++i) {
  131. printf("\n %06li %5.2f %4.1f %4.1f %8.2f",
  132. employeeData[i].clockNumber,
  133. employeeData[i].wageRate,
  134. employeeData[i].hours,
  135. employeeData[i].overtimeHrs,
  136. employeeData[i].grossPay);
  137. } // for
  138. } // printEmp
  139.  
  140. // TODO: Add your functions here
  141. //**************************************************************
  142. // Function: calculatePay
  143. //
  144. // Purpose: Calculates the overtime hours and gross pay for an
  145. // employee based on standard hours and overtime rate.
  146. //
  147. // Parameters:
  148. //
  149. // emp - A pointer to an employee structure to
  150. // calculate and update pay information for.
  151. //
  152. // Returns: Nothing (void)
  153. //
  154. //**************************************************************
  155. void calculatePay(struct employee *emp) {
  156. if (emp->hours > STD_HOURS) {
  157. emp->overtimeHrs = emp->hours - STD_HOURS;
  158. emp->grossPay = (STD_HOURS * emp->wageRate) + (emp->overtimeHrs * (emp->wageRate * OT_RATE));
  159. } else {
  160. emp->overtimeHrs = 0.0;
  161. emp->grossPay = emp->hours * emp->wageRate;
  162. }
  163. } // calculatePay
Success #stdin #stdout 0.01s 5316KB
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