Question

In: Computer Science

How to write a method that performs matrix multiplication with three rectangle arrays along with their...

How to write a method that performs matrix multiplication with three rectangle arrays along with their dimensions as parameters using Java programming?

Solutions

Expert Solution

import java.util.*;
public class Main
{
static int[][] matrixMultiplication(int [][]a,int [][]b,int [][]c,int r1,int c1,int r2,int c2,int r3,int c3)
{
int [][]product1=new int[r1][c2];// product1 matrix with dimensions r1 X c2
int i,j,k;
if(r2!=c1)//if first matrix column and second matrix row doesn't match
{
System.out.println("First matrix and second matrix cannot be multiplied");
return null;
}
for (i=0;i<r1;i++) {
for (j=0;j<c2;j++) {
for (k=0;k<c1;k++) {
product1[i][j]+=a[i][k]*b[k][j];
}
}
}
int [][]product2=new int[r1][c3];//final matrix with dimensions r1 X c3
if(c2!=r3)
{
System.out.println("product1 matrix and third matrix cannot be multiplied");
return null;
}
for (i=0;i<r1;i++) {
for (j=0;j<c3;j++) {
for (k=0;k<c2;k++) {
product2[i][j]+=product1[i][k]*c[k][j];
}
}
}
return product2;

}
   public static void main(String[] args) {
       Scanner s=new Scanner(System.in);
       int r1,r2,r3,c1,c2,c3,i,j;
       r1=s.nextInt();
       c1=s.nextInt();
       r2=s.nextInt();
       c2=s.nextInt();
       r3=s.nextInt();
       c3=s.nextInt();
       int [][]a=new int[r1][c1];
       int [][]b=new int[r2][c2];
       int [][]c=new int[r3][c3];
       int [][]res=new int[r1][c3];
       System.out.println("enter numbers into matrix1");
       for(i=0;i<r1;i++)
       {
       for(j=0;j<c1;j++)
       {
       a[i][j]=s.nextInt();
       }
       }
       System.out.println("enter numbers into matrix2");
       for(i=0;i<r2;i++)
       {
       for(j=0;j<c2;j++)
       {
       b[i][j]=s.nextInt();
       }
       }
       System.out.println("enter numbers into matrix3");
       for(i=0;i<r3;i++)
       {
       for(j=0;j<c3;j++)
       {
       c[i][j]=s.nextInt();
       }
       }
       res=matrixMultiplication(a,b,c,r1,c1,r2,c2,r3,c3);
       for(i=0;i<r1;i++)
       {
       for(j=0;j<c3;j++)
       {
       System.out.print(res[i][j]+" ");
       }
       System.out.println();
       }
   }
}


Related Solutions

build a program which performs matrix multiplication on square matrices. use UNIX "time" to capture the...
build a program which performs matrix multiplication on square matrices. use UNIX "time" to capture the time it takes to run the program with different data sizes. Languages: Python Task: Create matrix multiplication Input: Size of square matrix.   Size should be    250, 500, 1000, 1500, 2000 Internals: Explicitly or implicitly allocate sufficient memory to hold three NxN floating point Matrices, using a random number generator -- populate two of the Matrices, Multiply the two matrices, putting the result into the...
Need to write a code using c# Strassen’s Algorithm for matrix multiplication.
Need to write a code using c# Strassen’s Algorithm for matrix multiplication.
Use Java to: 1. Write a method with a Rectangle and a Point as parameters. Without...
Use Java to: 1. Write a method with a Rectangle and a Point as parameters. Without using methods from the Rectangle class, return true if the point is inside the rectangle and false otherwise. 2. Write a second method with the same functionality as Exercise 1. However, use a method from the Rectangle class this time. 3. Normally, the == operator cannot be used to compare two strings for equality. There are 2 main exceptions we talked about. The first...
Write a function such that given a number N, display the N*N multiplication matrix from 1...
Write a function such that given a number N, display the N*N multiplication matrix from 1 to N. Then, write a C++ program such that, it prints the multiplication matrices of numbers 1,4,7, and 10 using a loop concept. Check the sample output below, to see how the program should work. Make sure to have your output exactly the same as the below output.
Exercise 1. Rectangle, Circle and Square Write three Python classes named Rectangle constructed by a length...
Exercise 1. Rectangle, Circle and Square Write three Python classes named Rectangle constructed by a length and width, a Circle constructed by a radius and a Square constructed by a side length. Both classes should have the methods that compute: - The area - The diagonal - The perimeter Use as much abstraction as you can. At the end of the file, use those classes to calculate the perimeter of a circle with radius the half of the diagonal of...
First assignment for C++. How do I setup this dynamic multiplication table using 2D arrays and...
First assignment for C++. How do I setup this dynamic multiplication table using 2D arrays and double pointers? The assignment asks to Write a program that displays a 2D multiplication table based on row and column value specified by the user. Perform a data validation to ensure the number of rows and columns given by the user exist on the interval [1, 10]. If possible, protect against inputs that contain symbols that would normally crash the program (i.e. letter, symbols,...
Write a method weave that takes two arrays of ints, a and b, and that returns...
Write a method weave that takes two arrays of ints, a and b, and that returns an array that contains the elements of a and b in the order a[0], b[0], a[1], b[1], etc. If one of the arrays a or b is longer than the other, just add the extra elements at the end of the array. In your solution, you can use only 3 arrays, namely the two arrays a, and b passed to the method and the...
Write a short program that performs the following: - Method city returns the string "Seattle": -...
Write a short program that performs the following: - Method city returns the string "Seattle": - Method named state returns the string "Washington ": - Method named report calls the city methodand calls the state method. and prints their return values, so that the output will appear as : "Seattle Washington "
write the “largerComponents” method that takes in two integer arrays and returns true or false if...
write the “largerComponents” method that takes in two integer arrays and returns true or false if the first array’s components are strictly greater than the second array’s components. The arrays must be the same size or else return false. Clarification Note: Components meaning each value at a specific index. For instance, if we had the arrays {5,2,7} and {1,3,1} then this method would return false as the value “2” is not greater than “3”. here is a provided code //Do...
C++ DO not use arrays to write this program. Write a program that repeatedly generates three...
C++ DO not use arrays to write this program. Write a program that repeatedly generates three random integers in the range [1, 100] and continues as follows: If the right-most digit of all the three integers is equal, the program displays them in ascending order on the screen and continues. If the generated integers have different right-most digits, they are not displayed and the program continues. The program terminates once the right-most digits of all the three random numbers are...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT