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。
您的问题有两种解决方案:
编号。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;
}
}
相关文章:
- 比较并显示使用最小值(a,b)和最大值(a、b)升序排列的4个数字
- 使用指针从C++中的数组中获取最大值
- 如何将这个C++哈希表转换为动态扩展和收缩,而不是使用硬设置的最大值
- 如何创建一个函数来计算并返回平均值、最大值和最小值
- 在二维数组中查找最小值和最大值?
- 整数溢出,最大值为 pow(10,19)
- 以C++递归方式查找向量中的最大值
- C++ - 如何在结构向量中找到结构体一个成员的最大值?
- 查找数组中第一个最小值和最后一个最大值元素之间的算术平均值
- C++ 函数,用于查找传入的 N 个数字的平均值、总和、最小值和最大值
- 我的 If Else 语句无法在向量 (C++) 中提供最大值
- 如何使用可变参数模板类使用模板元编程获得最大值
- C++不同的最小最大值实现
- Vec3b:r/g/b 颜色的最大值为 254,而不是 255
- CUDA - 将 float3 数组的 (x,y,z) 分量的最小值/最大值分开?
- 是否可以将无符号 int 的最大值转换为 int 并将结果转换为 -1?
- C++在二维数组中寻找局部最大值
- 在结构数组中寻找最大值
- 寻找数和为最大值的路径
- 在二维数组中寻找最大值的快速算法