fork download
  1. //********************************************************
  2. //
  3. // Assignment 5 - Functions
  4. //
  5. // Name: <Azan Butt>
  6. //
  7. // Class: C Programming, <Spring 2025>
  8. //
  9. // Date: <03/1/2025>
  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,float overtimeHrs, float grossPay);
  30. float calculateOvertime(float hours);
  31. float calculateGrossPay(float wageRate, float hours, float overtimeHrs);
  32.  
  33. // complete: Add other function prototypes here as needed
  34.  
  35. int main()
  36. {
  37.  
  38. /* Variable Declarations */
  39.  
  40. long int clockNumber[SIZE] = {98401,526488,765349,34645,127615}; // ID
  41. float grossPay[SIZE]; // gross pay
  42. float hours[SIZE]; // hours worked in a given week
  43. int i; // loop and array index
  44. float overtimeHrs[SIZE]; // overtime hours
  45. float wageRate[SIZE] = {10.60,9.75,10.50,12.25,8.35}; // hourly wage rate
  46.  
  47. // process each employee
  48. for (i = 0; i < SIZE; ++i){
  49.  
  50. // Read in hours for employee
  51. hours[i] = getHours (clockNumber[i]);
  52.  
  53. overtimeHrs[i] = calculateOvertime(hours[i]);
  54.  
  55. grossPay[i] = calculateGrossPay(wageRate[i], hours[i], overtimeHrs[i]);
  56.  
  57. }
  58.  
  59. // print the header info
  60. printHeader();
  61.  
  62. // print out each employee
  63. for (i = 0; i < SIZE; ++i){
  64.  
  65. // Print all the employees - call by value
  66. printEmp(clockNumber[i], wageRate[i], hours[i], overtimeHrs[i], grossPay[i]);
  67.  
  68. }
  69.  
  70. return (0);
  71.  
  72. } // main
  73.  
  74. //**************************************************************
  75. // Function: getHours
  76. //
  77. // Purpose: Obtains input from user, the number of hours worked
  78. // per employee and stores the result in a local variable
  79. // that is passed back to the calling function.
  80. //
  81. // Parameters: clockNumber - The unique employee ID
  82. //
  83. // Returns: hoursWorked - hours worked in a given week
  84. //
  85.  
  86.  
  87.  
  88.  
  89.  
  90. // complete: Add other functions here as needed
  91. // ... remember your comment block headers for each function
  92.  
  93. float getHours(long int clockNumber) {
  94. float hoursWorked;
  95. printf("\nEnter hours worked by employee # %06li: ", clockNumber);
  96. scanf("%f", &hoursWorked);
  97. return hoursWorked;
  98. }
  99.  
  100. // Function to calculate overtime hours
  101. float calculateOvertime(float hours) {
  102. return (hours > STD_WORK_WEEK) ? (hours - STD_WORK_WEEK) : 0.0f;
  103. }
  104.  
  105. // Function to calculate gross pay
  106. float calculateGrossPay(float wageRate, float hours, float overtimeHrs) {
  107. float regularPay = (hours - overtimeHrs) * wageRate;
  108. float overtimePay = overtimeHrs * wageRate * OVERTIME_RATE;
  109. return regularPay + overtimePay;
  110. }
  111.  
  112. // Function to print the table header
  113. void printHeader(void) {
  114. printf("\n\n*** Pay Calculator ***\n");
  115. printf("\n------------------------------------------------\n");
  116. printf(" Clock# Wage Hours OT Gross\n");
  117. printf("------------------------------------------------\n");
  118. }
  119.  
  120. // Function to print employee details
  121. void printEmp(long int clockNumber, float wageRate, float hours, float overtimeHrs, float grossPay) {
  122. printf(" %06li %5.2f %5.1f %5.1f %7.2f\n", clockNumber, wageRate, hours, overtimeHrs, grossPay);
  123. }
  124.  
  125.  
Success #stdin #stdout 0s 5272KB
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.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