OVH Cloud OVH Cloud

exercice on java (arrays)

12 réponses
Avatar
Ronie
hello,please I have a project to do in java concerning arrays. If
somebody knows the answer,please send it to my email adress
(ziyade4@hotmail.com),i'll be thankfull.
this is the exercice.


// Topographical maps are easily represented with two-dimensional
arrays of integer or real valued
heights. However, the bird's eye view they provide is sometimes
difficult to appreciate after a day of
lugging a 70-pound backpack.
Consider a two-dimensional array variable storing real numbers and
representing heights of a
topographical map.
Write a program that locates peaks. We define a peak as any point that
is higher than its eight
neighbors.
Write a program that locates valleys. A valley is any series of three
points along a row or column that
are lower than their twelve neighbors. (Can you deal with longer
valleys as well?)

1. Define a two-dimensional array type for holding real numbers and
having a size of maxrow times
maxcol;

2. Declare a Boolean method that tests if a point of this map
represents a peak;

3. From the main program locate all peaks and output their positions
and heights;

The header should be like this:

peak number Row position Column position
weight

10 réponses

1 2
Avatar
oziris
something like... (not tested)

public class TopographicalMap
{
public static final int MAXROW = 10;
public static final int MAWCOL = 10;
private double[][] map;


public TopographicalMap()
{
map = new double[maxrow][maxcol];
}


public boolean isPeak(int rowIndex, int colIndex)
{
if (rowIndex < 0 || rowIndex > MAXROW
|| colIndex < 0 || colIndex > MAXCOL)
{
throw new IllegalArgumentException("bad coordinates");
}

boolean isPeak = true;
pointWeight = map[rowIndex][colIndex];

/* point eight neighbors shifts */
int rowIndexShifts[] = new int[] {-1, -1, -1, 0, 0, 1, 1, 1};
int colIndexShifts[] = new int[] {-1, 0, 1, -1, 1, -1, 0, 1};

int i = 0;
while (isPeak && i < 8)
{
int neighborRowIndex = rowIndex + rowIndexShifts[i];
int neighborColIndex = colIndex + colIndexShifts[i];

/* we ignore points which are outside map */
if (neighborRowIndex >= 0 && neighborRowIndex < MAXROW
&& neighborColIndex >= 0 && neighborColIndex < MAXCOL)
{
isPeak = (pointWeight >
map[neighborRowIndex][neighborColIndex]);
}
i++;
}

return isPeak;
}


public double getMapValue(int rowIndex, int colIndex)
{
return map[rowIndex][colIndex];
}


public static void main(String[] args)
{
TopographicalMap tm = new TopographicalMap();
System.out.println(
"Peak number weighttRowPositiontColumnPosition");
for (int i = 0; i < TopographicalMap.MAXROW; i++)
{
for (int j = 0; j < TopographicalMap.MAXCOL; j++)
{
if (tm.isPeak(i, j))
{
System.out.println(
String.valueOf(tm.getMapValue(i, j)) + "tt"
+ String.valueOf(i) + "tt"
+ String.valueOf(j) + "tt";
}
}
}
}
}
Avatar
Ronie
thanks for you, you help me a lot.You did a wonderfull job
Avatar
Ronie
But I still need the one for valeys, It will be the same heading.
Thanks anyway.
Avatar
oziris
I'll deal with the valeys this evening.
Avatar
Ronie
ok thanks man.
Avatar
Cyril Siman
Ronie wrote:

ok thanks man.


Maybe you can post your question in : fr.comp.algorithmes.

Avatar
oziris
About the valleys there are lot of durty ways.
Avatar
Ronie
I need just one of them
Avatar
oziris
I don't want to write durty stuffs. I prefer to do nothing ;-)
Maybe other guy would like continue my job.

-o--
Avatar
Ronie
ok no problem
1 2