Not able to get the average for output

I am trying to run this program and I can't seem to get the average output on screen. Here is the question along with my code.

Write a program that calculates the average number of days a company s employees
are absent. The program should have the following functions:
* A function called by main that asks the user for the number of employees in the
company. This value should be returned as an int. (The function accepts no
arguments.)
* A function called by main that accepts one argument: the number of employees in
the company. The function should ask the user to enter the number of days each
employee missed during the past year. The total of these days should be returned
as an int.
* A function called by main that takes two arguments: the number of employees in
the company and the total number of days absent for all employees during the
year. The function should return, as a double, the average number of days
absent. (This function does not perform screen output and does not ask the user
for input.)
Input Validation: Do not accept a number less than 1 for the number of employees.
Do not accept a negative number for the days any employee missed.

My code:

// Days Out is the programm that calculates the average number of days a company's
// employees are absent

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include <iostream>
using namespace std;

//Function prototypes
int numberEmployees();
int numberDays(int);
double averageDays(int, int);

int main()
{
//Declaring variables
int employees;
int total;
double average;

//Function call for first function
employees = numberEmployees();

//Function call for second function
total = numberDays(employees);

//Function call for last function prototype
average = averageDays(employees, total);


//Performing output by main function
cout<< "The average number of days a company's employees are absent is: " <<average<<endl;
return 0;
}

//Function header for number of employees
int numberEmployees()
{
int workers;
cout<<"Enter the number of employees in the company: ";
cin>>workers;

//Input validation
if(workers<=1)
{
cout<<" Do not accept number less than 1. Please, enter again: ";
cin>>workers;
}
return workers;
}

//Function header for the number of days
int numberDays(int w)
{
int workers = w;
int total = 0;
int absent;
//Creating a loop for every employees' missed days
for (int count=0; count<workers; count++)
{
cout <<"Enter the number of days each employee missed during past year: "<<count+1<<endl;
cin >>absent;
total+=absent;

//Input Validation
if (absent<0)
{
cout<<"Please, do not enter negative number! Try again: ";
cin>>absent;
}

}
return total;
}

//Function header for average number of days absent
double averageDays (int work, int totl)
{
int w = work;
int t = totl;
double aver;

aver=(w*365)/t;
return aver;
}
One thing is sure:
(int * 365) / int is (int * int) / int is int / int is int
and integer division drops remainder.

aver=(w*365.0)/t; is (int*double)/int is floating point division. Much better here.


Consider this. You have 10 workers and only one of them was one day off. Your "average" will be 3650. Case 2. Only 1 worker, but was absent 365 days. ==> Average of 1?

Just in case some harm would come to the 365, here is another way to enforce floating division:
aver = static_cast<double>(w) / t;
Hey thank you soo much. I have tried both, this time the average shows on the output but as soon as it shows on the scree, it crashes; I tried using system ("pause"), still no luck.
what do i do ?
The function should return, as a double, the average number of days absent.


Based on the above, I think you want average per employee.
Meaning if all employees take 5 days off, the average is 5.
aver=(t/w);

also
if(workers<=1)
should be
while(workers<=1)

This one probably needs to be changed too
if (absent<0)
Last edited on
Topic archived. No new replies allowed.