I'm trying to print a matrix diagonally, that is, the one in
1 2 3
4 5 6
7 8 9
print 1 2 4 3 5 7 6 8 9
I have this, in which I only print up to 7 because if it does not leave the matrix:
public class matriz {
public static void main(String args[]) {
int[][] matriz = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
imprimirMatriz(matriz);
}
public static void imprimirMatriz(int[][] matriz) {
int lado = matriz.length;
int x = 0;
int y = 0;
for (int i = 0; i < lado; i++) {
x = i;
y = 0;
while (x >= 0) {
System.out.print(matriz[y][x] + " ");
x--;
y++;
}
}
}
}
Another problem I have is to print a part of the matrix, print the lower triangular of the same in reverse, where you have to print 9 8 7 5 4 1, print me 9 8 5 7 4 1, inserting two numbers of position. The code:
public class matrizmas {
public static void main(String args[]) {
int[][] matriz = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
imprimirMatriz(matriz);
}
public static void imprimirMatriz(int[][] matriz) {
int lado = matriz.length;
for (int i = lado - 1; i >= 0; i--) {
for (int j = lado - 1; j >= i; j--) {
System.out.print(matriz[j][i]);
}
}
System.out.println();
System.out.println();
}
}