未初始化数组中的最小值和最大值

时间:2017-04-09 19:13:41

标签: c++ arrays

任务是将降雨量持续12个月并将其存放在一个阵列中。然后根据收集的数据计算总和,平均值,最小值和最大值。我找到了总和,平均就好了。我不知道我的最大值和最小值出错了。

#include <iostream>
using namespace std; 

int main()
{
    //inititalizing variables 
    double rain[13];
    double sum = 0;
    double max = rain[0];
    double min = rain[0];
    int count = 0;
    //string highMonth, lowMonth;

    //inputing rain values for each month 
    for (int i = 1; i < 13; i++)
    {
        cout << "Enter the rainfall (in inches) for month #" << i << ":";
        cin >> rain[13];
        sum = sum + rain[13];
    }

    //finding the max  
    for ( count = 1; count < 13; count++)
    {
        if (rain[count] > max)
        {
            max = rain[count];
        }
    }
    //finding the min 
    for (count = 1; count < 13; count++)
    {
        if (rain[count] < min)
        {
            min = rain[count]; 
        }
    }

    //printing results 
    cout << " The total rainfall for the year is" << sum<<endl; 
    cout << "The average rainfall is: " << sum / 12<<endl; 
    cout << "The month with the highest rainfall was " << max <<  endl;
    cout << "The month with the lowest rainfall was " << min << endl;
    return 0; 
}

4 个答案:

答案 0 :(得分:1)

首先,您应该将自己的循环视为cin >> rain[i],而不是rain[13]。 应使用现有值初始化min / max

您的数组未初始化,因此您指向无用的值。

您可以执行以下操作:

double max = 0;
double min = std::numeric_limits<double>::max();

答案 1 :(得分:1)

I recommend using std::array and range-based for loops in C++11. This is way less error prone.

#include <array>
#include <iostream>

int main()
{
    //storage variable
    std::array<double,12> rain;

    //inputing rain values for each month
    double sum = 0;
    unsigned i = 0;
    for (auto &p : rain)
    {
        std::cout << "Enter the rainfall (in inches) for month #" << ++i << ": ";
        std::cin >> p;
        sum += p;
    }

    //finding the max
    double max = rain[0];
    for (auto &p : rain)
    {
        if (p > max)
        {
            max = p;
        }
    }

    //finding the min
    double min = rain[0];
    for (auto &p : rain)
    {
        if (p < min)
        {
            min = p;
        }
    }

    //printing results 
    std::cout << "The total rainfall for the year is " << sum << "\n";
    std::cout << "The average rainfall is: " << sum / 12 << "\n"; 
    std::cout << "The month with the highest rainfall was " << max <<  "\n";
    std::cout << "The month with the lowest rainfall was " << min << "\n";
}

Or even better, just use the STL.

#include <algorithm>
#include <array>
#include <iostream>

int main()
{
    //storage variable
    std::array<double,12> rain;

    //inputing rain values for each month
    unsigned i = 0;
    for (auto &p : rain)
    {
        std::cout << "Enter the rainfall (in inches) for month #" << ++i << ": ";
        std::cin >> p;
    }

    //finding the min, max, average
    auto max = *std::max_element(std::begin(rain), std::end(rain));
    auto min = *std::min_element(std::begin(rain), std::end(rain));
    auto sum = std::accumulate(std::begin(rain), std::end(rain), double{0.0});

    //printing results 
    std::cout << "The total rainfall for the year is " << sum << "\n";
    std::cout << "The average rainfall is: " << sum / 12 << "\n"; 
    std::cout << "The month with the highest rainfall was " << max <<  "\n";
    std::cout << "The month with the lowest rainfall was " << min << "\n";
}

答案 2 :(得分:0)

// ...
double rain[13]; // uninitialized here
double sum = 0;
double max = rain[0]; // garbage
double min = rain[0]; // garbage
// ...
初始化rain[0]max时,

min指向垃圾。在将有意义的数据读入max后,您应该声明并初始化minrain

答案 3 :(得分:0)

这样做:

double max = rain[0];
double min = rain[0];
在未初始化的数组中的

与在其上放置一些垃圾值相同 变量最大 min ,您必须初始化为其他值,并且阵列下雨的内容相同。

下一个问题是:

for (int i = 1; i < 13; i++)
    {
        cout << "Enter the rainfall (in inches) for month #" << i << ":";
        cin >> rain[13];
        sum = sum + rain[13];
    }

您正在尝试在数组中的索引13处写入,这肯定超出了您的范围..

cin >> rain[i];

&#34; i&#34;是正确的事情。从0到13独家

另一方面,用于查找最大值和最小值的循环不完整(循环12次忽略索引0处的元素)

for ( count = 1; count < 13; count++)

如果您的数组有13个元素,则必须执行

for ( count = 0; count < 13; count++)