2015-04-06 406 views
0

我正在嘗試編寫一個程序來查找並打印此二維數組中的所有局部最大值,僅查看第二列。認爲我在正確的軌道上,但不知道如何繼續,並沒有給出正確的輸出。謝謝。C++在二維數組中尋找局部最大值

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; 

}

+0

它給出了什麼輸出,你期望的輸出是什麼? – Erik

+0

本地最大值爲15,但很明顯需要22,16和19而不是 – America32

+0

「所有本地最大值」,但您的輸出是單個「localmax」......所以這是什麼? –

回答

0

您將要覆蓋(單)浮動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; 
} 
+0

請注意,這只是給你你應該做的事情的要點。我剛剛複製了你的代碼並修改了相關的部分。我很確定你的程序在做任何事之前應該會崩潰,因爲你好像在第一次迭代中試圖訪問array [-1]。 – Thomas

0

你不和「之後」,「之前」設置前驗證數組索引,我很驚訝,你的代碼沒有崩潰(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; 
} 

如果你想保留結果,你可以使用std :: vector像Thomas。

0

我不認爲你的循環是正確的。如果你在數組的開頭插入了一個元素{0,20},你的代碼將返回19和22(假設'=='前的'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; 
    } 
}