fork download
  1. //********************************************************
  2. //
  3. // Assignment 6 - Structures
  4. //
  5. // Name: Dani D'Oleo Pena
  6. //
  7. // Class: C Programming, Summer 2025
  8. //
  9. // Date: 07/01/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. // Note that the structure type is global, but you don't want a variable
  30. // of that type to be global. Best to declare a variable of that type
  31. // in a function like main or another function and pass as needed.
  32.  
  33. struct employee
  34. {
  35. long int clockNumber;
  36. float wageRate;
  37. float hours;
  38. float overtimeHrs;
  39. float grossPay;
  40. };
  41.  
  42. // define prototypes here for each function except main
  43. float getHours (long int clockNumber);
  44. void printHeader (void);
  45. void printEmp (long int clockNumber, float wageRate, float hours,
  46. float overtimeHrs, float grossPay);
  47.  
  48. // TODO: Add your other function prototypes here
  49. float getOThours (float hours, float stdworkweek);
  50. float calculateGrossPay(float hours, float wageRate, float otwageRate, float othours, float stdworkweek);
  51.  
  52. int main ()
  53. {
  54. // Set up a local variable to store the employee information
  55. struct employee employeeData[SIZE] = {
  56. { 98401, 10.60 },
  57. { 526488, 9.75 },
  58. { 765349, 10.50 }, // initialize clock and wage values
  59. { 34645, 12.25 },
  60. { 127615, 8.35 }
  61. };
  62.  
  63. int i; // loop and array index
  64.  
  65. // Call functions as needed to read and calculate information
  66. for (i = 0; i < SIZE; ++i)
  67. {
  68.  
  69. // Prompt for the number of hours worked by the employee
  70. employeeData[i].hours = getHours (employeeData[i].clockNumber);
  71.  
  72. // TODO: Add other function calls as needed to calculate overtime and gross
  73. employeeData[i].overtimeHrs = getOThours(employeeData[i].hours,STD_HOURS);
  74. employeeData[i].grossPay = calculateGrossPay(employeeData[i].hours, employeeData[i].wageRate, OT_RATE, employeeData[i].overtimeHrs, STD_HOURS);
  75. } // for
  76.  
  77. // Print the column headers
  78. printHeader();
  79.  
  80. // print out each employee
  81. for (i = 0; i < SIZE; ++i)
  82. {
  83. printEmp (employeeData[i].clockNumber,
  84. employeeData[i].wageRate,
  85. employeeData[i].hours,
  86. employeeData[i].overtimeHrs,
  87. employeeData[i].grossPay);
  88. }
  89.  
  90. return(0); // success
  91.  
  92. } // main
  93.  
  94. //**************************************************************
  95. // Function: getHours
  96. //
  97. // Purpose: Obtains input from user, the number of hours worked
  98. // per employee and stores the result in a local variable
  99. // that is passed back to the calling function.
  100. //
  101. // Parameters: clockNumber - The unique employee ID
  102. //
  103. // Returns: hoursWorked - hours worked in a given week
  104. //
  105. //**************************************************************
  106.  
  107. float getHours (long int clockNumber)
  108. {
  109.  
  110. float hoursWorked; // hours worked in a given week
  111.  
  112. // Read in hours for employee
  113. printf("\nEnter hours worked by emp # %06li: ", clockNumber);
  114. scanf ("%f", &hoursWorked);
  115.  
  116. // return hours back to the calling function
  117. return (hoursWorked);
  118.  
  119. } // getHours
  120.  
  121. //**************************************************************
  122. // Function: getOTHours
  123. //
  124. // Purpose: Obtains number of Overtime hours based on the amount of hours worked.
  125. //
  126. // Parameters: hours - numbers of hours worked, stdworkweek - amount of hours worked without overtime
  127. //
  128. // Returns: othoursWorked - overtime hours worked in a given week
  129. //
  130. //**************************************************************
  131.  
  132. float getOThours (float hours, float stdworkweek)
  133. {
  134. float othoursWorked = hours - stdworkweek;
  135. if (othoursWorked > 0.0) { //checks if there is overtime or not.
  136. return (othoursWorked);
  137. }
  138. else {
  139. return (0);
  140. }
  141. } // getOThours
  142.  
  143.  
  144. //**************************************************************
  145. // Function: calculateGrossPay
  146. //
  147. // Purpose: Obtains Gross pay based on wage rates
  148. //
  149. // Parameters: hours - numbers of hours worked, wageRate - normal wage rate, otwageRate - overtime wage rate,
  150. // othours - numbers of overtime hours worked.
  151. //
  152. // Returns: grossPay - total money gained from working.
  153. //
  154. //**************************************************************
  155.  
  156. float calculateGrossPay(float hours, float wageRate, float otwageRate, float othours, float stdworkweek)
  157. {
  158. float grossPay;
  159. if (othours != 0){ // if there is overtime calculate grosspay with overtime
  160. grossPay = stdworkweek*wageRate + otwageRate*wageRate*othours;
  161. }
  162. else { // if there isn't overtime calculate grosspay without overtime
  163. grossPay = hours*wageRate;
  164. }
  165. return (grossPay);
  166.  
  167. } // calculateGrossPay
  168.  
  169. //**************************************************************
  170. // Function: printHeader
  171. //
  172. // Purpose: Prints the initial table header information.
  173. //
  174. // Parameters: none
  175. //
  176. // Returns: void
  177. //
  178. //**************************************************************
  179.  
  180. void printHeader (void)
  181. {
  182.  
  183. printf ("\n\n*** Pay Calculator ***\n");
  184.  
  185. // print the table header
  186. printf("\nClock# Wage Hours OT Gross\n");
  187. printf("------------------------------------------------\n");
  188.  
  189. } // printHeader
  190.  
  191.  
  192. //*************************************************************
  193. // Function: printEmp
  194. //
  195. // Purpose: Prints out all the information for an employee
  196. // in a nice and orderly table format.
  197. //
  198. // Parameters:
  199. //
  200. // clockNumber - unique employee ID
  201. // wageRate - hourly wage rate
  202. // hours - Hours worked for the week
  203. // overtimeHrs - overtime hours worked in a week
  204. // grossPay - gross pay for the week
  205. //
  206. // Returns: void
  207. //
  208. //**************************************************************
  209.  
  210. void printEmp (long int clockNumber, float wageRate, float hours,
  211. float overtimeHrs, float grossPay)
  212. {
  213.  
  214. // Print out a single employee
  215. printf("\n %06li %5.2f %4.1f %4.1f %8.2f",
  216. clockNumber, wageRate, hours,
  217. overtimeHrs, grossPay);
  218.  
  219. } // printEmp
  220.  
  221. // TODO: Add your functions here
Success #stdin #stdout 0.01s 5284KB
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