fork download
  1. //********************************************************
  2. //
  3. // Assignment 6 - Structures
  4. //
  5. // Name: <Isaac Boahndao>
  6. //
  7. // Class: C Programming, <Spring Semester and 2025>
  8. //
  9. // Date: <03_08_25>
  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.  
  19. // Define and Includes
  20. #include <stdio.h>
  21.  
  22. // Define constants for the program
  23. #define NUM_EMPL 5
  24. #define STD_HOURS 40.0
  25. #define OT_RATE 1.5
  26.  
  27. // Define a struct for storing employee data
  28. struct employee
  29. {
  30. long int clockNumber; // employee ID number
  31. float wageRate; // hourly wage rate
  32. float hours; // hours worked in a week
  33. float overtimeHrs; // overtime hours worked in a week
  34. float grossPay; // gross pay for the week
  35. };
  36.  
  37. // Declare functions used in the program
  38. float getHours(long int clockNumber); // gets the number of hours worked by an employee
  39. void printHeader(void); // prints a header for the table of employee data
  40. void printEmp(struct employee emp); // prints a row of data for a single employee
  41.  
  42. int main()
  43. {
  44. // Declare an array of employee structs and initialize with data
  45. struct employee employees[NUM_EMPL] = {
  46. {98401, 10.60},
  47. {526488, 9.75},
  48. {765349, 10.50},
  49. {34645, 12.25},
  50. {127615, 8.35}
  51. };
  52.  
  53. // Loop through each employee, get their hours worked, and calculate their gross pay
  54. for (int i = 0; i < NUM_EMPL; ++i)
  55. {
  56. employees[i].hours = getHours(employees[i].clockNumber);
  57.  
  58. // If the employee worked overtime, calculate the overtime pay
  59. if (employees[i].hours > STD_HOURS)
  60. {
  61. employees[i].overtimeHrs = employees[i].hours - STD_HOURS;
  62. employees[i].grossPay = STD_HOURS * employees[i].wageRate
  63. + employees[i].overtimeHrs * employees[i].wageRate * OT_RATE;
  64. }
  65. // If the employee did not work overtime, calculate regular pay only
  66. else
  67. {
  68. employees[i].overtimeHrs = 0.0;
  69. employees[i].grossPay = employees[i].hours * employees[i].wageRate;
  70. }
  71. }
  72.  
  73. // Print a header for the table of employee data
  74. printHeader();
  75.  
  76. // Loop through each employee and print their data
  77. for (int i = 0; i < NUM_EMPL; ++i)
  78. {
  79. printEmp(employees[i]);
  80. }
  81.  
  82. return 0;
  83. }
  84.  
  85. // Function to get the number of hours worked by an employee
  86. float getHours(long int clockNumber)
  87. {
  88. float hoursWorked;
  89.  
  90. // Prompt the user to enter the number of hours worked by the employee
  91. printf("Enter hours worked by emp # %06li: ", clockNumber);
  92. scanf("%f", &hoursWorked);
  93.  
  94. return hoursWorked;
  95. }
  96.  
  97. // Function to print a header for the table of employee data
  98. void printHeader(void)
  99. {
  100. printf("\n*** Pay Calculator ***\n\n");
  101. printf("Clock# Wage Hours OT Gross\n");
  102. printf("--------------------------------------\n");
  103. }
  104.  
  105. // Function to print a row of data for a single employee
  106. void printEmp(struct employee emp)
  107. {
  108. // Print the employee's ID number, wage rate, hours worked, overtime hours worked, and gross pay
  109. printf("%06li %5.2f %5.1f %5.1f %7.2f\n",
  110. emp.clockNumber, emp.wageRate, emp.hours,
  111. emp.overtimeHrs, emp.grossPay);
  112. }
  113.  
  114.  
  115.  
  116.  
  117.  
  118.  
Success #stdin #stdout 0.01s 5288KB
stdin
Standard input is empty
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    0.0    0.0     0.00
526488   9.75    0.0    0.0     0.00
765349  10.50    0.0    0.0     0.00
034645  12.25    0.0    0.0     0.00
127615   8.35    0.0    0.0     0.00