fork download
  1. //*******************************************************
  2. //
  3. // Assignment 4 - Arrays
  4. //
  5. // Name: <replace with your name>
  6. //
  7. // Class: C Programming, <replace with Semester and Year>
  8. //
  9. // Date: <replace with the current date>
  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. //********************************************************
  16.  
  17. #include <stdio.h>
  18.  
  19. // constants to use
  20. #define SIZE 5 // number of employees to process
  21. #define STD_HOURS 40.0 // normal work week hours before overtime
  22. #define OT_RATE 1.5 // time and half overtime setting
  23.  
  24. int main()
  25. {
  26. // unique employee identifier (initialized)
  27. long int clockNumber[SIZE] = {98401, 526488, 765349, 34645, 127615};
  28.  
  29. // hourly pay for each employee (initialized)
  30. float wageRate[SIZE] = {10.6f, 9.75f, 10.5f, 12.25f, 8.35f};
  31.  
  32. // arrays to be filled/calculated
  33. float grossPay[SIZE];
  34. float hours[SIZE];
  35. float normalPay[SIZE];
  36. float overtimeHrs[SIZE];
  37. float overtimePay[SIZE];
  38.  
  39. int i;
  40.  
  41. printf("\n*** Pay Calculator ***\n\n");
  42.  
  43. /* Read hours for each employee and compute normal/overtime/gross pay */
  44. for (i = 0; i < SIZE; i++)
  45. {
  46. printf("Enter number of hours worked for employee %06ld: ", clockNumber[i]);
  47. if (scanf("%f", &hours[i]) != 1) {
  48. // If input fails, set hours to 0 and clear input state (simple handling)
  49. hours[i] = 0.0f;
  50. }
  51.  
  52. if (hours[i] >= STD_HOURS)
  53. {
  54. overtimeHrs[i] = hours[i] - STD_HOURS;
  55. normalPay[i] = STD_HOURS * wageRate[i];
  56. overtimePay[i] = overtimeHrs[i] * wageRate[i] * OT_RATE;
  57. }
  58. else
  59. {
  60. overtimeHrs[i] = 0.0f;
  61. normalPay[i] = hours[i] * wageRate[i];
  62. overtimePay[i] = 0.0f;
  63. }
  64.  
  65. grossPay[i] = normalPay[i] + overtimePay[i];
  66. }
  67.  
  68. /* Print report header */
  69. printf("\n-------------------------------------------------------------\n");
  70. printf(" Clock# Wage Hours OT Gross\n");
  71. printf("-------------------------------------------------------------\n");
  72.  
  73. /* Print each employee's row */
  74. for (i = 0; i < SIZE; i++)
  75. {
  76. /*
  77.   Formats:
  78.   - Clock#: zero-padded to 6 digits -> %06ld
  79.   - Wage: 2 decimal places -> %6.2f
  80.   - Hours / OT: 1 decimal place -> %6.1f
  81.   - Gross: 2 decimal places -> %8.2f
  82.   */
  83. printf(" %06ld %6.2f %6.1f %6.1f %8.2f\n",
  84. clockNumber[i],
  85. wageRate[i],
  86. hours[i],
  87. overtimeHrs[i],
  88. grossPay[i]);
  89. }
  90.  
  91. return 0;
  92. }
Success #stdin #stdout 0.01s 5320KB
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