fork(1) download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. //Always use MACROS to define constants.
  4. #define MAX_EMPLOYEE 5
  5. #define MAX_NAME_LEN 50
  6.  
  7. //You can name function parameters same as local vairables because they have different scopes so they wont collide.
  8. void Prompt(char** name, float* rate, float* hours)
  9. {
  10. for (int i = 0, j = 0; i < MAX_EMPLOYEE; i++, j++)
  11. {
  12. printf("Enter name: ");
  13. scanf("%s", name + (i + j));
  14.  
  15. printf("Enter hourly rate: ");
  16. scanf("%f", rate + i);
  17. if (rate[i] == -1)
  18. {
  19. break;
  20. }
  21.  
  22. printf("Enter hours worked: ");
  23. scanf("%f", hours + i);
  24. if (hours[i] == -1)
  25. {
  26. break;
  27. }
  28. }
  29. //return; No need to return here.
  30. }
  31.  
  32. void GrossPay(float* grosspay, float* basepay, float* overtimepay, float* rate, float* hours)
  33. {
  34. for (int i = 0; i < MAX_EMPLOYEE; i++)
  35. {
  36. if (hours[i] > 40)
  37. {
  38. overtimepay[i] = (hours[i] - 40) * (rate[i]) * 1.5;
  39. basepay[i] = rate[i] * hours[i];
  40. grosspay[i] = rate[i] * hours[i] + (hours[i] - 40) * (rate[i]) * 1.5;
  41.  
  42. }
  43.  
  44. else
  45. {
  46. overtimepay[i] = 0;
  47. basepay[i] = rate[i] * (hours[i]);
  48. grosspay[i] = basepay[i];
  49.  
  50. }
  51. }
  52.  
  53. //return; No need to return from void function.
  54. }
  55.  
  56. //When passing array as arguments use arr* instead of arr[]
  57. void* Taxes(float* taxes_owed, float* gross_pay)
  58. {
  59. for (int i = 0; i < MAX_EMPLOYEE; i++)
  60. {
  61. taxes_owed[i] = 0.2 * gross_pay[i];
  62. }
  63. //return taxes_owed; //you cannot return 5 tax values either return single value or whole array containing those values.
  64. }
  65.  
  66. float CalculateTotal(float* gross_pay)
  67. {
  68. float total_pay = 0.0f;//0.0f for floats only.
  69.  
  70. for (int i = 0; i < MAX_EMPLOYEE; i++)
  71. {
  72. total_pay += gross_pay[i];
  73. }
  74.  
  75. return total_pay;
  76. }
  77.  
  78. void PrintOutput(char** first_name, float* rate, float* hours, float* gross_pay, float* base_pay, float* overtime_pay, float* taxes_owed, float* net_pay, float total_pay)
  79. {
  80. for (int i = 0, j = 0; i < MAX_EMPLOYEE; i++, j++)
  81. {
  82. printf("\nPay to: %s\n", first_name + (i + j));
  83. printf("Hours worked: %5.1f\n", hours[i]);
  84. printf("Hourly rate: $%5.2f\n", rate[i]);
  85. printf("Gross pay: $%5.2f \n", gross_pay[i]);
  86. printf("Base pay: $%5.2f \n", base_pay[i]);
  87. printf("Overtime pay: $%5.2f\n", overtime_pay[i]);
  88. printf("Taxes paid: $%5.2f\n", taxes_owed[i]);
  89. printf("Net pay: $%5.2f\n\n", net_pay[i]);
  90. }
  91. printf("Total paid to all employees= $%5.2f\n", total_pay);
  92. }
  93.  
  94. int main()
  95. {
  96. char first_name[MAX_EMPLOYEE][MAX_NAME_LEN] = { NULL };
  97. float rate[MAX_EMPLOYEE] = { 0.0f };
  98. float hours[MAX_EMPLOYEE] = { 0.0f };
  99. float gross_pay[MAX_EMPLOYEE] = { 0.0f };
  100. float base_pay[MAX_EMPLOYEE] = { 0.0f };
  101. float overtime_pay[MAX_EMPLOYEE] = { 0.0f };
  102. float taxes_owed[MAX_EMPLOYEE] = { 0.0f };
  103. float net_pay[MAX_EMPLOYEE] = { 0.0f };
  104. float total_pay = 0.0f;
  105.  
  106. Prompt(first_name, rate, hours);
  107. GrossPay(gross_pay, base_pay, overtime_pay, rate, hours);
  108. Taxes(taxes_owed, gross_pay);
  109.  
  110. total_pay = CalculateTotal(gross_pay);
  111.  
  112. for (int i = 0; i < MAX_EMPLOYEE; i++)
  113. net_pay[i] = gross_pay[i] - taxes_owed[i];
  114.  
  115. PrintOutput(first_name, rate, hours, gross_pay, base_pay, overtime_pay, taxes_owed, net_pay, total_pay);
  116. return 0;
  117. }
  118.  
Success #stdin #stdout 0s 4512KB
stdin
emp1
10
20
emp2
15
20
emp3
15
25
emp4
20
25
emp5
25
40
stdout
Enter name: Enter hourly rate: Enter hours worked: Enter name: Enter hourly rate: Enter hours worked: Enter name: Enter hourly rate: Enter hours worked: Enter name: Enter hourly rate: Enter hours worked: Enter name: Enter hourly rate: Enter hours worked: 
Pay to: emp1
Hours worked:  20.0
Hourly rate: $10.00
Gross pay: $200.00 
Base pay: $200.00 
Overtime pay: $ 0.00
Taxes paid: $40.00
Net pay: $160.00


Pay to: emp2
Hours worked:  20.0
Hourly rate: $15.00
Gross pay: $300.00 
Base pay: $300.00 
Overtime pay: $ 0.00
Taxes paid: $60.00
Net pay: $240.00


Pay to: emp3
Hours worked:  25.0
Hourly rate: $15.00
Gross pay: $375.00 
Base pay: $375.00 
Overtime pay: $ 0.00
Taxes paid: $75.00
Net pay: $300.00


Pay to: emp4
Hours worked:  25.0
Hourly rate: $20.00
Gross pay: $500.00 
Base pay: $500.00 
Overtime pay: $ 0.00
Taxes paid: $100.00
Net pay: $400.00


Pay to: emp5
Hours worked:  40.0
Hourly rate: $25.00
Gross pay: $1000.00 
Base pay: $1000.00 
Overtime pay: $ 0.00
Taxes paid: $200.00
Net pay: $800.00

Total paid to all employees= $2375.00