Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

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;

       }

      

       }

Explanation / Answer

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 matrix 1: "); for (int i = 0; i
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote