2015-04-06 246 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; 
    } 
}