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. 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.
There are two solutions to your problem:
Nr. 1: You could print localmax everytime you find one (put the cout in the for loop)
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: You create a localmax vector and use push_back to save any local maxima you find.
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).
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.
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). I expect you want 22, 16, 19. If so, your loop should look like this:
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;
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.