//Amari Mosley CSC5 Chapter 6, P. 371, #12
//
/*****************************************************************
*
* Calculate Days Out
* _______________________________________________________________
* This program calculates the average number of days a company's
* employees were absent during the past year.
*
* Computation is based on the formula:
* average = totalDays / numEmployees
* _______________________________________________________________
* INPUT
* numEmployees : number of employees
* daysMissed : days each employee missed
*
* OUTPUT
* average : average number of days absent per employee
*
*****************************************************************/
#include <iostream>
#include <iomanip>
using namespace std;
// Function prototypes
int getNumEmployees();
int getTotalDays(int numEmployees);
double calcAverage(int numEmployees, int totalDays);
int main()
{
int numEmployees; // number of employees
int totalDays; // total days absent for all employees
double average; // average days absent per employee
// Get the number of employees
numEmployees = getNumEmployees();
// Get the total days absent
totalDays = getTotalDays(numEmployees);
// Calculate the average
average = calcAverage(numEmployees, totalDays);
// Display the result
cout << fixed << setprecision(2);
cout << "\nThe average number of days absent per employee is "
<< average << endl;
return 0;
}
// getNumEmployees
// Asks the user for the number of employees, validates the
// input (must be at least 1), and returns it as an int.
int getNumEmployees()
{
int numEmployees;
cout << "Enter the number of employees in the company: ";
cin >> numEmployees;
// Input validation: no number less than 1
while (numEmployees < 1)
{
cout << "The number of employees must be at least 1. "
<< "Please re-enter: ";
cin >> numEmployees;
}
return numEmployees;
}
//getTotalDays
// Receives the number of employees, asks for the days each
// employee missed, validates the input (must not be negative),
// and returns the total days absent as an int.
int getTotalDays(int numEmployees)
{
int total = 0; // accumulator
int days; // days missed by one employee
for (int count = 1; count <= numEmployees; count++)
{
cout << "Enter the days missed by employee #" << count << ": ";
cin >> days;
// Input validation: no negative days
while (days < 0)
{
cout << "Days missed cannot be negative. Please re-enter: ";
cin >> days;
}
total += days;
}
return total;
}
// calcAverage
// Receives the number of employees and the total days absent
// and returns the average days absent as a double.
// (No screen output and no user input in this function.)
double calcAverage(int numEmployees, int totalDays)
{
return static_cast<double>(totalDays) / numEmployees;
}