在数组C++中排序最大和最小值

Sec*_*ake 5 c++ arrays sorting algorithm loops

这是一个非常简单和常见的练习,虽然我遇到了一个我似乎无法理解的错误,我无法在任何地方找到解释,因为它可能过于具体.

该程序只是提示用户输入人1到10吃了多少煎饼然后打印出被人吃掉的最大煎饼数量.我的问题是,"手工制作的循环"理清最大和最小值的作品,但该算法(它强烈建议在这个论坛上,而不是使用手工制作的循环)不打印出正确的最大价值,但适用于最小的.

这是我的代码:

void pancakes() {
    int pan[11];
    int small, big;
    for (int i = 1; i < 11; i++)  // counts to 11-1 and prompts user for pancakes
                                  // eaten by person 1==>10
    {
        cout << "How many pancakes did person " << i << " eat?\n";
        cin >> pan[i];
    }

    big = small = pan[1];  // assigns element to be highest or lowest value

    for (int i = 1; i < 11; i++) {
        if (pan[i] > big)  // compare biggest value with current "big" element
        {
            big = pan[i];
        }
        if (pan[i] < small)  // compares smallest value with current "small" element
        {
            small = pan[i];
        }
    }
    cout << "The person who ate the most pancakes ate " << big << " of them."
             << endl;  // prints biggest value
    cout << "The person who ate the least pancakes ate " << small << " of them."
             << endl;  // prints smallest value

    auto minmax = minmax_element(begin(pan), end(pan));

    cout << "min element " << *(minmax.first) << "\n";
    cout << "max element " << *(minmax.second) << "\n";
}   
Run Code Online (Sandbox Code Playgroud)

这是控制台返回的内容:

How many pancakes did person 1 eat?
45
How many pancakes did person 2 eat?
64
How many pancakes did person 3 eat?
7
How many pancakes did person 4 eat?
34
How many pancakes did person 5 eat?
87
How many pancakes did person 6 eat?
45
How many pancakes did person 7 eat?
89
How many pancakes did person 8 eat?
32
How many pancakes did person 9 eat?
55
How many pancakes did person 10 eat?
66
The person who ate the most pancakes ate 89 of them.
The person who ate the least pancakes ate 7 of them.
min element 7
max element 1606416304
Run Code Online (Sandbox Code Playgroud)

vso*_*tco 9

auto minmax = minmax_element(begin(pan), end(pan));
Run Code Online (Sandbox Code Playgroud)

确实找到了min/max,但C++中的数组索引从0 int pan[11];开始.从1索引开始填充,

big=small=pan[1]; //assigns element to be highest or lowest value; change to pan[0]
for (int i = 1; i < 11; i++){...} // change to i=0
Run Code Online (Sandbox Code Playgroud)

所以pan[0]将包含垃圾(在你的情况下,价值1606416304)将由minmax_element.

实际上,从未初始化的变量读取是C和C++中未定义的行为,任何事情都可能发生,尽管大多数时候您只是阅读了存储在该内存地址的内容.

如果你使用C++ 11(你现在应该使用它),那么你也可以使用基于范围的for循环来处理煎饼:)

for(auto& pancake: pan) // note the reference, we are reading
{
    cin >> pancake; // to read
}
Run Code Online (Sandbox Code Playgroud)

for(auto pancake: pan)
{
    // further processing here, like
    if(pancake < small) { small = pancake;} // etc
}
Run Code Online (Sandbox Code Playgroud)


gsa*_*ras 7

你有一个大小为11的数组,但你只循环10个元素,留下第一个元素未初始化.这意味着它包含垃圾(未定义的行为),在这种情况下1606416304,这是最大值,不是吗?=)

从以下位置更改循环:

for (int i = 1; i < 11; i++)
Run Code Online (Sandbox Code Playgroud)

至:

for (int i = 0; i < 11; i++)
Run Code Online (Sandbox Code Playgroud)

std :: minmaxelement()然后就像你想要的那样工作.


后果:

通常,使用能够提供与预期结果不同的函数的一个常见错误是检查您提供该函数的数据.这样你就知道数据是否有问题或/和功能.在您的情况下,打印阵列会让您明白您的数据不正常!

  • 你不必提示Person 0,在你的print语句中你可以将`<< i <<`改为`<< i + 1 <<` (2认同)