Foros aprenderaprogramar.com

Aprender a programar => C, C++, C#, Java, Visual Basic, HTML, PHP, CSS, Javascript, Ajax, Joomla, MySql y más => Mensaje iniciado por: ivan15 en 31 de Octubre 2015, 00:28

Título: Java sumar matrices método que recibe dos matrices y retorna la suma en otra
Publicado por: ivan15 en 31 de Octubre 2015, 00:28
Hola, me piden hacer un método que sume 2 matrices, y a mí se me ocurrió hacerlo así, pero a la hora de probarlo me tira error, me tira que el error está en

matrisResultado[j] = matrisA[j] + matrisB[ib][jb];

, pero no logro corregirlo, alguien puede darme una mano?

Código: [Seleccionar]
public int[][] sumarOtraMatris(int[][] matrisA , int[][] matrisB) {

int[][] matrisResultado = null;

if(matrisA.length == matrisB.length) {

for(int i = 0; i < matrisA.length ; i++) {
for(int ib = 0; ib < matrisB.length ; i++) {
for(int j = 0; j < matrisA[0].length ; j++) {
for(int jb = 0; jb < matrisB[0].length ; jb++) {

if(i==ib && j==jb) {

matrisResultado[i][j] = matrisA[i][j] + matrisB[ib][jb];

}

}

}


}

}

} else {

throw new Error("Las matrises deben tener la misma cantidad de filas que columnas");

}


return matrisResultado;
Título: Re:sumar matrises
Publicado por: Mastermind en 01 de Noviembre 2015, 21:30
Hola ten en cuenta que se escribe matriz con zeta y matrices si es plural

Tienes la algoritmia mal planteada si quieres hacerlo tú debes primero pensar cuál es la algoritmia del proceso.

Bases de programación y algoritmia: http://aprenderaprogramar.com/index.php?option=com_content&view=category&id=28&Itemid=59

Bases de la programación Java: http://aprenderaprogramar.com/index.php?option=com_content&view=category&id=68&Itemid=188

Este código suma dos matrices:

Código: [Seleccionar]
public class sumadorMatrices {

    public static void main (String [] Args) {
        int [][] matriz1 = new int[][] { {2,3,4}, {5,6,7} };
        int [][] matriz2 = new int[][] { {1,1,1}, {1,1,1} };
        sumarOtraMatriz (matriz1, matriz2);
    }

    public static int[][] sumarOtraMatriz(int[][] matrizA , int[][] matrizB) {
        int[][] matrizResultado;
        int filasA =matrizA.length;
        int columnasA = matrizA[0].length;

        int filasB =matrizB.length;
        int columnasB = matrizB[0].length;

        System.out.println("Primera matriz:");
        for (int i = 0; i < filasA; i++) {
            for (int j = 0; j < columnasA; j++) {
                System.out.print(matrizA[i][j] + "   ");
            }
            System.out.println("");
        }

        System.out.println("Segunda matriz:");
        for (int i = 0; i < filasA; i++) {
            for (int j = 0; j < columnasA; j++) {
                System.out.print(matrizA[i][j] + "   ");
            }
            System.out.println("");
        }

        if (filasA==filasB && columnasB==columnasA) {

            matrizResultado = new int[filasA][columnasA];
            for (int i = 0; i < filasA; i++) {
                for (int j = 0; j < columnasA; j++) {
                    matrizResultado[i][j] = matrizA[i][j] + matrizB[i][j];
                }
            }

        } else {
            throw new Error("Las matrices deben tener la misma cantidad de filas que columnas");
        }
        System.out.println("Matriz resultado:");
        for (int i = 0; i < filasA; i++) {
            for (int j = 0; j < columnasA; j++) {
                System.out.print(matrizResultado[i][j] + "   ");
            }
            System.out.println("");
        }
        return matrizResultado;
    }
}

;)
Título: Re:sumar matrises
Publicado por: ivan15 en 02 de Noviembre 2015, 02:58
hola gracias por tu respuesta, pero, que seria la algoritmia?
Título: Re:Java sumar matrices método que recibe dos matrices y retorna la suma en otra
Publicado por: Mastermind en 03 de Noviembre 2015, 10:02
La algoritmia son los fundamentos de la programación, la lógica para construir programas, puedes leer https://www.aprenderaprogramar.com/foros/index.php?topic=1313.0

Está explicado en http://aprenderaprogramar.com/index.php?option=com_content&view=category&id=28&Itemid=59

Saludos!