fork download
  1. //
  2. // Pay Calculator using arrays
  3. // Calculates overtime hours and gross pay for SIZE employees
  4. //
  5. #include <stdio.h>
  6.  
  7. // constants to use
  8. #define SIZE 5 // number of employees to process
  9. #define STD_HOURS 40.0 // normal work week hours before overtime
  10. #define OT_RATE 1.5 // time and half overtime setting
  11.  
  12. int main(void)
  13. {
  14. // unique employee identifier (initialized)
  15. long int clockNumber[SIZE] = {98401, 526488, 765349, 34645, 127615};
  16.  
  17. // hourly pay for each employee (initialized)
  18. float wageRate[SIZE] = {10.6f, 9.75f, 10.5f, 12.25f, 8.35f};
  19.  
  20. // arrays to be filled / calculated
  21. float hours[SIZE]; // hours worked in a given week (read from input)
  22. float normalPay[SIZE]; // normal weekly pay without overtime
  23. float overtimeHrs[SIZE]; // overtime hours worked in a given week
  24. float overtimePay[SIZE]; // overtime pay for a given week
  25. float grossPay[SIZE]; // weekly gross pay - normal pay + overtime pay
  26.  
  27. int i;
  28.  
  29. printf("\n*** Pay Calculator ***\n\n");
  30.  
  31. // Read hours for each employee and compute pay
  32. for (i = 0; i < SIZE; i++)
  33. {
  34. // Prompt and read hours worked for employee
  35. printf("Enter number of hours worked for employee %06ld: ", clockNumber[i]);
  36. if (scanf("%f", &hours[i]) != 1) {
  37. // If input fails, set hours to 0 and clear stdin
  38. hours[i] = 0.0f;
  39. }
  40.  
  41. // Calculate overtime and gross pay for employee
  42. if (hours[i] >= STD_HOURS)
  43. {
  44. overtimeHrs[i] = hours[i] - STD_HOURS;
  45. normalPay[i] = STD_HOURS * wageRate[i];
  46. overtimePay[i] = overtimeHrs[i] * wageRate[i] * OT_RATE;
  47. }
  48. else // no OT
  49. {
  50. overtimeHrs[i] = 0.0f;
  51. normalPay[i] = hours[i] * wageRate[i];
  52. overtimePay[i] = 0.0f;
  53. }
  54.  
  55. // Calculate Gross Pay
  56. grossPay[i] = normalPay[i] + overtimePay[i];
  57. }
  58.  
  59. // Print a nice table header
  60. printf("\n--------------------------------------------------------------------------\n");
  61. printf(" Clock# Wage Hours OT Gross\n");
  62. printf("--------------------------------------------------------------------------\n");
  63.  
  64. // Print employee information from arrays
  65. for (i = 0; i < SIZE; i++)
  66. {
  67. // Clock# printed with leading zeros to match sample (6 digits)
  68. // Wage printed with 2 decimals, Hours with 1 decimal, OT with 1 decimal, Gross with 2 decimals
  69. printf(" %06ld %6.2f %6.1f %6.1f %8.2f\n",
  70. clockNumber[i],
  71. wageRate[i],
  72. hours[i],
  73. overtimeHrs[i],
  74. grossPay[i]);
  75. }
  76.  
  77. return 0;
  78. }
  79.  
Success #stdin #stdout 0.01s 5300KB
stdin
51.0
42.5
37.0
45.0
0.0
stdout
*** Pay Calculator ***

Enter number of hours worked for employee 098401: Enter number of hours worked for employee 526488: Enter number of hours worked for employee 765349: Enter number of hours worked for employee 034645: Enter number of hours worked for employee 127615: 
--------------------------------------------------------------------------
    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