fork download
  1. //Amari Mosley CSC5 Chapter 6, P. 371, #12
  2. //
  3. /*****************************************************************
  4.  *
  5.  * Calculate Days Out
  6.  * _______________________________________________________________
  7.  * This program calculates the average number of days a company's
  8.  * employees were absent during the past year.
  9.  *
  10.  * Computation is based on the formula:
  11.  * average = totalDays / numEmployees
  12.  * _______________________________________________________________
  13.  * INPUT
  14.  * numEmployees : number of employees
  15.  * daysMissed : days each employee missed
  16.  *
  17.  * OUTPUT
  18.  * average : average number of days absent per employee
  19.  *
  20.  *****************************************************************/
  21. #include <iostream>
  22. #include <iomanip>
  23. using namespace std;
  24.  
  25. // Function prototypes
  26. int getNumEmployees();
  27. int getTotalDays(int numEmployees);
  28. double calcAverage(int numEmployees, int totalDays);
  29.  
  30. int main()
  31. {
  32. int numEmployees; // number of employees
  33. int totalDays; // total days absent for all employees
  34. double average; // average days absent per employee
  35.  
  36. // Get the number of employees
  37. numEmployees = getNumEmployees();
  38.  
  39. // Get the total days absent
  40. totalDays = getTotalDays(numEmployees);
  41.  
  42. // Calculate the average
  43. average = calcAverage(numEmployees, totalDays);
  44.  
  45. // Display the result
  46. cout << fixed << setprecision(2);
  47. cout << "\nThe average number of days absent per employee is "
  48. << average << endl;
  49.  
  50. return 0;
  51. }
  52.  
  53.  
  54. // getNumEmployees
  55. // Asks the user for the number of employees, validates the
  56. // input (must be at least 1), and returns it as an int.
  57.  
  58. int getNumEmployees()
  59. {
  60. int numEmployees;
  61.  
  62. cout << "Enter the number of employees in the company: ";
  63. cin >> numEmployees;
  64.  
  65. // Input validation: no number less than 1
  66. while (numEmployees < 1)
  67. {
  68. cout << "The number of employees must be at least 1. "
  69. << "Please re-enter: ";
  70. cin >> numEmployees;
  71. }
  72.  
  73. return numEmployees;
  74. }
  75.  
  76.  
  77. //getTotalDays
  78. // Receives the number of employees, asks for the days each
  79. // employee missed, validates the input (must not be negative),
  80. // and returns the total days absent as an int.
  81.  
  82. int getTotalDays(int numEmployees)
  83. {
  84. int total = 0; // accumulator
  85. int days; // days missed by one employee
  86.  
  87. for (int count = 1; count <= numEmployees; count++)
  88. {
  89. cout << "Enter the days missed by employee #" << count << ": ";
  90. cin >> days;
  91.  
  92. // Input validation: no negative days
  93. while (days < 0)
  94. {
  95. cout << "Days missed cannot be negative. Please re-enter: ";
  96. cin >> days;
  97. }
  98.  
  99. total += days;
  100. }
  101.  
  102. return total;
  103. }
  104.  
  105.  
  106. // calcAverage
  107. // Receives the number of employees and the total days absent
  108. // and returns the average days absent as a double.
  109. // (No screen output and no user input in this function.)
  110.  
  111. double calcAverage(int numEmployees, int totalDays)
  112. {
  113. return static_cast<double>(totalDays) / numEmployees;
  114. }
Success #stdin #stdout 0s 5328KB
stdin
3
2
4
3
stdout
Enter the number of employees in the company: Enter the days missed by employee #1: Enter the days missed by employee #2: Enter the days missed by employee #3: 
The average number of days absent per employee is 3.00