2
I need to make two matrices multiply, but I’m wrong when it comes to multiplying.
int ma[][] = new int [3][2];
int mb[][] = new int [2][2];
int mab[][] = new int [3][2];
for (int i = 0; i < ma.length; i++) {
for (int j = 0; j < ma[i].length; j++) {
System.out.println("Digite o elemento da linha " + (i + 1) + " e coluna " + (j + 1) + " da matriz 1");
ma[i][j] = new Scanner(System.in).nextInt();
}
}
for (int i = 0; i < mb.length; i++) {
for (int j = 0; j < mb[i].length; j++) {
System.out.println("Digite o elemento da linha " + (i + 1) + " e coluna " + (j + 1) + " da matriz 2");
mb[i][j] = new Scanner(System.in).nextInt();
}
}
mab[1][1] = (ma[1][1] * mb[1][1]) + (ma[1][2] * mb[2][1]);
mab[1][2] = (ma[1][1] * mb[1][2]) + (ma[1][2] * mb[2][2]);
mab[2][1] = (ma[2][1] * mb[1][1]) + (ma[2][2] * mb[1][2]);
mab[2][2] = (ma[2][1] * mb[1][2]) + (ma[2][2] * mb[2][2]);
mab[3][1] = (ma[3][1] * mb[1][1]) + (ma[3][2] * mb[2][1]);
mab[3][2] = (ma[3][1] * mb[1][2]) + (ma[3][2] * mb[2][2]);
System.out.println("Multipliacação das matrizes:");
for (int i = 0; i < mab.length; i++) {
for (int j = 0; j < mab[i].length; j++) {
System.out.println(mab[i][j] + "\t");
}
}
In netbeans, it indicates that from the error on line 26, which is where you have the first multiplication, this message appears:
Exception in thread "main" java.lang.Arrayindexoutofboundsexception: 2 At multiplication.Multiplication.main(Multiplication.java:26)
Did I miss the operation? I’m new to java
That’s right, thank you, thank you very much
– Pedro Henrique Kuzminskas