【问题标题】:C++ Finding local maxima in 2d arrayC ++在二维数组中查找局部最大值
【发布时间】:2015-04-06 23:32:49
【问题描述】:

我正在尝试编写一个程序来查找并打印此二维数组中的所有局部最大值,仅查看第二列。认为我在正确的轨道上,但不知道如何进行,并且没有给出正确的输出。谢谢。

int main()
{
float array[7][2] = { { 1, 22 }, { 2, 15 }, { 3, 16 }, { 4, 14 }, { 5, 13 }, {6,19}, {7,12} };
int i;
float before = 0, after = 0, localmax = 0;
int Index = 0;

for (i = 0; i<7; i++)
{
    if ((array[i][1] >= before) && (array[i][1] >= after))
    {
        before = array[i-1][1];
        after = array[i + 1][1];
        localmax = array[i][1];
        Index = i;
    }
}

cout << "The local maxima in the array are " << localmax << endl;
cout << "The corresponding values in the array are " << array[Index][0] << endl;
_getch();
return 0;

}

【问题讨论】:

  • 它给出了什么输出,你期望的输出是什么?
  • 局部最大值为 15,但显然需要 22、16 和 19
  • "所有局部最大值",但你的输出是一个单一的 'localmax' ......那么它是什么?
  • 是的,我承认我犯了一个错误,因为我毕竟是最大值

标签: c++ arrays


【解决方案1】:

您正在覆盖您的(单个)浮动 localmax。

您的问题有两种解决方案:

编号1:每次找到一个时都可以打印localmax(将cout放在for循环中)

int main()
{
float array[7][2] = { { 1, 22 }, { 2, 15 }, { 3, 16 }, { 4, 14 }, { 5, 13 }, {6,19}, {7,12} };
int i;
float before = 0, after = 0, localmax = 0;
int Index = 0;

for (i = 0; i<7; i++)
{
    if ((array[i][1] >= before) && (array[i][1] >= after))
    {
        before = array[i-1][1];
        after = array[i + 1][1];
        localmax = array[i][1];
        cout << "A local maxima is: " << localmax << endl;
        Index = i;
    }
}

_getch();
return 0;
}

编号2:您创建一个局部最大值向量并使用 push_back 保存您找到的任何局部最大值。

    int main()
{
float array[7][2] = { { 1, 22 }, { 2, 15 }, { 3, 16 }, { 4, 14 }, { 5, 13 }, {6,19}, {7,12} };
int i;
float before = 0, after = 0, localmax = 0;
int Index = 0;
std::vector<float> localMaxVector;

for (i = 0; i<7; i++)
{
    if ((array[i][1] >= before) && (array[i][1] >= after))
    {
        before = array[i-1][1];
        after = array[i + 1][1];
        localMaxVector.push_back(array[i][1]);
        Index = i;
    }
}

cout << "The local maxima in the array are " << endl;
for( std::vector<float>::const_iterator i = localMaxVector.begin(); i != localMaxVector.end(); ++i)
std::cout << *i << ' ';
 _getch();
return 0;
}

【讨论】:

  • 请注意,这只是为了让您了解应该做什么。我只是复制了您的代码并修改了相关部分。我很确定您的程序应该在执行任何操作之前崩溃,因为您似乎正在尝试在第一次迭代中访问 array[-1]。
【解决方案2】:

您在设置“之前”和“之后”之前没有验证数组索引,我很惊讶您的代码没有崩溃(i-1 和 i+1)。

我没有太多时间,所以我没有尝试过,但它应该可以工作。

int main()
{
    float array[7][2] = { { 1, 22 }, { 2, 15 }, { 3, 16 }, { 4, 14 }, { 5, 13 }, { 6, 19 }, { 7, 12 } };
    int i;
    float before = 0, after = 0;
    int Index = 0;

    for (i = 0; i<7; i++)
    {
        if (i > 0)
        {
            before = array[i-1][1];
        }
        if (i < 6)
        {
            after = array[i+1][1];
        }
        if ((i == 0 || array[i][1] >= before) && (i == 6 or array[i][1] >= after))
        {
            //when you're at the very first point, you don't have to verify the 'before' variable, and for the very last point, you don't have to verify 'after'
            cout << array[i][1] << " at position " << i << " is a maxima" << endl;
        }
    }

    _getch();
    return 0;
}

如果要保留结果,可以像 Thomas 一样使用 std::vector。

【讨论】:

    【解决方案3】:

    我不认为你的循环是正确的。如果您在数组的开头插入一个元素 {0, 20},您的代码将返回 19 和 22(假设当 i == 0 时 'before' 保持为 0)。我希望你想要 22、16、19。如果是这样,你的循环应该是这样的:

    for (i = 0; i<7; i++)
    {
        if ((i == 0 || array[i][1] > array[i - 1][1]) && (i == 6 || array[i][1] >= array[i + 1][1]))
        {
            localmax = array[i][1];
            Index = i;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2017-07-17
      • 2017-04-03
      • 2012-04-21
      • 2015-04-06
      • 2019-03-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多