-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatrixMultiply.java
More file actions
79 lines (66 loc) · 1.79 KB
/
MatrixMultiply.java
File metadata and controls
79 lines (66 loc) · 1.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import java.util.Scanner;
public class MatrixMultiply
{
// Multiply 2 square matrices and returns the product of the 2 user entered matrices
public static void main(String[] args)
{
Scanner input=new Scanner(System.in);
System.out.print("Enter dimension of square matrix: ");
int n=input.nextInt();
int[][] m1=new int[n][n];
System.out.println("Enter values for m1:");
readMatrix(m1, input);
int[][] m2=new int[n][n];
System.out.println("Enter values for m2:");
readMatrix(m2, input);
int[][] m3= MatrixMulty (m1,m2);
System.out.println("Product of m1 and m2:");
printMatrix(m3);
input.close();
}
// matrix m will be filled with values from user input separated by space
public static void readMatrix(int[][] m1,Scanner input)
{
for(int i=0;i<m1.length;i++)
{
System.out.printf("\t\tEnter integers for row %d separated by white "
+ "space: ",i);
for(int j=0;j<m1.length;j++)
{
m1[i][j]=input.nextInt();
}
System.out.println();
}
}
//Returns A matrix that is the Product of m1 and m2.
public static int[][] MatrixMulty(int[][] m1,int[][] m2)
{
int[][] m3=new int[m1.length][m1.length];
for(int i=0;i<m1.length;i++)
{
for(int j=0;j<m1.length;j++)
{
int result=0;
int sum=0;
for(int k=0;k<m1.length;k++)
{
result=m1[i][k]*m2[k][j];
m3[i][j]+=result;
}
}
}
return m3;
}
//Prints the product matrix or m3
public static void printMatrix(int[][] m3)
{
for(int i=0;i<m3.length;i++)
{
for(int j=0;j<m3.length;j++)
{
System.out.print(m3[i][j]+" ");
}
System.out.println();
}
}
}