fork download
  1. #include <stdio.h>
  2.  
  3. // constants
  4. #define SIZE 5
  5. #define OVERTIME_RATE 1.5f
  6. #define STD_WORK_WEEK 40.0f
  7.  
  8. // function prototypes
  9. float getHours(long int clockNumber);
  10. void printHeader(void);
  11. void printEmp(long int clockNumber, float wageRate, float hours, float overtimeHrs, float grossPay);
  12. float calculateOvertime(float hours);
  13. float calculateGrossPay(float wageRate, float hours, float overtimeHrs);
  14.  
  15. int main() {
  16. /* Variable Declarations */
  17. long int clockNumber[SIZE] = {98401, 526488, 765349, 34645, 127615}; // Employee IDs
  18. float grossPay[SIZE]; // Gross pay
  19. float hours[SIZE]; // Hours worked in a given week
  20. float overtimeHrs[SIZE]; // Overtime hours
  21. float wageRate[SIZE] = {10.60, 9.75, 10.50, 12.25, 8.35}; // Hourly wage rate
  22. int i; // Loop variable
  23.  
  24. // Process each employee
  25. for (i = 0; i < SIZE; ++i) {
  26. // Read in hours for employee
  27. hours[i] = getHours(clockNumber[i]);
  28.  
  29. // Calculate overtime hours
  30. overtimeHrs[i] = calculateOvertime(hours[i]);
  31.  
  32. // Calculate gross pay
  33. grossPay[i] = calculateGrossPay(wageRate[i], hours[i], overtimeHrs[i]);
  34. }
  35.  
  36. // Print the header info
  37. printHeader();
  38.  
  39. // Print out each employee
  40. for (i = 0; i < SIZE; ++i) {
  41. printEmp(clockNumber[i], wageRate[i], hours[i], overtimeHrs[i], grossPay[i]);
  42. }
  43.  
  44. return 0;
  45. }
  46.  
  47. // Function to obtain hours worked from the user
  48. float getHours(long int clockNumber) {
  49. float hoursWorked;
  50. printf("\nEnter hours worked by emp # %06li: ", clockNumber);
  51. scanf("%f", &hoursWorked);
  52. return hoursWorked;
  53. }
  54.  
  55. // Function to calculate overtime hours
  56. float calculateOvertime(float hours) {
  57. return (hours > STD_WORK_WEEK) ? (hours - STD_WORK_WEEK) : 0.0f;
  58. }
  59.  
  60. // Function to calculate gross pay
  61. float calculateGrossPay(float wageRate, float hours, float overtimeHrs) {
  62. float regularPay = (hours - overtimeHrs) * wageRate;
  63. float overtimePay = overtimeHrs * wageRate * OVERTIME_RATE;
  64. return regularPay + overtimePay;
  65. }
  66.  
  67. // Function to print the table header
  68. void printHeader(void) {
  69. printf("\n\n*** Pay Calculator ***\n");
  70. printf("\n------------------------------------------------\n");
  71. printf(" Clock# Wage Hours OT Gross\n");
  72. printf("------------------------------------------------\n");
  73. }
  74.  
  75. // Function to print employee details
  76. void printEmp(long int clockNumber, float wageRate, float hours, float overtimeHrs, float grossPay) {
  77. printf(" %06li %5.2f %5.1f %5.1f %7.2f\n", clockNumber, wageRate, hours, overtimeHrs, grossPay);
  78. }
  79.  
  80.  
Success #stdin #stdout 0.01s 5284KB
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