fork(1) download
  1. //********************************************************
  2. //
  3. // Assignment 6 - Structures
  4. //
  5. // Name: Seth Hin
  6. //
  7. // Class: C Programming, Spring 2026
  8. //
  9. // Date: March 7, 2026
  10. //
  11. // Description: Program which determines overtime and
  12. // gross pay for a set of employees using an array of
  13. // structures. The program prompts the user for hours
  14. // worked, calculates overtime hours and gross pay,
  15. // and prints the payroll report.
  16. //
  17. // Call by Value design
  18. //
  19. //********************************************************
  20.  
  21. //-------------------- Includes -------------------------
  22.  
  23. #include <stdio.h>
  24.  
  25. #define SIZE 5 // number of employees
  26. #define STD_HOURS 40.0 // standard hours before overtime
  27. #define OT_RATE 1.5 // overtime pay multiplier
  28.  
  29.  
  30. //-------------------- Structure Definition ------------------
  31.  
  32. // Structure to store employee payroll information
  33. struct employee
  34. {
  35. long int clockNumber; // Employee clock number
  36. float wageRate; // Hourly wage rate
  37. float hours; // Hours worked in the week
  38. float overtimeHrs; // Overtime hours worked
  39. float grossPay; // Total gross pay
  40. };
  41.  
  42.  
  43. //-------------------- Function Prototypes -------------------
  44.  
  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. void calcTotals(struct employee empData[], int size,
  52. float *totalWage, float *totalHours,
  53. float *totalOvertime, float *totalGross);
  54. void printTotalsAndAverages(float totalWage, float totalHours,
  55. float totalOvertime, float totalGross,
  56. int size);
  57.  
  58.  
  59. //-------------------- Main Function -------------------
  60.  
  61. int main()
  62. {
  63. // Array of structures to store employee payroll data
  64. struct employee employeeData[SIZE] = {
  65. {98401, 10.60},
  66. {526488, 9.75},
  67. {765349, 10.50},
  68. {34645, 12.25},
  69. {127615, 8.35}
  70. };
  71.  
  72. int i; // Loop counter
  73.  
  74. // Loop through each employee to gather hours worked
  75. // and calculate payroll information
  76. for (i = 0; i < SIZE; ++i)
  77. {
  78. // Prompt the user to enter hours worked
  79. employeeData[i].hours = getHours(employeeData[i].clockNumber);
  80.  
  81. // Calculate overtime hours worked
  82. employeeData[i].overtimeHrs = calcOvertime(employeeData[i].hours);
  83.  
  84. // Calculate total gross pay including overtime
  85. employeeData[i].grossPay = calcGross(employeeData[i].hours,
  86. employeeData[i].wageRate,
  87. employeeData[i].overtimeHrs);
  88.  
  89. } // end for (employee payroll calculations)
  90.  
  91.  
  92. // Print payroll report header
  93. printHeader();
  94.  
  95.  
  96. // Loop through employees and print payroll information
  97. for (i = 0; i < SIZE; ++i)
  98. {
  99. printEmp(employeeData[i].clockNumber,
  100. employeeData[i].wageRate,
  101. employeeData[i].hours,
  102. employeeData[i].overtimeHrs,
  103. employeeData[i].grossPay);
  104.  
  105. } // end for (printing employee data)
  106.  
  107.  
  108. // Variables to store totals
  109. float totalWage; // Sum of all employee wage rates
  110. float totalHours; // Total hours worked by all employees
  111. float totalOvertime; // Total overtime hours worked
  112. float totalGross; // Total gross pay for all employees
  113.  
  114.  
  115. // Calculate totals for all employees
  116. calcTotals(employeeData, SIZE,
  117. &totalWage, &totalHours,
  118. &totalOvertime, &totalGross);
  119.  
  120.  
  121. // Print totals and averages
  122. printTotalsAndAverages(totalWage, totalHours,
  123. totalOvertime, totalGross, SIZE);
  124.  
  125.  
  126. return 0; // Program completed successfully
  127.  
  128. } // end main
  129.  
  130.  
  131.  
  132. //**************************************************************
  133. // Function: getHours
  134. //
  135. // Purpose: Prompts the user to enter hours worked
  136. // for a specific employee.
  137. //
  138. // Parameters:
  139. // clockNumber - unique employee ID
  140. //
  141. // Returns:
  142. // hoursWorked - number of hours worked during the week
  143. //
  144. //**************************************************************
  145. float getHours(long int clockNumber)
  146. {
  147. float hoursWorked; // Local variable to store user input
  148.  
  149. // Prompt user for hours worked
  150. printf("\nEnter hours worked by emp # %06li: ", clockNumber);
  151.  
  152. // Read input from user
  153. scanf("%f", &hoursWorked);
  154.  
  155. return hoursWorked;
  156.  
  157. } // end getHours
  158.  
  159.  
  160.  
  161. //**************************************************************
  162. // Function: calcOvertime
  163. //
  164. // Purpose: Calculates overtime hours worked by an employee.
  165. //
  166. // Parameters:
  167. // hours - total hours worked during the week
  168. //
  169. // Returns:
  170. // overtime - number of overtime hours worked
  171. //
  172. //**************************************************************
  173. float calcOvertime(float hours)
  174. {
  175. float overtime; // Local variable to store overtime hours
  176.  
  177. // Determine if employee worked overtime
  178. if (hours > STD_HOURS)
  179. {
  180. overtime = hours - STD_HOURS; // Calculate overtime hours
  181. }
  182. else
  183. {
  184. overtime = 0.0; // No overtime worked
  185. }
  186.  
  187. return overtime;
  188.  
  189. } // end calcOvertime
  190.  
  191.  
  192.  
  193. //**************************************************************
  194. // Function: calcGross
  195. //
  196. // Purpose: Calculates the total gross pay including overtime.
  197. //
  198. // Parameters:
  199. // hours - total hours worked
  200. // wageRate - hourly wage rate
  201. // overtimeHrs - overtime hours worked
  202. //
  203. // Returns:
  204. // gross - total gross pay
  205. //
  206. //**************************************************************
  207. float calcGross(float hours, float wageRate, float overtimeHrs)
  208. {
  209. // Calculate regular pay (hours minus overtime)
  210. float regularPay = (hours - overtimeHrs) * wageRate;
  211.  
  212. // Calculate overtime pay using overtime multiplier
  213. float overtimePay = overtimeHrs * wageRate * OT_RATE;
  214.  
  215. // Add regular pay and overtime pay to get total gross
  216. float gross = regularPay + overtimePay;
  217.  
  218. return gross;
  219.  
  220. } // end calcGross
  221.  
  222.  
  223.  
  224. //**************************************************************
  225. // Function: printHeader
  226. //
  227. // Purpose: Prints the payroll report header.
  228. //
  229. // Parameters: none
  230. //
  231. // Returns: void
  232. //
  233. //**************************************************************
  234. void printHeader(void)
  235. {
  236. printf("\n\n*** Pay Calculator ***\n");
  237.  
  238. // Print column headers
  239. printf("\nClock# Wage Hours OT Gross\n");
  240. printf("------------------------------------------------\n");
  241.  
  242. } // end printHeader
  243.  
  244.  
  245.  
  246. //**************************************************************
  247. // Function: printEmp
  248. //
  249. // Purpose: Prints employee payroll information in a formatted row.
  250. //
  251. // Parameters:
  252. // clockNumber - employee ID
  253. // wageRate - hourly wage
  254. // hours - hours worked
  255. // overtimeHrs - overtime hours
  256. // grossPay - total gross pay
  257. //
  258. // Returns: void
  259. //
  260. //**************************************************************
  261. void printEmp(long int clockNumber, float wageRate, float hours,
  262. float overtimeHrs, float grossPay)
  263. {
  264. printf("\n %06li %5.2f %5.1f %4.1f %8.2f",
  265. clockNumber, wageRate, hours, overtimeHrs, grossPay);
  266.  
  267. } // end printEmp
  268.  
  269.  
  270.  
  271. //**************************************************************
  272. // Function: calcTotals
  273. //
  274. // Purpose: Calculates totals for wage, hours, overtime,
  275. // and gross pay across all employees.
  276. //
  277. // Parameters:
  278. // empData - array of employee structures
  279. // size - number of employees
  280. // totalWage - pointer storing total wage rate
  281. // totalHours - pointer storing total hours
  282. // totalOvertime - pointer storing total overtime hours
  283. // totalGross - pointer storing total gross pay
  284. //
  285. // Returns: void
  286. //
  287. //**************************************************************
  288. void calcTotals(struct employee empData[], int size,
  289. float *totalWage, float *totalHours,
  290. float *totalOvertime, float *totalGross)
  291. {
  292. int i; // Loop counter
  293.  
  294. // Initialize totals to zero
  295. *totalWage = 0.0;
  296. *totalHours = 0.0;
  297. *totalOvertime = 0.0;
  298. *totalGross = 0.0;
  299.  
  300. // Loop through employees and accumulate totals
  301. for (i = 0; i < size; ++i)
  302. {
  303. *totalWage += empData[i].wageRate;
  304. *totalHours += empData[i].hours;
  305. *totalOvertime += empData[i].overtimeHrs;
  306. *totalGross += empData[i].grossPay;
  307.  
  308. } // end for (totals calculation)
  309.  
  310. } // end calcTotals
  311.  
  312.  
  313.  
  314. //**************************************************************
  315. // Function: printTotalsAndAverages
  316. //
  317. // Purpose: Prints total and average payroll values.
  318. //
  319. // Parameters:
  320. // totalWage - total wage rate
  321. // totalHours - total hours worked
  322. // totalOvertime - total overtime hours
  323. // totalGross - total gross pay
  324. // size - number of employees
  325. //
  326. // Returns: void
  327. //
  328. //**************************************************************
  329. void printTotalsAndAverages(float totalWage, float totalHours,
  330. float totalOvertime, float totalGross,
  331. int size)
  332. {
  333. // Calculate averages
  334. float avgWage = totalWage / size; // Average wage rate
  335. float avgHours = totalHours / size; // Average hours worked
  336. float avgOvertime = totalOvertime / size; // Average overtime hours
  337. float avgGross = totalGross / size; // Average gross pay
  338.  
  339. printf("\n------------------------------------------------");
  340.  
  341. // Print totals
  342. printf("\nTotal %5.2f %5.1f %4.1f %8.2f",
  343. totalWage, totalHours, totalOvertime, totalGross);
  344.  
  345. // Print averages
  346. printf("\nAverage %5.2f %5.1f %4.1f %8.2f\n",
  347. avgWage, avgHours, avgOvertime, avgGross);
  348.  
  349. } // end printTotalsAndAverages
Success #stdin #stdout 0.01s 5288KB
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
------------------------------------------------
Total   51.45 175.5 18.5  1995.84
Average 10.29  35.1  3.7   399.17