fork download
  1. //********************************************************
  2. //
  3. // Assignment 5 - Functions
  4. //
  5. // Name: Carlos Dominguez
  6. //
  7. // Class: C Programming, Spring 2026
  8. //
  9. // Date: 03/01/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. // All functions are called by value
  16. //
  17. //********************************************************
  18.  
  19. #include <stdio.h>
  20.  
  21. // constants
  22. #define SIZE 5
  23. #define OVERTIME_RATE 1.5f
  24. #define STD_WORK_WEEK 40.0f
  25.  
  26. // function prototypes
  27. float getHours (long int clockNumber);
  28. void printHeader (void);
  29. void printEmp (long int clockNumber, float wageRate, float hours,
  30. float overtimeHrs, float grossPay);
  31.  
  32. // --- Functions prototypes for overtime, grosspay calculators---
  33. float calcOvertime(float hours);
  34. float calcGrossPay(float wageRate, float hours);
  35.  
  36. // ---summary function prototype ---
  37. // Prints totals and averages for Wage, Hours, OT, and Gross at the end of the table
  38. void printSummary(const float wageRate[], const float hours[], const float overtimeHrs[],
  39. const float grossPay[], int n);
  40.  
  41. int main(void)
  42. {
  43. /* Variable Declarations */
  44. long int clockNumber[SIZE] = {98401, 526488, 765349, 34645, 127615}; // Employee unique ID
  45. float grossPay[SIZE]; // gross pay
  46. float hours[SIZE]; // hours worked in a given week
  47. float overtimeHrs[SIZE]; // overtime hours
  48. float wageRate[SIZE] = {10.60f, 9.75f, 10.50f, 12.25f, 8.35f}; // hourly wage rate
  49. int i; // loop and array index
  50.  
  51. // process each employee
  52. for (i = 0; i < SIZE; ++i)
  53. {
  54. // Read in hours for employee
  55. hours[i] = getHours(clockNumber[i]);
  56.  
  57. // Calculate overtime hours
  58. overtimeHrs[i] = calcOvertime(hours[i]);
  59.  
  60. // Calculate gross pay
  61. grossPay[i] = calcGrossPay(wageRate[i], hours[i]);
  62. }
  63.  
  64. // print the header info
  65. printHeader();
  66.  
  67. // print out each employee
  68. for (i = 0; i < SIZE; ++i)
  69. {
  70. // Print all the employees - call by value
  71. printEmp(clockNumber[i], wageRate[i], hours[i],
  72. overtimeHrs[i], grossPay[i]);
  73. }
  74.  
  75. // print totals and averages for Wage, Hours, OT, and Gross ---
  76. printSummary(wageRate, hours, overtimeHrs, grossPay, SIZE);
  77.  
  78. return 0;
  79. } // main
  80.  
  81. //**************************************************************
  82. // Function: getHours
  83. //
  84. // Purpose: Obtains input from user, the number of hours worked
  85. // per employee and stores the result in a local variable
  86. // that is passed back to the calling function.
  87. //
  88. // Parameters: clockNumber - The unique employee ID
  89. //
  90. // Returns: hoursWorked - hours worked in a given week
  91. //
  92. //**************************************************************
  93.  
  94. float getHours (long int clockNumber)
  95. {
  96. float hoursWorked = 0.0f; // hours worked in a given week
  97.  
  98. // Read in hours for employee
  99. printf("\nEnter hours worked by employee # %06li: ", clockNumber);
  100. scanf("%f", &hoursWorked);
  101.  
  102. // return hours back to the calling function
  103. return hoursWorked;
  104. } // getHours
  105.  
  106. //**************************************************************
  107. // Function: printHeader
  108. //
  109. // Purpose: Prints the initial table header information.
  110. //
  111. // Parameters: none
  112. //
  113. // Returns: void
  114. //
  115. //**************************************************************
  116.  
  117. void printHeader (void)
  118. {
  119. printf("\n\n*** Pay Calculator ***\n");
  120. printf("\nClock# Wage Hours OT Gross\n");
  121. printf("-----------------------------------------------\n");
  122. } // printHeader
  123.  
  124. //*************************************************************
  125. // Function: printEmp
  126. //
  127. // Purpose: Prints out all the information for an employee
  128. // in a nice and orderly table format.
  129. //
  130. // Parameters:
  131. // clockNumber - unique employee ID
  132. // wageRate - hourly wage rate
  133. // hours - hours worked for the week
  134. // overtimeHrs - overtime hours worked in a week
  135. // grossPay - gross pay for the week
  136. //
  137. // Returns: void
  138. //
  139. //**************************************************************
  140.  
  141. void printEmp (long int clockNumber, float wageRate, float hours,
  142. float overtimeHrs, float grossPay)
  143. {
  144. // Print one row with neat alignment:
  145. // Clock# as 6 digits with leading zeros
  146. // Wage shown with 2 decimals
  147. // Hours and OT with 2 decimals
  148. // Gross with 2 decimals
  149. printf("%06li %6.2f %6.2f %6.2f %8.2f\n",
  150. clockNumber, wageRate, hours, overtimeHrs, grossPay);
  151. } // printEmp
  152.  
  153. //**************************************************************
  154. // Function: calcOvertime
  155. //
  156. // Purpose: Computes overtime hours (> 40 hours).
  157. //
  158. // Parameters:
  159. // hours - hours worked for the week
  160. //
  161. // Returns: overtime hours (0 if hours <= 40)
  162. //
  163. //**************************************************************
  164.  
  165. float calcOvertime(float hours)
  166. {
  167. if (hours > STD_WORK_WEEK)
  168. return hours - STD_WORK_WEEK;
  169. else
  170. return 0.0f;
  171. } // calcOvertime
  172.  
  173. //**************************************************************
  174. // Function: calcGrossPay
  175. //
  176. // Purpose: Computes gross pay given wage rate and hours,
  177. // applying time-and-a-half for hours beyond 40.
  178. //
  179. // Parameters:
  180. // wageRate - hourly wage rate
  181. // hours - hours worked for the week
  182. //
  183. // Returns: gross pay for the week
  184. //
  185. //**************************************************************
  186.  
  187. float calcGrossPay(float wageRate, float hours)
  188. {
  189. if (hours <= STD_WORK_WEEK)
  190. {
  191. return wageRate * hours;
  192. }
  193. else
  194. {
  195. float overtime = hours - STD_WORK_WEEK;
  196. float regularPay = wageRate * STD_WORK_WEEK;
  197. float overtimePay = wageRate * OVERTIME_RATE * overtime;
  198. return regularPay + overtimePay;
  199. }
  200. } // calcGrossPay
  201.  
  202. //**************************************************************
  203. // Function: printSummary
  204. //
  205. // Purpose: Prints totals and averages for Wage, Hours, OT, and Gross,
  206. // aligned with the existing table columns.
  207. //
  208. // Parameters:
  209. // wageRate[] - array of hourly wage rates
  210. // hours[] - array of weekly hours
  211. // overtimeHrs[] - array of weekly overtime hours
  212. // grossPay[] - array of weekly gross pay
  213. // n - number of employees
  214. //
  215. // Returns: void
  216. //
  217. //**************************************************************
  218.  
  219. void printSummary(const float wageRate[], const float hours[], const float overtimeHrs[],
  220. const float grossPay[], int n)
  221. {
  222. float totalWage = 0.0f; // sum of wage rates (note: rate, not pay)
  223. float totalHours = 0.0f; // total hours for all employees
  224. float totalOT = 0.0f; // total overtime hours for all employees
  225. float totalGross = 0.0f; // total gross pay for all employees
  226.  
  227. // Accumulate totals across all employees
  228. for (int i = 0; i < n; ++i) {
  229. totalWage += wageRate[i];
  230. totalHours += hours[i];
  231. totalOT += overtimeHrs[i];
  232. totalGross += grossPay[i];
  233. }
  234.  
  235. // Divider under the employee rows
  236. printf("-----------------------------------------------\n");
  237.  
  238. // Print totals
  239. printf("Totals: %6.2f %6.2f %6.2f %8.2f\n",
  240. totalWage, totalHours, totalOT, totalGross);
  241.  
  242. // Print averages
  243. printf("Averages:%6.2f %6.2f %6.2f %8.2f\n",
  244. totalWage / n, totalHours / n, totalOT / n, totalGross / n);
  245. } // printSummary
Success #stdin #stdout 0s 5308KB
stdin
51.0
42.5
37.0
45.0
0.0
stdout
Enter hours worked by employee # 098401: 
Enter hours worked by employee # 526488: 
Enter hours worked by employee # 765349: 
Enter hours worked by employee # 034645: 
Enter hours worked by employee # 127615: 

*** Pay Calculator ***

Clock#   Wage    Hours    OT       Gross
-----------------------------------------------
098401   10.60    51.00   11.00     598.90
526488    9.75    42.50    2.50     426.56
765349   10.50    37.00    0.00     388.50
034645   12.25    45.00    5.00     581.88
127615    8.35     0.00    0.00       0.00
-----------------------------------------------
Totals:   51.45   175.50   18.50    1995.84
Averages: 10.29    35.10    3.70     399.17