fork download
  1. #include <stdio.h>
  2.  
  3. // constants to use
  4. #define SIZE 5 // number of employees to process
  5. #define STD_HOURS 40.0 // normal work week hours before overtime
  6. #define OT_RATE 1.5 // time and half overtime setting
  7.  
  8. int main()
  9. {
  10. // Declare variables needed for the program
  11. // unique employee identifier
  12. long int clockNumber [SIZE] = {98401, 526488, 765349, 34645, 127615};
  13.  
  14. float grossPay [SIZE]; // weekly gross pay - normal pay + overtime pay
  15. float hours [SIZE]; // hours worked in a given week
  16. int i; // loop and array index
  17. float normalPay [SIZE]; // normal weekly pay without any overtime
  18. float overtimeHrs[SIZE]; // overtime hours worked in a given week
  19. float overtimePay [SIZE]; // overtime pay for a given week
  20.  
  21. // hourly pay for each employee
  22. float wageRate [SIZE] = {10.6, 9.75, 10.5, 12.25, 8.35};
  23.  
  24. printf ("\n*** Pay Calculator ***\n\n");
  25.  
  26. // Process each employee one at a time
  27. for (i = 0; i < SIZE; i++)
  28. {
  29. // Prompt and Read in hours worked for employee
  30. printf("Enter hours worked for employee %06ld: ", clockNumber[i]);
  31. scanf("%f", &hours[i]);
  32.  
  33. // Calculate overtime and gross pay for employee
  34. if (hours[i] > STD_HOURS)
  35. {
  36. overtimeHrs[i] = hours[i] - STD_HOURS;
  37.  
  38. // Calculate arrays normalPay and overtimePay with overtime
  39. normalPay[i] = STD_HOURS * wageRate[i];
  40. overtimePay[i] = overtimeHrs[i] * (wageRate[i] * OT_RATE);
  41. }
  42. else // no OT
  43. {
  44. overtimeHrs[i] = 0.0;
  45.  
  46. // Calculate arrays normalPay and overtimePay without overtime
  47. normalPay[i] = hours[i] * wageRate[i];
  48. overtimePay[i] = 0.0;
  49. }
  50.  
  51. // Calculate Gross Pay
  52. grossPay[i] = normalPay[i] + overtimePay[i];
  53. }
  54.  
  55. // Print a nice table header
  56. printf("\n----------------------------------------------------------------------------------------\n");
  57. printf("%-10s %-10s %-10s %-12s %-12s %-12s %-10s\n",
  58. "Clock #", "Wage Rate", "Hours", "Normal Pay", "OT Hours", "OT Pay", "Gross Pay");
  59. printf("----------------------------------------------------------------------------------------\n");
  60.  
  61. // Access each employee and print to screen
  62. for (i = 0; i < SIZE; i++)
  63. {
  64. printf("%-10ld $%-9.2f %-10.1f $%-11.2f %-12.1f $%-11.2f $%-9.2f\n",
  65. clockNumber[i], wageRate[i], hours[i], normalPay[i],
  66. overtimeHrs[i], overtimePay[i], grossPay[i]);
  67. }
  68. printf("----------------------------------------------------------------------------------------\n");
  69.  
  70. return 0;
  71. }
Success #stdin #stdout 0s 5320KB
stdin
51.0
42.5
37.0
45.0
0.0
stdout
*** Pay Calculator ***

Enter hours worked for employee 098401: Enter hours worked for employee 526488: Enter hours worked for employee 765349: Enter hours worked for employee 034645: Enter hours worked for employee 127615: 
----------------------------------------------------------------------------------------
Clock #    Wage Rate  Hours      Normal Pay   OT Hours     OT Pay       Gross Pay 
----------------------------------------------------------------------------------------
98401      $10.60     51.0       $424.00      11.0         $174.90      $598.90   
526488     $9.75      42.5       $390.00      2.5          $36.56       $426.56   
765349     $10.50     37.0       $388.50      0.0          $0.00        $388.50   
34645      $12.25     45.0       $490.00      5.0          $91.88       $581.88   
127615     $8.35      0.0        $0.00        0.0          $0.00        $0.00     
----------------------------------------------------------------------------------------