fork download
  1. //********************************************************
  2. //
  3. // Assignment 6 - Structures
  4. //
  5. // Name: James Park
  6. //
  7. // Class: C Programming, Fall Semester, 2026
  8. //
  9. // Date: 18 Oct 2026
  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. // float calcOvertime (float hours);
  49. float calcGross (float wageRate, float hours, float overtimeHrs);
  50. float calcOvertime (float hours);
  51. float calcGross (float wageRate, float hours, float overtimeHrs);
  52.  
  53. int main ()
  54. {
  55. // Set up a local variable to store the employee information
  56. struct employee employeeData[SIZE] = {
  57. { 98401, 10.60 },
  58. { 526488, 9.75 },
  59. { 765349, 10.50 }, // initialize clock and wage values
  60. { 34645, 12.25 },
  61. { 127615, 8.35 }
  62. };
  63.  
  64. int i; // loop and array index
  65.  
  66. // Call functions as needed to read and calculate information
  67. for (i = 0; i < SIZE; ++i)
  68. {
  69.  
  70. // Prompt for the number of hours worked by the employee
  71. employeeData[i].hours = getHours (employeeData[i].clockNumber);
  72.  
  73. // Calculate overtime hours
  74. employeeData[i].overtimeHrs = calcOvertime (employeeData[i].hours);
  75. } // for Calculate gross pay
  76. employeeData[i].grossPay = calcGross (employeeData[i].wageRate, employeeData[i].hours,
  77. employeeData[i].overtimeHrs);
  78.  
  79. // Print the column headers
  80. printHeader();
  81.  
  82. // print out each employee
  83. for (i = 0; i < SIZE; ++i)
  84. {
  85. printEmp (employeeData[i].clockNumber,
  86. employeeData[i].wageRate,
  87. employeeData[i].hours,
  88. employeeData[i].overtimeHrs,
  89. employeeData[i].grossPay);
  90. }
  91.  
  92. return(0); // success
  93.  
  94. } // main
  95.  
  96. //**************************************************************
  97. // Function: getHours
  98. //
  99. // Purpose: Obtains input from user, the number of hours worked
  100. // per employee and stores the result in a local variable
  101. // that is passed back to the calling function.
  102. //
  103. // Parameters: clockNumber - The unique employee ID
  104. //
  105. // Returns: hoursWorked - hours worked in a given week
  106. //
  107. //**************************************************************
  108.  
  109. float getHours (long int clockNumber)
  110. {
  111.  
  112. float hoursWorked; // hours worked in a given week
  113.  
  114. // Read in hours for employee
  115. printf("\nEnter hours worked by emp # %06li: ", clockNumber);
  116. scanf ("%f", &hoursWorked);
  117.  
  118. // return hours back to the calling function
  119. if (scanf ("%f", &hoursWorked) != 1)
  120. {
  121. hoursWorked = 0.0;
  122. }
  123. } // getHours
  124.  
  125. //**************************************************************
  126. // Function: printHeader
  127. //
  128. // Purpose: Prints the initial table header information.
  129. //
  130. // Parameters: none
  131. //
  132. // Returns: void
  133. //
  134. //**************************************************************
  135.  
  136. void printHeader (void)
  137. {
  138.  
  139. printf ("\n\n*** Pay Calculator ***\n");
  140.  
  141. // print the table header
  142. printf("\nClock# Wage Hours OT Gross\n");
  143. printf("------------------------------------------------\n");
  144.  
  145. } // printHeader
  146.  
  147.  
  148. //*************************************************************
  149. // Function: printEmp
  150. //
  151. // Purpose: Prints out all the information for an employee
  152. // in a nice and orderly table format.
  153. //
  154. // Parameters:
  155. //
  156. // clockNumber - unique employee ID
  157. // wageRate - hourly wage rate
  158. // hours - Hours worked for the week
  159. // overtimeHrs - overtime hours worked in a week
  160. // grossPay - gross pay for the week
  161. //
  162. // Returns: void
  163. //
  164. //**************************************************************
  165.  
  166. void printEmp (long int clockNumber, float wageRate, float hours,
  167. float overtimeHrs, float grossPay)
  168. {
  169.  
  170. // Print out a single employee
  171. printf("\n %06li %5.2f %4.1f %4.1f %8.2f",
  172. clockNumber, wageRate, hours,
  173. overtimeHrs, grossPay);
  174.  
  175. } // printEmp
  176.  
  177. // //**************************************************************
  178. // Function: calcOvertime
  179. //
  180. // Purpose: Calculates overtime hours worked (any hours over 40)
  181. //
  182. // Parameters: hours - total hours worked
  183. //
  184. // Returns: overtime hours worked
  185. //*****************************************************************
  186. float calcOvertime (float hours)
  187. {
  188. if (hours > STD_HOURS)
  189. {
  190. return (hours - STD_HOURS);
  191. }
  192. return 0.0;
  193. }
  194.  
  195. //**************************************************************
  196. // Function: calcGross
  197. //
  198. // Purpose: Calculates gross pay, incorporating standard pay
  199. // and overtime pay rates if applicable.
  200. //
  201. // Parameters: wageRate - hourly rate of pay
  202. // hours - total hours worked
  203. // overtimeHrs - calculated overtime hours
  204. //
  205. // Returns: total gross pay earned
  206. //**************************************************************
  207. float calcGross (float wageRate, float hours, float overtimeHrs)
  208. {
  209. if (overtimeHrs > 0.0)
  210. {
  211. float standardPay = STD_HOURS * wageRate;
  212. float overtimePay = overtimeHrs * (wageRate * OT_RATE);
  213. return (standardPay + overtimePay);
  214. }
  215.  
  216. return (hours * wageRate);
  217. }
Success #stdin #stdout 0s 5328KB
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 42.5  2.5     0.00
 526488  9.75 45.0  5.0     0.00
 765349 10.50 -5486443233280.0  0.0     0.00
 034645 12.25 -5486443233280.0  0.0     0.00
 127615  8.35 -5486443233280.0  0.0     0.00