Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/131.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 统计计算器输出无限循环_C++ - Fatal编程技术网

C++ 统计计算器输出无限循环

C++ 统计计算器输出无限循环,c++,C++,我的程序设计为从用户那里获取3-30个值,并通过一些函数来计算平均值、中值和标准偏差。 然而,一旦我放置了一组测试用例值,我的输出就会导致一团混乱,我假设这是一个无限循环 我完全不知道我到底做错了什么。我将非常感谢任何帮助 #include <iostream> #include <iomanip> // for Setprecision #include <cmath> // for pow and sqrt using namespace std; i

我的程序设计为从用户那里获取3-30个值,并通过一些函数来计算平均值、中值和标准偏差。 然而,一旦我放置了一组测试用例值,我的输出就会导致一团混乱,我假设这是一个无限循环

我完全不知道我到底做错了什么。我将非常感谢任何帮助

#include <iostream>
#include <iomanip> // for Setprecision
#include <cmath> // for pow and sqrt

using namespace std;

int main()
{
    // Declare Local Variables
    const int SIZE=30.0;
    double array[SIZE];
    int count = 0;
}
//Module: inputArrayValues
//Description: Ask the user to input values between 3 and 30 elements for the array to hold. Also validates that the user inputs a total of values between 3 and 30.
void inputArrayValues (double array[], int SIZE, int &count)
{
    double number = 0;
    const double SENTINEL = -1.0;
    // Basic information about what the user can input. Does not repeat.
    cout << "Please enter values one at a time." <<endl;
    cout << "Up to a maximum of 30 values and a minimum of 3 values." << endl;
    cout << "Only positive values are accepted or the program will not work." << endl;
    cout << "With the exception, please enter the value -1.0 to stop entering values." << endl;

    // While Loop
    // Variable count is counting how many values the user inputs for later calculation
    for (int i = 0; i < SIZE; i++)
    {
        while (number!= SENTINEL)
        {
            cout << "Please enter a value or enter -1.0 to stop entering values" << endl;
            cin >> number;
            array[i] = number;
            if (number != SENTINEL)
            {
                count++;
            }

        }
    }

    if (count < 3 || count > SIZE)
    {
        cout << "Invalid total number of values." << endl;
        cout << "The total number of values must between 3 and 30 values." <<endl;
        cout << "This program will now close..." << endl;
        cout << "Thank you for using this program." << endl;
    }
}

//Function: comupteAverage
//Description: Computes the average of the given inputs.
double computeAverage (double array[], int count)
{
    double sum = 0.0;
    double resultA;
    for (int i =0; i < count; i++)
    {
        sum = sum + array[i];
    }
    resultA = sum / count;
    return resultA;
}
//Function: computeMedian
//Description: Computes the Median of the given inputs.
double computeMedian (double array[], int count)
{
    double resultM;
    if ((count % 2) == 0)
    {
        resultM = (array[count/2] + (array[count/2] -1.0) /2.0);
    }
    else
        resultM = array[count/2];
    return resultM;
}
//Function: computeSTD
//Description: Computes the Standard Deviation of the given inputs.
double computeSTD (double array[], int count, double average)
{
    double temp;
    double sum = 0;
    double resultV;
    for(int i = 0; i < count; i++)
    {
        temp = pow((array[i] - average), 2);
        sum = sum + temp;
    }
    //Account for Sample Standard Deviation N-1
    resultV = sqrt(sum/(count -1));
    return resultV;
}
但是,我仍然不清楚是什么原因导致程序以这种方式运行。

首先:

第二:调用函数的代码在哪里

第三:你的电脑平均值和电脑测试看起来不错,但你在computeMedian干什么呢

更新: 正确的中值计算应如下所示:

count = 0;
do
{
    cout << "Please enter a value or enter -1.0 to stop entering values" << endl;
    cin >> number;
    if ( number != SENTINEL )
    {
        array[ count ] = number;
        count++;
    }
}
while ( number != SENTINEL && count < SIZE );
double computeMedian (double array[], int count)
{
    double resultM;
    double * sorted_array = new double[ count ];
    for ( int i = 0; i < count; i++ )
    {
        sorted_array[ i ] = array[ i ];
    }
    std::sort( sorted_array, sorted_array + count );
    if ((count % 2) == 0)
    {
        //resultM = (sorted_array[count/2] + (sorted_array[count/2] -1.0) /2.0); // <-- compare this line
        resultM = ( sorted_array[count/2] + sorted_array[count/2 - 1] ) / 2.0;  // <-- and this
    }
    else
        resultM = sorted_array[count/2];
    delete []sorted_array;
    return resultM;
}

最后一点:我强烈建议您使用std::vector而不是原始数组。

这不是一个简短的代码示例。大多数代码与问题无关。看我的道歉!我发布了整个代码,因为我不知道是什么导致了它。请允许我缩小范围。谢谢你让我排队。你好!非常感谢你的建议。关于你的问题。我不得不缩短代码的显示量,使其不那么碍眼。模块调用工作得很好,对于computeMedian来说,我很遗憾不知道该怎么做,我能想到的最好办法就是在互联网上看到一些例子。如果你能在这一部分帮助我,我将不胜感激。看看Max Shawabkeh对这个问题的回答——这就是我基于中值函数的例子。但是,我还没有学会如何确定数组的确切长度。相反,我使用了一个count变量来跟踪用户输入值的次数。我不确定我能不能用这个方法,因为我的课上没有教过这个方法。有没有更简单的方法来表示这个例子?非常感谢你的帮助。非常感谢你的辛勤工作。遗憾的是,我不确定我是否可以使用这个例子,因为它包含许多我在课堂上没有学到的方面,比如std::。实际上,如果我用这个例子来学习如何计算中位数,我的教授很可能会给我打分。然而,我非常感谢你为帮助我所付出的努力。函数std::sort as-can-guest-from它的名称是将给定数组从最小元素排序到最大元素。你可以通过阅读自己写
count = 0;
do
{
    cout << "Please enter a value or enter -1.0 to stop entering values" << endl;
    cin >> number;
    if ( number != SENTINEL )
    {
        array[ count ] = number;
        count++;
    }
}
while ( number != SENTINEL && count < SIZE );
double computeMedian (double array[], int count)
{
    double resultM;
    double * sorted_array = new double[ count ];
    for ( int i = 0; i < count; i++ )
    {
        sorted_array[ i ] = array[ i ];
    }
    std::sort( sorted_array, sorted_array + count );
    if ((count % 2) == 0)
    {
        //resultM = (sorted_array[count/2] + (sorted_array[count/2] -1.0) /2.0); // <-- compare this line
        resultM = ( sorted_array[count/2] + sorted_array[count/2 - 1] ) / 2.0;  // <-- and this
    }
    else
        resultM = sorted_array[count/2];
    delete []sorted_array;
    return resultM;
}