当我运行此代码时,我似乎不断下降平均值,并且想知道是否有人有任何建议尝试的一个例子就是做了5个员工,所有人都错过了5天并且平均得到15个,我在做数学错误?再次感谢您的帮助。我发布了完整的代码,以防它不是我试图尽可能整洁的功能,所以很抱歉,如果它有点凌乱,编程不到一个月。
#include<iostream>
using namespace std;
function prototypes
//return type: void
//parameter type: 1 int by refrence
//purpose: This function asks the user for the number of employees in the company.
void GetNumEmployees(int&);
//return type: int
//parameter type: 1 int
//Purpose: The function should as the user to enter the number of days each employee missed during the past year
int TotalDaysMissed(int);
//return type: float
//parameters: 2 int
//Purpose: Returns the average of total number of days missed for all employees in the company during the year
float AverageDaysMissed(int, int);
int main()
{
//Declare and Initilize Variables
int empnum = 0, daysmissed = 0 ;
float averagedays = 0.0 ;
GetNumEmployees(empnum) ;
daysmissed = TotalDaysMissed(empnum) ;
averagedays = AverageDaysMissed(empnum, daysmissed) ;
cout<<"The Average Work Days your Employees Missed is "<<averagedays<<endl ;
return 0;
}
//function definitions
void GetNumEmployees(int &emp)
{
do
{
cout<<"Enter the number of Employees in the company: ";
cin>>emp ;
if(emp < 1)
cout<<"Invalid. Cannot be Less than 1\n\n";
}
while (emp < 1) ;
}
int TotalDaysMissed(int empn)
{
int daysmissed = 0 ;
int total = 0 ;
for(int n = empn; n > 0 ; n--)
{
do
{
cout<<"How Many days did Employee "<<n<< " miss? " ;
cin>>daysmissed ;
total += daysmissed;
if(daysmissed < 0)
cout<<"Invalid days must be a Positive Number\n\n";
}
while(daysmissed < 0) ;
}
return total;
}
float AverageDaysMissed(int empn, int daystotal)
{
float average = 0.0 ;
average = (empn + daystotal) / 2.0 ;
return average;
}
答案 0 :(得分:0)
您正在错误地计算平均值。
您有5名员工,每人都错过了5天。
(5 + 5 + 5 + 5 + 5)/ 5 = 5
你的平均值是5。
因此,如果您有5名员工按顺序错过了几天:4,3,2,4,5。 然后你的平均值=(4 + 3 + 2 + 4 +5)/ 5 = 3.6
平均值是所有观测值的总和除以观测值的数量。