This code is supposed to multiply 2 3x3 matrices in Java, however for some reaso
ID: 3728690 • Letter: T
Question
This code is supposed to multiply 2 3x3 matrices in Java, however for some reason it does not work, please explain why.
import java.util.Scanner;
public class MatrixMultiplication {
public static void main(String[] args) {
int n = 3;
Scanner input = new Scanner(System.in);
double[][]a = new double[n][n];
double[][]b = new double[n][n];
double[][]c = new double[n][n];
System.out.print("Enter matirx 1: ");
for (int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
a[i][j] = input.nextDouble();
}
}
System.out.print("Enter matirx 2: ");
for (int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
b[i][j] = input.nextDouble();
}
}
input.close();
c = mulitplyMatrix(a,b);
System.out.print("Multiplication of the matrices is: ");
for (int i = 0; i < n; i++) {
if(i!=0)
System.out.print(" ");
for (int j = 0; j < n; j++) {
System.out.printf("%.1f ", c[i][j]);
}
System.out.print(" ");
}
}
public static double[][] mulitplyMatrix(double[][] a,double[][] b)
{
int n = 3;
double[][] product = new double[3][3];
for (int i = 0; i <n; i++) {
for (int j = 0; j < n; j++) {
for (int k = 0; k <n; k++) {
product[i][j] += a[i][k] * b[k][j];
return product;
}
}
}
return product;
}
}