Note: Knowledge is limited to arrays. Language is Java. Assignment has to be thr
ID: 3756857 • Letter: N
Question
Note: Knowledge is limited to arrays. Language is Java. Assignment has to be through Dr Java. Thank you!
13. Terrain analysis. Given an N-by-N grid of elevation values (in meters), a peak is a grid point for which all four neighboring cells are strictly lower. Write a code fragment that counts the number of peaks in a given N-by-N grid. Test your program on several sets of input. Note: when it says four neighboring cells, it means the ones above, below, to the right, and to the left of the cell (if those exist). For example, in the array below, the gray-shaded cells are the neighboring cells:Explanation / Answer
/**
*
* @author VISHAL
*/
public class TerrainAnalysis {
public static boolean CheckForPeak(int[][]A,int row, int col,int i,int j)
{
if((i==0 && j==0)&&(i<row && j<col))
{
System.out.println(i+" : "+j);
if((A[i][j]>A[i+1][j]) &&(A[i][j]>A[i][j+1]))
return true;
else
return false;
}
else if((i==row && j==0) &&(row>i && j<col))
{
System.out.println(i+" : "+j);
if((A[i][j]>A[i-1][j]) &&(A[i][j]>A[i][j+1]))
return true;
else
return false;
}
else if((i==row && j==col) &&(row>0 && col >0))
{
System.out.println(i+" : "+j);
if((A[i][j]>A[i][j-1]) &&(A[i][j]>A[i-1][j]))
return true;
else
return false;
}
else if((i==0 && j==col) &&(row>0 && col >0))
{
System.out.println(i+" : "+j);
if((A[i][j]>A[i][j-1]) &&(A[i][j]>A[i+1][j]))
return true;
else
return false;
}
else if((i==0 &&i <=row) && (j>0 && j<col))
{
System.out.println(i+" : "+j);
if((A[i][j]>A[i][j-1]) &&(A[i][j]>A[i][j+1]) && (A[i][j]>A[i+1][j]))
return true;
else
return false;
}
else if((i==row && (j>0 && j<col)))
{
System.out.println(i+" : "+j+" ::");
if((A[i][j]>A[i][j-1]) &&(A[i][j]>A[i][j+1]) && (A[i][j]>A[i-1][j]))
return true;
else
return false;
}
else if((j==0 && (i>0 && i<row)))
{
System.out.println(i+" : "+j);
if((A[i][j]>A[i][j+1]) &&(A[i][j]>A[i-1][j]) && (A[i][j]>A[i+1][j]))
return true;
else
return false;
}
else if((j==col && (i>0 && i<row)))
{
System.out.println(i+" : "+j);
if((A[i][j]>A[i][j-1]) &&(A[i][j]>A[i-1][j]) && (A[i][j]>A[i+1][j]))
return true;
else
return false;
}
else
{
System.out.println(i+" : "+j);
if((A[i][j]>A[i][j-1]) &&(A[i][j]>A[i-1][j]) && (A[i][j]>A[i+1][j]) && (A[i][j]>A[i][j+1]))
return true;
else
return false;
}
}
public static void FindAllPeks(int A[][],int row,int col)
{
for(int i=0; i<row;i++)
{
for(int j=0; j<col; j++)
{
if(CheckForPeak(A,row,col,i,j))
{
System.out.println(A[i][j]);
}
}
}
}
public static void main(String[]args)
{
int row=3,column=4;
int A[][]={{1,2,1,4},{5,1,7,2},{9,10,11,12}};
FindAllPeks(A,row-1,column-1);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.