Problem when trying to multiply matrices

3

I am trying to multiply matrices in the following way, without success.

package multiplicacionaleatoria;

import javax.swing.JOptionPane;

public class MultiplicacionAleatoria {

    public static void main(String args[]) {

        int n = Integer.parseInt(JOptionPane.showInputDialog("ingrese numero de filas de la primera matriz")); // x1

        int m = Integer.parseInt(JOptionPane.showInputDialog("ingrese numero de columnas de la primera matriz")); //x2

        int o = Integer.parseInt(JOptionPane.showInputDialog("ingrese numero de filas de la segunda  matriz")); //y1

        int p = Integer.parseInt(JOptionPane.showInputDialog("ingrese numero de columnas de la primera matriz"));//y2

        int[][] m1 = new int[n][o];
        int[][] m2 = new int[m][p];
        int[][] mR = new int[n][p];

        for (int i = 0; i < n; i++) {
            for (int j = 0; j < o; j++) {
                m1[i][j] = Integer.parseInt(JOptionPane.showInputDialog("numeros de la matriz 1 "));
                Integer.parseInt(JOptionPane.showInputDialog(m1[i][j] + " "));
            }

        }

        for (int i = 0; i < m; i++) {

            for (int j = 0; j < p; j++) {
                m2[i][j] = Integer.parseInt(JOptionPane.showInputDialog("numeros de la matriz 2 "));
                Integer.parseInt(JOptionPane.showInputDialog(m2[i][j] + " "));
            }

        }

        if (p == m) {
            JOptionPane.showMessageDialog (null,( "La matriz resultante es: "));

            for (int i = 0; i < n; i++) {

                for (int j = 0; j < p; j++) {

                    for (int h = 0; h < n; h++) {

                        mR[i][j] += m1[i][h] * m2[h][j];
                    }
                    Integer.parseInt(JOptionPane.showInputDialog(mR[i][j] + " "));
                }

            }
        } else {
          JOptionPane.showMessageDialog(null,("Los rangos de las matrices son incorrectos"));
        }
    }

}
    
asked by brayan de la hoz 12.09.2016 в 03:35
source

1 answer

3

Errors detected:

  • n is the number of rows in the first matrix and m the number of columns; so you should use int[][] m1 = new int[n][m];
  • A similar situation occurs with matrix 2.
  • To multiply matrices the columns of the first matrix and the rows of the second matrix must be equal. With what the correct check is: if (m == o)
  • Using m , n , o and p as names of variables leads to confusion. This is not an error in itself but makes it easier to comment on errors. Better to use more descriptive variable names.

This is the corrected code:

public class MultiplicacionAleatoria {

    public static void main(String args[]) {

        int m1Filas = Integer.parseInt(JOptionPane.showInputDialog("ingrese numero de filas de la primera matriz")); // x1

        int m1Columnas = Integer.parseInt(JOptionPane.showInputDialog("ingrese numero de columnas de la primera matriz")); //x2

        int m2Filas = Integer.parseInt(JOptionPane.showInputDialog("ingrese numero de filas de la segunda  matriz")); //y1

        int m2Columnas = Integer.parseInt(JOptionPane.showInputDialog("ingrese numero de columnas de la primera matriz"));//y2

        int[][] m1 = new int[m1Filas][m1Columnas];
        int[][] m2 = new int[m2Filas][m2Columnas];
        int[][] mR = new int[m1Filas][m2Columnas];

        for (int i = 0; i < m1Filas; i++) {
            for (int j = 0; j < m1Columnas; j++) {
                m1[i][j] = Integer.parseInt(JOptionPane.showInputDialog(
                        "Introduzca numero matriz 1 fila=" + i + " columna=" + j ));
            }

        }

        for (int i = 0; i < m1Columnas; i++) {

            for (int j = 0; j < m2Columnas; j++) {
                m2[i][j] = Integer.parseInt(JOptionPane.showInputDialog(
                        "Introduzca numero matriz 2 fila=" + i + " columna=" +j ));
            }

        }

        if (m1Columnas == m2Filas) {
            JOptionPane.showMessageDialog (null,( "La matriz resultante es: "));

            for (int i = 0; i < m1Filas; i++) {

                for (int j = 0; j < m2Columnas; j++) {

                    for (int h = 0; h < m2Filas; h++) {

                        mR[i][j] += m1[i][h] * m2[h][j];
                    }
                    Integer.parseInt(JOptionPane.showInputDialog(mR[i][j] + " "));
                }

            }
        } else {
          JOptionPane.showMessageDialog(null,("Los rangos de las matrices son incorrectos"));
        }
    }

}

I have removed the second showInputDialog that is in the data entry because I think it does not contribute anything. And as far as showing the result, the interface you use is very uncomfortable, I would change it, but I understand that it goes beyond the question.

    
answered by 12.09.2016 в 10:03