fork download
  1. //********************************************************
  2. //
  3. // Assignment 6 - Structures
  4. //
  5. // Name: Juan Difot
  6. //
  7. // Class: C Programming, Spring 2025
  8. //
  9. // Date: March 9th, 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 calcOT (float hours);
  50. float calcGross (float wageRate, float hours, float overtimeHrs);
  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.  
  74. //Function call to calculate overtime hours
  75. employeeData[i].overtimeHrs = calcOT (employeeData[i].hours);
  76.  
  77. // Function time to calculate gross pay
  78. employeeData[i].grossPay = calcGross (employeeData[i].wageRate,
  79. employeeData[i].hours,
  80. employeeData[i].overtimeHrs);
  81.  
  82.  
  83. } // for
  84.  
  85. // Print the column headers
  86. printHeader();
  87.  
  88. // print out each employee
  89. for (i = 0; i < SIZE; ++i)
  90. {
  91. printEmp (employeeData[i].clockNumber,
  92. employeeData[i].wageRate,
  93. employeeData[i].hours,
  94. employeeData[i].overtimeHrs,
  95. employeeData[i].grossPay);
  96. }
  97.  
  98. return(0); // success
  99.  
  100. } // main
  101.  
  102. //**************************************************************
  103. // Function: getHours
  104. //
  105. // Purpose: Obtains input from user, the number of hours worked
  106. // per employee and stores the result in a local variable
  107. // that is passed back to the calling function.
  108. //
  109. // Parameters: clockNumber - The unique employee ID
  110. //
  111. // Returns: hoursWorked - hours worked in a given week
  112. //
  113. //**************************************************************
  114.  
  115. float getHours (long int clockNumber)
  116. {
  117.  
  118. float hoursWorked; // hours worked in a given week
  119.  
  120. // Read in hours for employee
  121. printf("\nEnter hours worked by emp # %06li: ", clockNumber);
  122. scanf ("%f", &hoursWorked);
  123.  
  124. // return hours back to the calling function
  125. return (hoursWorked);
  126.  
  127. } // getHours
  128.  
  129. //**************************************************************
  130. // Function: printHeader
  131. //
  132. // Purpose: Prints the initial table header information.
  133. //
  134. // Parameters: none
  135. //
  136. // Returns: void
  137. //
  138. //**************************************************************
  139.  
  140. void printHeader (void)
  141. {
  142.  
  143. printf ("\n\n*** Pay Calculator ***\n");
  144.  
  145. // print the table header
  146. printf("\nClock# Wage Hours OT Gross\n");
  147. printf("------------------------------------------------\n");
  148.  
  149. } // printHeader
  150.  
  151.  
  152. //*************************************************************
  153. // Function: printEmp
  154. //
  155. // Purpose: Prints out all the information for an employee
  156. // in a nice and orderly table format.
  157. //
  158. // Parameters:
  159. //
  160. // clockNumber - unique employee ID
  161. // wageRate - hourly wage rate
  162. // hours - Hours worked for the week
  163. // overtimeHrs - overtime hours worked in a week
  164. // grossPay - gross pay for the week
  165. //
  166. // Returns: void
  167. //
  168. //**************************************************************
  169.  
  170. void printEmp (long int clockNumber, float wageRate, float hours,
  171. float overtimeHrs, float grossPay)
  172. {
  173.  
  174. // Print out a single employee
  175. printf("\n %06li %5.2f %4.1f %4.1f %8.2f",
  176. clockNumber, wageRate, hours,
  177. overtimeHrs, grossPay);
  178.  
  179. } // printEmp
  180.  
  181. // TODO: Add your functions here
  182.  
  183. //*************************************************************
  184. // Function: calcOT
  185. //
  186. // Purpose: Calculates overtime pay
  187. //
  188. // Parameters:
  189. //
  190. // hours - Hours worked for the week
  191. //
  192. // Returns: overtime
  193. //
  194. //**************************************************************
  195.  
  196. float calcOT (float hours)
  197. {
  198. // declare local variables
  199. float result; // Overtime hours result
  200. // add in code
  201. if (hours >= STD_HOURS)
  202. {
  203. result = hours - STD_HOURS;
  204. } // for
  205. else
  206. {
  207. result = 0;
  208. } // else
  209.  
  210. // return a value
  211. return (result);
  212.  
  213. } //calcOT
  214.  
  215. //*************************************************************
  216. // Function: calcGross
  217. //
  218. // Purpose: Calculates gross pay
  219. //
  220. // Parameters:
  221. //
  222. // wageRate - hourly wage rate
  223. // hours - Hours worked for the week
  224. // overtimeHrs - overtime hours worked in a week
  225. //
  226. // Returns: gross pay
  227. //
  228. //**************************************************************
  229.  
  230. float calcGross (float wageRate, float hours, float overtimeHrs)
  231. {
  232. // declare local variables
  233. float gross; // Gross pay result
  234.  
  235. // add in code
  236. if (hours >= STD_HOURS)
  237. {
  238. gross = (wageRate * STD_HOURS) + (overtimeHrs * (OT_RATE *wageRate));
  239. } // for
  240. else
  241. {
  242. gross = wageRate * hours;
  243. } // else
  244.  
  245. // return a value
  246. return (gross);
  247.  
  248. } //calcGross
Success #stdin #stdout 0s 5252KB
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