0

I was hoping that someone could help me out with this. For our program, we are supposed to write the following function

int reportMaxima( double array[ ] [ NCOLS ], int nRows, int nCols, double xMin,
double xMax, double yMin, double yMax );
Finds and prints out cells in the array that correspond to local maxima, defined as
cells that are no lower than all neighboring cells. For most cells, there will be four
neighbors, but for cells on the edges or in the corners, there will be 3 or 2 neighbors
respectively.

I know what the question asks but have no idea how to implement this in c++.
I know that there will be a couple of if statements within the function for the number of neighbors. My question is which loop you would use to find the values of the four neighboring cells. I'm thinking a for loop should be used but I can't see how I can actually use it. Any help would be appreciated.

I just now thought of using nested for loops. This is assuming that there are 4 neighboring cells.
Would something like this work?

for(i=0;i<1;i++)//for 1st neighboring cell on the top
{
//some code
for(j=0;j<2;j++;j++)// for the cell to the left and to the right of the cell in question
{
//some code to skip the cell in question. I don't know how to do this though.
}
for(k=0;k<1;k++)//for the cell below
{
//some code
}
}

Am I on the right track or way off here?

2
Contributors
1
Reply
3
Views
8 Years
Discussion Span
Last Post by dougy83
0

you're given:

int reportMaxima( double array[ ] [ NCOLS ], int nRows, int nCols, double xMin,
double xMax, double yMin, double yMax );

I assume the array is nRows x nCols in size. What are the xMin, xMax, yMin, yMax for?

Your loops should pass over all elements in each row for each column. e.g.

for(int row = 0; row < nRows; row++){
   for(int col = 0; col < nCols; col++){
      check neighbouring elements here... if row == 0 or row == nRows or col == 0 or col == nCols you're on an edge, and will check <4 neighbours. 
      display element if it's the local maxima
      }
   }
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.