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

Difference of two matrices.java Write a program that accepts two 3 x 3 matrices

ID: 3853502 • Letter: D

Question

Difference of two matrices.java

Write a program that accepts two 3 x 3 matrices and invokes a method to subtract them together. The formula for subtracting two matrices is shown below:

You will need:

1.) A scanner object to read the values that fill the arrays

2.) Three 2-dimensional arrays:

-One to store the values in the first 3x3 matrix

-One to store the values in the second 3x3 matrix

-One to store the difference of the values from the first two matrices

3.) A method that subtracts the two matrices using the formula above. The method should accept the first and second matrices as parameters (i.e. a and b,) and return an array. (The array should be returned to the matrix in part c.)

4.) Comments where necessary.

A sample of the output is shown below:

Enter matrix1: 29 28 27 26 25 24 23 22 21

Enter matrix2: 1 2 3 4 5 6 7 8 9

The difference of the matrices is:

29.0 28.0 27.0

26.0 25.0 24.0

23.0 22.0 21.0

-

1.0 2.0 3.0

4.0 5.0 6.0

7.0 8.0 9.0

=

28.0 26.0 24.0

22.0 20.0 18.0

16.0 14.0 12.0

6 11 12 13 11 12 13 21 22 23-21 22 23 21-D 21 a 22-D 22 a 23-D 23 31 3233 131 32 32 33-3

Explanation / Answer


import java.util.*;

class MatrixDifference
{
   public static void main (String[] args)
   {
   //declare three 3 by 3 matrices for a,b and diff
       double[][] a = new double[3][3];
       double[][] b = new double[3][3];
       double[][] diff = new double[3][3];
      
       Scanner scan = new Scanner(System.in);
      
       int i,j;
      
       //input matrix a
      
       for(i=0;i<3;i++)
       {
       for(j=0;j<3;j++)
       {
       a[i][j] = scan.nextDouble();
       }
       }
      
      
       //input matrix b
       for(i=0;i<3;i++)
       {
       for(j=0;j<3;j++)
       {
       b[i][j] = scan.nextDouble();
       }
       }
      
      
      
       diff = difference(a,b); //call difference function
      
       System.out.println(" The difference of Matrix a and Matrix b : ");
       //display the diff matrix
       for(i=0;i<3;i++)
       {
       for(j=0;j<3;j++)
       {
       System.out.print(diff[i][j]+" ");
       }
       System.out.println();
       }
      
   }
  
  
   public static double[][] difference(double[][] a,double[][] b)
   {
   int i,j;
   double[][] diff = new double[3][3];
   //compute difference of matrix a and matrix b
   for(i=0;i<3;i++)
       {
       for(j=0;j<3;j++)
       {
       diff[i][j] = a[i][j] - b[i][j];
       }
       }
       return diff;
   }
}

Output:

29 28 27 26 25 24 23 22 21
1 2 3 4 5 6 7 8 9

The difference of Matrix a and Matrix b :
28.0   26.0   24.0  
22.0   20.0   18.0  
16.0   14.0   12.0  

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