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

Write a program that can create two matrices and then it multiplies them! MAKE S

ID: 3569324 • Letter: W

Question

Write a program that can create two matrices and then it multiplies them!

MAKE SURE TO PLEASE INCLUDE COMMENTS FOR ME TO ADD POINTS !!!!!!!!!!!!!!! :))))))

The user first inputs the size length and width

Next the user populates that matrix with data

The user then inputs the size length and width of the next matrix

Once again they populate it

Next the program must check to see if the dimensions match

A n-by-m matrix can only be multiplied by a m-by-l matrix where n, m, and l correspond to the sizes of the matrices.

If they do match correctly then create a new matrix and put the newly multiplied info inside of it.

Multiply the individual numbers of the first matrix

Explanation / Answer

Program Code:

//MatrixMultiplication.java

//import util package to import the Scanner class to read

//data from the user

import java.util.*;

public class MatrixMultiplication

{

     //main method

     public static void main(String args[])

     {

          //declare the int variables to

          //store the size of the two matrix

          int n, m, r, s;

          //declare three two dimensional arrays

          int a[][], b[][], c[][];

          //create an object to the Scanner class

          Scanner input=new Scanner(System.in);

          //prompt the user to enter the size of the arrays

          System.out.println("Welcome to the matrix multiplier program.");

          System.out.println("Please enter the length of the first matrix: ");

          n=input.nextInt();

          System.out.println("Please enter the height of the first matrix ");

          m=input.nextInt();

          System.out.println("Please enter the length of the second matrix: ");

          r=input.nextInt();

          System.out.println("Please enter the height of the second matrix ");

          s=input.nextInt();

          //condition to check the whether the height of the matrix one is

          //not equal to length of the second matrix.

          if(m!=r)

          {

              //if they are not equal prompt the user to re-enter the size of

              //second matrix

              System.out.println("Please enter the length of the second matrix: ");

              r=input.nextInt();

              System.out.println("Please enter the height of the second matrix ");

              s=input.nextInt();

          }

          //initialise the three matrices with the dimensions

          a=new int[n][m];

          b=new int[r][s];

          c=new int[n][s];

          //condition to check the whether the height of the matrix one is

          //equal to length of the second matrix.

          if(m==r)

          {  

              //enter the elements into the first matrix

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

              {

                   for(int j=0;j<m;j++)

                   {

                        System.out.println("Please enter a value for matrix 1 space "+(i+1)+","+(j+1));

                        a[i][j]=input.nextInt();

                   }

              }

              //enter the elements into the second matrix

              for(int i=0;i<r;i++)

              {

                   for(int j=0;j<s;j++)

                   {

                        System.out.println("Please enter a value for matrix 2 space "+(i+1)+","+(j+1));

                        b[i][j]=input.nextInt();

                   }

              }

            

              //logic to compute the product of the two matrices.

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

              {

                   for (int j = 0; j < m; j++)

                   {

                        for (int k = 0; k < s; k++)

                        {

                             c[i][j] = c[i][j] + a[i][k] * b[k][j];

                        }

                   }

              }

          }

          //display the elements present in the first matrix by calling display method

          System.out.println("Elements in Matrix 1 is: ");

          display(a, n, m);

        

          //display the elements present in the second matrix by calling display method

          System.out.println("Elements in Matrix 2 is: ");

          display(b, r, s);

        

          //display the elements present in the resultant matrix by calling display method

          System.out.println("The product of the matrix 1 and matrix 2 is the resultant matrix.");

          System.out.println("The resultant matrix is : ");

          display(c, n,s);

          System.out.println("DONE!");

     }

     //static method to display the elements of the matrix.

     public static void display(int [][]x, int l, int h)

     {

          for(int i=0;i<l;i++)

          {

              for(int j=0;j<h;j++)

              {

                   System.out.print(x[i][j]+"   ");

              }

              System.out.println();

          }

     }

}

-------------------------------------------------------------------------------------------------------------------------------

Sample output:

Welcome to the matrix multiplier program.

Please enter the length of the first matrix:

1

Please enter the height of the first matrix

3

Please enter the length of the second matrix:

3

Please enter the height of the second matrix

3

Please enter a value for matrix 1 space 1,1

3

Please enter a value for matrix 1 space 1,2

1

Please enter a value for matrix 1 space 1,3

4

Please enter a value for matrix 2 space 1,1

1

Please enter a value for matrix 2 space 1,2

3

Please enter a value for matrix 2 space 1,3

8

Please enter a value for matrix 2 space 2,1

2

Please enter a value for matrix 2 space 2,2

4

Please enter a value for matrix 2 space 2,3

6

Please enter a value for matrix 2 space 3,1

8

Please enter a value for matrix 2 space 3,2

0

Please enter a value for matrix 2 space 3,3

9

Elements in Matrix 1 is:

3   1   4

Elements in Matrix 2 is:

1   3   8

2   4   6

8   0   9

The product of the matrix 1 and matrix 2 is the resultant matrix.

The resultant matrix is:

37   13   66

DONE!

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