c++ - 当我尝试计算平均绝对偏差时,为什么我的程序返回 inf?

标签 c++ for-loop

当我调用 getDeviation 函数时,它返回的只是“inf”,我不确定为什么。而且我认为问题出在 getDeviation 函数的 for 循环中,但我不确定它有什么问题。我也是 C++ 的初学者,所以如果我搞砸了很多东西,我很抱歉。

#include <iostream>
#include <cmath>

using namespace std;

double getAverage(int amount, int numbers[])
{
    // Declare the variables
    double total = 0;
    double avg = 0;
    //Find each number in the array then add it to the total
    for (int i = 0; i < amount; ++i) {
        total += numbers[i];
    }
    //Divide the total by the amount to get the average
    avg = total / amount;
    cout << "The Average is: " << avg << "\n";
    //Return back to the functions original state
    return 0;
}

double getDeviation(int amount, int numbers[], double avg)
{
    //Declare the variables and arrays
    int absoluteNums[1000] = {};
    double tempNum = 0;
    double absoNum = 0;
    double total = 0;
    double deviation = 0;

    //Search for each number, subtract the mean, make it an absolute value, then store it in another array
    for (int i = 0; i < amount; ++i) {
        tempNum += numbers[i];
        absoNum = abs(tempNum);
        numbers[i] = absoNum;
        numbers[i] -= avg;
        total += numbers[i];
    }

    //Divide the total by the average to get the deviation
    if (avg == 0) {
        return 0;
    }
    else {
        deviation = total / avg;
        cout << "The Mean Absolute Deviation is: " << deviation << "\n";
    }
    //Return back to the functions original state
    return 0;
}

int main()
{
    // Declare the variables and arrays
    int varNum = 1;
    int totVar = 0;
    int userNums[1000] = {};
    double avg;
    //Ask user for how many variables they want then record it
    cout << "How many variables would you like to have? (Max 999) ";
    cin >> totVar;
    //Check if totVar is to big
    if (totVar >= 1000) {
        cout << "To big. Nice try though!\n";
        return 0;
    }

    //Ask the user for each variable, then record it into the array
    for (int i = 0; i < totVar; ++i) {
        cout << "Please input variable " << varNum << ": ";
        cin >> userNums[i];
        varNum++;
    }
    //Make the avg value the average of the numbers using the getAverage function
    avg = getAverage(totVar, userNums);
    //Call the average function
    getAverage(totVar, userNums);
    //Call the deviation function
    getDeviation(totVar, userNums, avg);
    //Return back to the functions original state
    return 0;
}

最佳答案

1.

totalgetDeviation() 初始化之前使用。

您应该始终在使用变量之前对其进行初始化。

double absoNum = 0;
double total = 0;
double deviation = 0;

2.

getAverage() 在最后一行返回 0。它应该返回 avg

3.

getDeviation() 在最后一行返回 0。它应该返回偏差

关于c++ - 当我尝试计算平均绝对偏差时,为什么我的程序返回 inf?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41668963/

相关文章:

c++ - 为什么共享库可以定位为一个用户,而不是另一个?

c++ - 矩形交点。为空路口打印消息

c++ - 如何以及何时正确使用 *this 指针和参数匹配?

Java程序跳过for循环

java - 如何在 Java 中使用 Scanner、while 和 for 循环来递增字符串名称

c++ - 命名空间和运算符解析

c++ - move 构造函数可以接受类本身以外的参数吗?

r - 如何在R中没有for循环的情况下将一列与一系列相关的虚拟变量进行比较

c++ - 我该如何进一步优化这个循环?

python - 修复 python/Bioinformatics 中的代码