
Recorrer matriz en vertical problema
Publicado por Pedro Javier (3 intervenciones) el 19/12/2014 21:58:43
El programa inicia una matriz en horizontal y vertical con un valor que le pasamos por parámetro la horizontal me la hace bien la vertical no.
A ver si alguien puede echarle un vistazo.
public static void main(String[] args) throws IOException {
int matriz[][] = new int[5][5];
System.out.println("****************************");
System.out.println("****** Iniciar Modo 1 ******");
System.out.println("****************************");
IniciarModo1(matriz, 1);
Mostrar(matriz);
System.out.println();
Reiniciar(matriz, 0);
System.out.println("****************************");
System.out.println("****** Iniciar Modo 2 ******");
System.out.println("****************************");
IniciarModo2(matriz, 1);
Mostrar(matriz);
}
private static void IniciarModo1(int[][] matriz, int num) {
int f, c, cant;
for (f = 0; f < matriz.length; f++) {
for (c = 0; c < matriz[f].length; c++) {
cant = matriz[0].length;
if (f == 0 && c == 0) {
matriz[0][0] = num;
}
if (f == 0 && c != 0) {
matriz[f][c] = matriz[f][c - 1] + 1;
}
if (f != 0 && c == 0) {
matriz[f][c] = matriz[f - 1][cant - 1] + 1;
}
if (f != 0 && c != 0) {
matriz[f][c] = matriz[f][c - 1] + 1;
}
}
}
}
private static void IniciarModo2(int[][] matriz, int num) {
int f, c, cuantas_fila;
for (f = 0; f < matriz.length; f++) {
for (c = 0; c < matriz[f].length; c++) {
cuantas_fila = matriz[f].length;
if (f == 0 && c == 0) {
matriz[f][c] = num;
}
if (f != 0 && c == 0) {
matriz[f][c] = matriz[f - 1][c] + 1;
}
if (f == 0 && c != 0) {
// matriz[f][c] = AQUI TENGO EL PROBLEMA QUE NO COJO DE LA ULTIMA FILA EL VALOR DE LA COLUMNA ANTERIOR
}
}
}
}
private static void Mostrar(int[][] matriz) {
int f, c;
for (f = 0; f < matriz.length; f++) {
for (c = 0; c < matriz[f].length; c++) {
System.out.print(matriz[f][c] + " ");
}
System.out.println();
}
}
private static void Reiniciar(int[][] matriz, int i) {
int f, c;
for (f = 0; f < matriz.length; f++) {
for (c = 0; c < matriz[f].length; c++) {
matriz[f][c] = 0;
}
}
}
}
Gracias de antemano.
Si podéis darme consejos de como optimizar el código más lo agradecería.
A ver si alguien puede echarle un vistazo.
public static void main(String[] args) throws IOException {
int matriz[][] = new int[5][5];
System.out.println("****************************");
System.out.println("****** Iniciar Modo 1 ******");
System.out.println("****************************");
IniciarModo1(matriz, 1);
Mostrar(matriz);
System.out.println();
Reiniciar(matriz, 0);
System.out.println("****************************");
System.out.println("****** Iniciar Modo 2 ******");
System.out.println("****************************");
IniciarModo2(matriz, 1);
Mostrar(matriz);
}
private static void IniciarModo1(int[][] matriz, int num) {
int f, c, cant;
for (f = 0; f < matriz.length; f++) {
for (c = 0; c < matriz[f].length; c++) {
cant = matriz[0].length;
if (f == 0 && c == 0) {
matriz[0][0] = num;
}
if (f == 0 && c != 0) {
matriz[f][c] = matriz[f][c - 1] + 1;
}
if (f != 0 && c == 0) {
matriz[f][c] = matriz[f - 1][cant - 1] + 1;
}
if (f != 0 && c != 0) {
matriz[f][c] = matriz[f][c - 1] + 1;
}
}
}
}
private static void IniciarModo2(int[][] matriz, int num) {
int f, c, cuantas_fila;
for (f = 0; f < matriz.length; f++) {
for (c = 0; c < matriz[f].length; c++) {
cuantas_fila = matriz[f].length;
if (f == 0 && c == 0) {
matriz[f][c] = num;
}
if (f != 0 && c == 0) {
matriz[f][c] = matriz[f - 1][c] + 1;
}
if (f == 0 && c != 0) {
// matriz[f][c] = AQUI TENGO EL PROBLEMA QUE NO COJO DE LA ULTIMA FILA EL VALOR DE LA COLUMNA ANTERIOR
}
}
}
}
private static void Mostrar(int[][] matriz) {
int f, c;
for (f = 0; f < matriz.length; f++) {
for (c = 0; c < matriz[f].length; c++) {
System.out.print(matriz[f][c] + " ");
}
System.out.println();
}
}
private static void Reiniciar(int[][] matriz, int i) {
int f, c;
for (f = 0; f < matriz.length; f++) {
for (c = 0; c < matriz[f].length; c++) {
matriz[f][c] = 0;
}
}
}
}
Gracias de antemano.
Si podéis darme consejos de como optimizar el código más lo agradecería.
Valora esta pregunta


0