fork download
  1. //********************************************************
  2. //
  3. // Assignment 5 - Functions
  4. //
  5. // Name: Robert Hyde
  6. //
  7. // Class: C Programming, Fall 2026
  8. //
  9. // Date: October 5, 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. float calculateOvertime (float hours);
  29. float calculateGrossPay (float wageRate, float hours, float overtimeHrs);
  30. void printHeader (void);
  31. void printEmp (long int clockNumber, float wageRate, float hours,
  32. float overtimeHrs, float grossPay);
  33.  
  34. int main()
  35. {
  36.  
  37. /* Variable Declarations */
  38.  
  39. long int clockNumber[SIZE] = {98401, 526488, 765349, 34645, 127615};
  40. float grossPay[SIZE]; // gross pay
  41. float hours[SIZE]; // hours worked in a given week
  42. int i; // loop and array index
  43. float overtimeHrs[SIZE]; // overtime hours
  44. float wageRate[SIZE] = {10.60, 9.75, 10.50, 12.25, 8.35};
  45.  
  46. // process each employee
  47. for (i = 0; i < SIZE; ++i)
  48. {
  49.  
  50. // Read in hours for employee
  51. hours[i] = getHours(clockNumber[i]);
  52.  
  53. // Calculate overtime hours
  54. overtimeHrs[i] = calculateOvertime(hours[i]);
  55.  
  56. // Calculate gross pay
  57. grossPay[i] = calculateGrossPay(wageRate[i], hours[i],
  58. overtimeHrs[i]);
  59.  
  60. } // for
  61.  
  62. // print the header info
  63. printHeader();
  64.  
  65. // print out each employee
  66. for (i = 0; i < SIZE; ++i)
  67. {
  68.  
  69. // Print all the employees - call by value
  70. printEmp(clockNumber[i], wageRate[i], hours[i],
  71. overtimeHrs[i], grossPay[i]);
  72.  
  73. } // for
  74.  
  75. return (0);
  76.  
  77. } // main
  78.  
  79.  
  80. //**************************************************************
  81. // Function: getHours
  82. //
  83. //**************************************************************
  84.  
  85. float getHours(long int clockNumber)
  86. {
  87.  
  88. float hoursWorked; // hours worked in a given week
  89.  
  90. // Read in hours for employee
  91. printf("\nEnter hours worked by emp # %06li: ", clockNumber);
  92. scanf("%f", &hoursWorked);
  93.  
  94. // return hours back to the calling function
  95. return (hoursWorked);
  96.  
  97. } // getHours
  98.  
  99.  
  100. //**************************************************************
  101. // Function: calculateOvertime
  102. //
  103. //**************************************************************
  104.  
  105. float calculateOvertime(float hours)
  106. {
  107.  
  108. float overtimeHrs;
  109.  
  110. if (hours > STD_WORK_WEEK)
  111. {
  112. overtimeHrs = hours - STD_WORK_WEEK;
  113. }
  114. else
  115. {
  116. overtimeHrs = 0.0f;
  117. }
  118.  
  119. return (overtimeHrs);
  120.  
  121. } // calculateOvertime
  122.  
  123.  
  124. //**************************************************************
  125. // Function: calculateGrossPay
  126. //
  127. //**************************************************************
  128.  
  129. float calculateGrossPay(float wageRate, float hours, float overtimeHrs)
  130. {
  131.  
  132. float regularHours;
  133. float grossPay;
  134.  
  135. if (hours > STD_WORK_WEEK)
  136. {
  137. regularHours = STD_WORK_WEEK;
  138. }
  139. else
  140. {
  141. regularHours = hours;
  142. }
  143.  
  144. grossPay = (regularHours * wageRate) +
  145. (overtimeHrs * wageRate * OVERTIME_RATE);
  146.  
  147. return (grossPay);
  148.  
  149. } // calculateGrossPay
  150.  
  151.  
  152. //**************************************************************
  153. // Function: printHeader
  154. //
  155. //**************************************************************
  156.  
  157. void printHeader(void)
  158. {
  159.  
  160. printf("\n\n*** Pay Calculator ***\n");
  161.  
  162. // print the table header
  163. printf("\n Clock# Wage Hours OT Gross\n");
  164. printf(" ------------------------------------------\n");
  165.  
  166. } // printHeader
  167.  
  168.  
  169. //**************************************************************
  170. // Function: printEmp
  171. //
  172. //**************************************************************
  173.  
  174. void printEmp(long int clockNumber, float wageRate, float hours,
  175. float overtimeHrs, float grossPay)
  176. {
  177.  
  178. // Print employee information in the required format
  179. printf(" %06li %6.2f %5.1f %6.1f %8.2f\n",
  180. clockNumber, wageRate, hours, overtimeHrs, grossPay);
  181.  
  182. } // printEmp
Success #stdin #stdout 0s 5248KB
stdin
51
42.5
37
45
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