簡體   English   中英

C ++在二維數組中查找局部最大值

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

我正在嘗試編寫一個程序,該程序僅在第二列中查找並打印此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。

您的問題有兩種解決方案:

Nr。 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。 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”和“ 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 :: vector。

我認為您的循環不正確。 如果在數組的開頭插入元素{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;
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM