fork download
  1. //********************************************************
  2. //
  3. // Assignment 6 - Structures
  4. //
  5. // Name: MARTIN CLYTON MAYANJA
  6. //
  7. // Class: C Programming, SPRING,2026
  8. //
  9. // Date: 08MARCH26
  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.  
  44. // Function Prototype
  45. float getHours(long int clockNumber);
  46. float calcOvertime(float hours);
  47. float calcGross(float hours, float wageRate, float overtimeHrs);
  48. void printHeader(void);
  49. void printEmp(long int clockNumber, float wageRate, float hours,
  50. float overtimeHrs, float grossPay);
  51.  
  52. // Main Function
  53.  
  54. int main (void)
  55. {
  56. // Set up a local variable to store the employee information
  57. struct employee employeeData[SIZE] = {
  58. { 98401, 10.60 },
  59. { 526488, 9.75 },
  60. { 765349, 10.50 }, // initialize clock and wage values
  61. { 34645, 12.25 },
  62. { 127615, 8.35 }
  63. };
  64.  
  65. int i; // loop and array index
  66.  
  67. // Call functions as needed to read and calculate information
  68. for (i = 0; i < SIZE; ++i)
  69. {
  70.  
  71. // Prompt for the number of hours worked by the employee
  72. employeeData[i].hours = getHours (employeeData[i].clockNumber);
  73.  
  74. // Calculate overtime and gross
  75. employeeData[i].overtimeHrs =
  76. calcOvertime(employeeData[i].hours);
  77.  
  78. employeeData[i].grossPay =
  79. calcGross(employeeData[i].hours,
  80. employeeData[i].wageRate,
  81. employeeData[i].overtimeHrs);
  82.  
  83. }
  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: calcOvertime
  131. //
  132. // Purpose: Calculates overtime hours worked
  133. //
  134. // Parameters: hours - total hours worked
  135. //
  136. // Returns: overtime hours worked
  137. //
  138. //**************************************************************
  139. float calcOvertime(float hours)
  140. {
  141. float overtime = 0;
  142.  
  143. if (hours > STD_HOURS)
  144. overtime = hours - STD_HOURS;
  145.  
  146. return overtime;
  147.  
  148. } // calcOvertime
  149.  
  150. //********************************************************
  151. // Function: calcGross
  152. //
  153. // Purpose: Calculates gross pay including overtime
  154. //********************************************************
  155. float calcGross(float hours, float wageRate, float overtimeHrs)
  156. {
  157. float grossPay;
  158.  
  159. if (hours > STD_HOURS)
  160. {
  161. grossPay = (STD_HOURS * wageRate) +
  162. (overtimeHrs * wageRate * OT_RATE);
  163. }
  164. else
  165. {
  166. grossPay = hours * wageRate;
  167. }
  168.  
  169. return grossPay;
  170.  
  171. } // calcGross
  172.  
  173. //********************************************************
  174. // Function: printHeader
  175. //
  176. // Purpose: Prints the table header
  177. //********************************************************
  178. void printHeader (void)
  179. {
  180.  
  181. printf ("\n\n*** Pay Calculator ***\n");
  182.  
  183. // print the table header
  184. printf("\nClock# Wage Hours OT Gross\n");
  185. printf("------------------------------------------------\n");
  186.  
  187. } // printHeader
  188.  
  189. //*************************************************************
  190. // Function: printEmp
  191. //
  192. // Purpose: Prints out all the information for an employee
  193. // in a nice and orderly table format.
  194. //
  195. // Parameters:
  196. //
  197. // clockNumber - unique employee ID
  198. // wageRate - hourly wage rate
  199. // hours - Hours worked for the week
  200. // overtimeHrs - overtime hours worked in a week
  201. // grossPay - gross pay for the week
  202. //
  203. // Returns: void
  204. //
  205. //**************************************************************
  206.  
  207. void printEmp (long int clockNumber, float wageRate, float hours,
  208. float overtimeHrs, float grossPay)
  209. {
  210.  
  211. // Print out a single employee
  212. printf("\n %06li %5.2f %4.1f %4.1f %8.2f",
  213. clockNumber, wageRate, hours,
  214. overtimeHrs, grossPay);
  215.  
  216. } // printEmp
  217.  
Success #stdin #stdout 0.01s 5240KB
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