【问题标题】:Average of 2 functions [closed]2个函数的平均值[关闭]
【发布时间】:2015-03-22 23:40:17
【问题描述】:

当我运行这段代码时,我似乎一直在平均水平下降,想知道是否有人有任何建议,尝试过的一个例子就是让 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;
}

【问题讨论】:

  • average = (empn + daystotal) / 2.0 - 这不是平均值的计算方式

标签: c++ function average


【解决方案1】:

您计算的平均值不正确。

您有 5 名员工,他们每人都缺勤 5 天。

(5 + 5 + 5 + 5 + 5) / 5 = 5

你的平均成绩是 5。

因此,如果您有 5 名员工按以下顺序缺勤:4、3、2、4、5。 那么你的平均值 = (4 + 3 + 2 + 4 +5) / 5 = 3.6

平均值是所有观测值的总和除以观测值的数量。

【讨论】:

    猜你喜欢
    • 2018-09-26
    • 1970-01-01
    • 1970-01-01
    • 2020-07-06
    • 2020-04-25
    • 1970-01-01
    • 2017-01-17
    • 2023-03-12
    • 1970-01-01
    相关资源
    最近更新 更多