So, I have a 2D matrix, and I'm trying to print the values โโfrom the largest to the smallest. I basically do this, always looking for max, and when I find it, I set this position equal 1to adjacencyMatrixso that we don't recount it again. The problem is that when I checked the code that it started correctly, printed the largest, and then skipped the second largest. Then he found the 3rd and 4th largest. We skipped a few more, and then, finally, we just started typing 0 s.
Here is my code:
public static void findLongestPath(int rows, int columns, int[][] elevationMatrix, int[][] adjacencyMatrix)
{
int max = 0;
for (int x = 0; x < rows * columns; x++)
{
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < columns; j++)
{
if (elevationMatrix[i][j] > max && adjacencyMatrix[i][j] == 0)
{
max = elevationMatrix[i][j];
adjacencyMatrix[i][j] = 1;
}
}
}
System.out.println(max);
max = 0;
}
}
I looked at it for a while and cannot find a mistake, therefore, although another pair of eyes can help.
P.S. , , , , . .