简体   繁体   English

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

[英]C++ Finding local maxima in 2d array

I am trying to write a program that finds and prints all the local maxima in this 2D array, looking at the 2nd column only. 我正在尝试编写一个程序,该程序仅在第二列中查找并打印此2D数组中的所有局部最大值。 Think I am on the right track but don't know how to proceed and it doesn't give the correct output. 认为我走在正确的道路上,但不知道如何进行,并且没有给出正确的输出。 Thanks. 谢谢。

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;

} }

You are overwriting your (single) float localmax. 您正在覆盖(单个)float localmax。

There are two solutions to your problem: 您的问题有两种解决方案:

Nr. Nr。 1: You could print localmax everytime you find one (put the cout in the for loop) 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;
}

Nr. Nr。 2: You create a localmax vector and use push_back to save any local maxima you find. 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;
}

You don't verify the array index before setting "before" and "after", I'm surprised your code didn't crash (i-1 and i+1). 在设置“ before”和“ after”之前,您无需验证数组索引,我很惊讶您的代码没有崩溃(i-1和i + 1)。

I don't have much time so I haven't tried it, but it should work. 我没有太多时间,所以我没有尝试过,但是应该可以。

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;
}

If you want to keep the results, you can use the std::vector like Thomas. 如果要保留结果,可以使用像Thomas一样的std :: vector。

I don't think your loop is correct. 我认为您的循环不正确。 If you inserted an element {0, 20} at the start of your array, your code would return 19 and 22 (assuming 'before' remains at 0 when i == 0). 如果在数组的开头插入元素{0,20},则代码将返回19和22(假设当i == 0时,“ before”保持为0)。 I expect you want 22, 16, 19. If so, your loop should look like this: 我希望您需要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;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM