平均2个函数
Average of 2 functions
当我运行这段代码时,我似乎一直在偏离平均值,并想知道是否有人有任何建议。尝试过的一个例子是,只做了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 1nn";
}
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 Numbernn";
}
while(daysmissed < 0) ;
}
return total;
}
float AverageDaysMissed(int empn, int daystotal)
{
float average = 0.0 ;
average = (empn + daystotal) / 2.0 ;
return average;
}
您计算的平均值不正确。
您有5名员工,他们每人都缺席了5天。
(5+5+5+5)/5=5
你的平均成绩是5。
因此,如果你有5名员工按顺序错过了工作日:4、3、2、4、5。那么你的平均值=(4+3+2+4+5)/5=3.6
平均值是所有观测值的总和除以观测值的数量。
相关文章:
- 当在同一名称空间中有两个具有相同签名的函数时,会发生什么
- 在两个类中共享相同的函数调用,并在不需要时避免空实例化
- 我需要将多个函数组合为一个函数
- 为字符串中每 N 个字符插入空格的函数没有按照我认为的方式工作?
- 在不传递参数数量且只有3个点的情况下,如何使用变差函数
- STL算法函数在多个一维容器上的使用
- 使用cpp中的结构和函数的多个学生条目
- 具有多个模板的模板函数,用于特定数据类型(如字符串)?
- 运行相同函数名称C++的多个类的列表
- 重载 -> shared_ptr 个实例中的箭头运算符<interface>,接口中没有纯虚拟析构函数
- 用C++编写多级内联函数是个好主意吗?
- 将函数两个向量合并为第三个向量
- 如何获取指向函数第一个参数的指针
- 函数多个参数的模板
- 预处理器:生成具有动态名称的函数.多个定义问题
- 在头文件中定义c++函数是个好习惯吗?
- Std::set构造函数第二个参数错误
- 模板大小递归--构造函数多个重载
- 在c++中,在命名空间std中重载数学函数是个好做法吗?
- C++朋友函数两个数字的加法