0
write a program in Java for multithreaded matrix multiplication
2 Respostas
+ 1
I'm not sure but try this
import java.util.*;
public class le1{
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int N = scan.nextInt();
int M = scan.nextInt();
int[][] array = new int[N][M]; //1 matrix
Random r = new Random();
for (int i = 0 ;i<N ;i++ ) {
for (int j = 0;j<M ;j++ ) {
array[i][j]=scan.nextInt();
}
}
System.out.println();
int N1 = scan.nextInt();
int M1 = scan.nextInt();
int[][] array2 = new int[N1][M1]; //2 matrix
for (int i = 0 ;i<N1 ;i++ ) {
for (int j = 0;j<M1 ;j++ ) {
array2[i][j]=scan.nextInt();
}
}
System.out.println();
if (M==N1) {
int[][] result_array = new int[N][M1];
for (int i = 0;i<N ;i++ ) { //Multiplication
for (int j =0;j<M1 ;j++ ) {
for (int k = 0;k<N ;k++ ) {
result_array[i][j]+=array[i][k]*array2[k][j];
}
}
}
for (int i = 0;i<N ;i++ ) { //Printing
for (int j = 0;j<M1 ;j++ ) {
System.out.print(result_array[i][j]+" ");
}
System.out.println();
}
}else{
System.out.println("Error");
}
}
}
0
how to write java program to implement the matrixADT using a class