C++在二维数组中寻找局部最大值

C++ Finding local maxima in 2d array

本文关键字:最大值 寻找局 二维数组 C++      更新时间:2023-10-16

我正试图编写一个程序,只查看第二列,查找并打印这个2D数组中的所有局部最大值。我认为我走在了正确的轨道上,但不知道如何继续,它也没有给出正确的输出。谢谢

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;

}

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

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

编号。1:每次找到localmax(将cout放入for循环)时,都可以打印localmax

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:创建一个localmax向量,并使用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;
}

在设置"before"answers"after"之前,您没有验证数组索引,我很惊讶您的代码没有崩溃(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::向量。

我认为您的循环不正确。如果在数组的开头插入元素{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;
    }
}