fork download
  1. //********************************************************
  2. //
  3. // Assignment 6 - Structures
  4. //
  5. // Name: <rHaroon Malik>
  6. //
  7. // Class: C Programming, <Fall 2026>
  8. //
  9. // Date: <10/14/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. // Defines and Includes
  19. #include <stdio.h>
  20.  
  21. // Define Constants
  22. #define SIZE 5
  23. #define STD_HOURS 40.0
  24. #define OT_RATE 1.5
  25.  
  26. // Define a global structure type.
  27. // Employee variables are declared locally in main.
  28. struct employee
  29. {
  30. long int clockNumber;
  31. float wageRate;
  32. float hours;
  33. float overtimeHrs;
  34. float grossPay;
  35. };
  36.  
  37. // Function prototypes
  38. float getHours(long int clockNumber);
  39. float getOvertime(float hours);
  40. float getGrossPay(float wageRate, float hours, float overtimeHrs);
  41. void printHeader(void);
  42. void printEmp(long int clockNumber, float wageRate, float hours,
  43. float overtimeHrs, float grossPay);
  44.  
  45. int main(void)
  46. {
  47. // Store employee information in a local array.
  48. struct employee employeeData[SIZE] = {
  49. { 98401, 10.60f, 0.0f, 0.0f, 0.0f },
  50. { 526488, 9.75f, 0.0f, 0.0f, 0.0f },
  51. { 765349, 10.50f, 0.0f, 0.0f, 0.0f },
  52. { 34645, 12.25f, 0.0f, 0.0f, 0.0f },
  53. { 127615, 8.35f, 0.0f, 0.0f, 0.0f }
  54. };
  55.  
  56. int i; // loop and array index
  57.  
  58. // Read hours and calculate overtime and gross pay.
  59. for (i = 0; i < SIZE; ++i)
  60. {
  61. employeeData[i].hours =
  62. getHours(employeeData[i].clockNumber);
  63.  
  64. employeeData[i].overtimeHrs =
  65. getOvertime(employeeData[i].hours);
  66.  
  67. employeeData[i].grossPay =
  68. getGrossPay(employeeData[i].wageRate,
  69. employeeData[i].hours,
  70. employeeData[i].overtimeHrs);
  71. }
  72.  
  73. // Print the column headers.
  74. printHeader();
  75.  
  76. // Print each employee.
  77. for (i = 0; i < SIZE; ++i)
  78. {
  79. printEmp(employeeData[i].clockNumber,
  80. employeeData[i].wageRate,
  81. employeeData[i].hours,
  82. employeeData[i].overtimeHrs,
  83. employeeData[i].grossPay);
  84. }
  85.  
  86. printf("\n");
  87.  
  88. return 0;
  89. }
  90.  
  91. //**************************************************************
  92. // Function: getHours
  93. //
  94. // Purpose: Obtains the number of hours worked by an employee.
  95. //
  96. // Parameters: clockNumber - unique employee ID
  97. //
  98. // Returns: hoursWorked - hours worked in a given week
  99. //**************************************************************
  100.  
  101. float getHours(long int clockNumber)
  102. {
  103. float hoursWorked;
  104. int inputStatus;
  105. int character;
  106.  
  107. // Continue prompting until valid, nonnegative hours are entered.
  108. do
  109. {
  110. printf("\nEnter hours worked by emp # %06li: ", clockNumber);
  111. inputStatus = scanf("%f", &hoursWorked);
  112.  
  113. if (inputStatus == EOF)
  114. {
  115. return 0.0f;
  116. }
  117.  
  118. // Discard the remaining input on this line.
  119. while ((character = getchar()) != '\n' && character != EOF)
  120. {
  121. }
  122.  
  123. if (inputStatus != 1)
  124. {
  125. printf("Invalid input. Please enter a number.\n");
  126. }
  127. else if (hoursWorked < 0.0f)
  128. {
  129. printf("Hours cannot be negative. Please try again.\n");
  130. }
  131. } while (inputStatus != 1 || hoursWorked < 0.0f);
  132.  
  133. return hoursWorked;
  134. }
  135.  
  136. //**************************************************************
  137. // Function: getOvertime
  138. //
  139. // Purpose: Calculates hours worked beyond the standard workweek.
  140. //
  141. // Parameters: hours - total hours worked for the week
  142. //
  143. // Returns: overtime hours
  144. //**************************************************************
  145.  
  146. float getOvertime(float hours)
  147. {
  148. if (hours > STD_HOURS)
  149. {
  150. return hours - STD_HOURS;
  151. }
  152.  
  153. return 0.0f;
  154. }
  155.  
  156. //**************************************************************
  157. // Function: getGrossPay
  158. //
  159. // Purpose: Calculates regular pay plus overtime pay.
  160. //
  161. // Parameters:
  162. // wageRate - hourly wage rate
  163. // hours - total hours worked for the week
  164. // overtimeHrs - overtime hours worked for the week
  165. //
  166. // Returns: gross pay for the week
  167. //**************************************************************
  168.  
  169. float getGrossPay(float wageRate, float hours, float overtimeHrs)
  170. {
  171. float regularHours = hours - overtimeHrs;
  172.  
  173. return (regularHours * wageRate) +
  174. (overtimeHrs * wageRate * OT_RATE);
  175. }
  176.  
  177. //**************************************************************
  178. // Function: printHeader
  179. //
  180. // Purpose: Prints the initial table header information.
  181. //
  182. // Parameters: none
  183. //
  184. // Returns: void
  185. //**************************************************************
  186.  
  187. void printHeader(void)
  188. {
  189. printf("\n\n*** Pay Calculator ***\n");
  190. printf("\n Clock# Wage Hours OT Gross\n");
  191. printf("----------------------------------------\n");
  192. }
  193.  
  194. //**************************************************************
  195. // Function: printEmp
  196. //
  197. // Purpose: Prints information for one employee in table format.
  198. //
  199. // Parameters:
  200. // clockNumber - unique employee ID
  201. // wageRate - hourly wage rate
  202. // hours - hours worked for the week
  203. // overtimeHrs - overtime hours worked for the week
  204. // grossPay - gross pay for the week
  205. //
  206. // Returns: void
  207. //**************************************************************
  208.  
  209. void printEmp(long int clockNumber, float wageRate, float hours,
  210. float overtimeHrs, float grossPay)
  211. {
  212. printf("\n %06li %5.2f %5.1f %4.1f %8.2f",
  213. clockNumber, wageRate, hours, overtimeHrs, grossPay);
  214. }
Success #stdin #stdout 0.01s 5324KB
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