Question

Write a method to multiply two matrices. The header of the method is: public static double[][]...

Write a method to multiply two matrices. The header of the method is: public static double[][] multiplyMatrix(double[][] a, double[][] b) To multiply matrix a by matrix b, the number of columns in a must be the same as the number of rows in b, and the two matrices must have elements of the same or compatible types. Let c be the result of the multiplication. Assume the column size of matrix a is n. Each element is For example, for two matrices a and b, c is where Write a test program that prompts the user to enter two matrices and displays their product. Write in java language, with comments and display a flowchart.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Hey,

Note: Brother in case of any queries, just comment in box I would be very happy to assist all your queries

import java.util.Scanner;
public class MultiplyMatrixTest {

public static double[][] multiplyMatrix(double[][] a, double[][] b) {
double[][] result = new double[a.length][b[0].length];
for(int i = 0; i < result.length; ++i) {
for(int j = 0; j < result[i].length; ++j) {
double total = 0;
for(int k = 0; k < a[i].length; ++k) {
total += (a[i][k] * b[k][j]);
}
result[i][j] = total;
}
}
return result;
}

public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
double[][] matrix1;
double[][] matrix2;
int m1,n1,m2,n2;
System.out.println("Enter number of rows for matrix 1: ");
m1=sc.nextInt();
System.out.println("Enter number of columns for matrix 1: ");
n1=sc.nextInt();
matrix1=new double[m1][n1];
for(int i = 0; i < m1; ++i) {
for(int j = 0; j < n1; ++j) {
System.out.println("Enter element at index ("+i+", "+j+")");
matrix1[i][j]=sc.nextDouble();
  
}
}

System.out.println("Enter number of rows for matrix 2: ");
m2=sc.nextInt();
System.out.println("Enter number of columns for matrix 2: ");
n2=sc.nextInt();
  
matrix2=new double[m2][n2];
for(int i = 0; i < m2; ++i) {
for(int j = 0; j < n2; ++j) {
System.out.println("Enter element at index ("+i+", "+j+")");
matrix2[i][j]=sc.nextDouble();
  
}
}
if(n1!=m2)
{
System.out.println("Error!!");
return;
}
double[][] result = multiplyMatrix(matrix1, matrix2);

for(int i = 0; i < result.length; ++i) {
for(int j = 0; j < result[i].length; ++j) {
System.out.print(result[i][j] + " ");
}
System.out.println();
}
}

}

Kindly revert for any queries

Thanks.

Add a comment
Know the answer?
Add Answer to:
Write a method to multiply two matrices. The header of the method is: public static double[][]...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Exercise 3: Write a method to add two matrices. The header of the method is: I...

    Exercise 3: Write a method to add two matrices. The header of the method is: I public static double[][] addMatrix(double [ ][] a, double[][] b) In order to be added, the two matrices must have the same dimensions and the same or compatible types of elements. Let e be the resulting matrix. Each element c is c -a, +b For example, for two 3x3 matrices a and b, c is Write a test program that (a) prompts the user to...

  • Write a MATLAB program that loads two matrices A and B from the files hw41matA.txt andhw41matB.txt...

    Write a MATLAB program that loads two matrices A and B from the files hw41matA.txt andhw41matB.txt and multiplies them together to obtain matrix C such that the element C(i,j) ofmatrix C is given by: C(i, j) =  ∑A(i, k) ∗ B(k, j) where n = number of columns in A = number of rows in B. Display the matrix C in defaultMATLAB format.
You cannot use the MATLAB matrix multiply or other inbuilt MATLAB functions forarithmetic operations. You must implement it....

  • Write a VBA Sub Program to perform matrices multiplication of matrix A and B using 2D...

    Write a VBA Sub Program to perform matrices multiplication of matrix A and B using 2D arrays. a. Get the number of rows and columns of both matrix A and B from the user, and check if multiplication is possible with matrix A and B. b. If matrices multiplication is possible, input the matrix A and B using arrays and multiply matrix A and B.

  • Problem 1 Write your code in the file MatrixOps.java. . Consider the following definitions from matrix...

    Problem 1 Write your code in the file MatrixOps.java. . Consider the following definitions from matrix algebra: A vector is a one-dimensional set of numbers, such as [42 9 20]. The dot product of two equal-length vectors A and B is computed by multiplying the first entry of A by the first entry of B, the second entry of A by the second entry of B, etc., and then summing these products. For example, the dot product of [42 9...

  • C++ must use header files and implementation files as separate files. I’ll need a header file,...

    C++ must use header files and implementation files as separate files. I’ll need a header file, implementation file and the main program file at a minimum. Compress these files into one compressed file. Make sure you have adequate documentation. We like to manipulate some matrix operations. Design and implement a class named matrixMagic that can store a matrix of any size. 1. Overload the addition, subtraction, and multiplication operations. 2. Overload the extraction (>>) and insertion (<<) operators to read...

  • Write your own matlab code to perform matrix multiplication. Recall that to multiply two matrices, the...

    Write your own matlab code to perform matrix multiplication. Recall that to multiply two matrices, the inner dimensions must be the same. Write this as a function with input arguments the matrices to be multiplied and output argument the resultant matrix, use "assert" statement(s) to check that the input arguments are valid and print an error message if they are not. Test with A=[1,2,3;4,5,6] and B=[7,8;9,10;11,12] showing results for: (a) A*B, (b) B*A, and (c) A*A.

  • Write a c++ program: Many mathematical problems require the addition, subtraction, and multiplication of two matrices. Write an ADT Matrix. You may use the following class definition: const int MAX_RO...

    Write a c++ program: Many mathematical problems require the addition, subtraction, and multiplication of two matrices. Write an ADT Matrix. You may use the following class definition: const int MAX_ROWS = 10; const int MAX_COLS = 10; class MatrixType { public: MatrixType(); void MakeEmpty(); void SetSize(int rowsSize, int colSize); void StoreItem(int item, int row, int col); void Add(MatrixType otherOperand, MatrixType& result); void Sub(MatrixType otherOperand, MatrixType& result); void Mult(MatrixType otherOperand, MatrixType& result); void Print(ofstream& outfile); bool AddSubCompatible(MatrixType otherOperand); bool MultCompatible(MatrixType otherOperand);...

  • Submit Chapter7.java with four (4) public static methods as follows: A. Write a method called mostCommon...

    Submit Chapter7.java with four (4) public static methods as follows: A. Write a method called mostCommon that accepts an array of integers as its only parameter, and returns the int that occurs most frequently. Break ties by returning the lower value For example, {1,2,2,3,4,4} would return 2 as the most common int. B. Write mostCommon (same as above) that accepts an array of doubles, and returns the double that occurs most frequently. Consider any double values that are within 0.1%...

  • Java Here is the template public class ShiftNumbers { public static void main(String[] args) { // TODO:...

    Java Here is the template public class ShiftNumbers { public static void main(String[] args) { // TODO: Declare matrix shell size // TODO: Create first row // TODO: Generate remaining rows // TODO: Display matrix } /** * firstRow * * This will generate the first row of the matrix, given the size n. The * elements of this row will be the values from 1 to n * * @param size int Desired size of the array * @return...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT