You are to write a program name matrix.java that multiply and add 2 matrices. Re
ID: 2247721 • Letter: Y
Question
You are to write a program name matrix.java that multiply and add 2 matrices.
Requriements:
1. Your program should prompt the user for the dimensions of the two matrices, then check for compatibility (remember 2 matrices can be multiplied only if the column of the first is equal to the row of the second). You could prompt for ONE dimension and build two square matrices. Minimum dimension of matrix must be 25.
2. If the above is not met, prompt the user for new and compatible dimensions.
3. Now generate random integer numbers (ranging from 1 to 30) to fill both matrices.
4. Display these two matrices on the screen.
5. Multiply the two matrices and display the result on the screen.
6. Insert a clock to see how long it would take, in milliseconds, to multiply these two matrices and display the time (with a message to this effect).
7. Now add the two matrices and display the result on the screen.
8. Insert a clock to see how long it would take, in milliseconds, to add these two matrices and display the time (with a message to this effect).
9. Prompt the user asking if they want to repeat the program.
Explanation / Answer
Below is your code. Note that I cannot paste the output because it is too long. Let me know if you have any trouble undersanding or running it.
Matrix.java
import java.util.Random;
import java.util.Scanner;
public class Matrix {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
boolean done = false;
while (!done) {
int m1, n1 = 1, m2, n2;
int it = 0;
do {
if (it != 0)
System.out.println(
"Please enter the valid dimensions (columns in Mat1 must be equal to rows in Mat2) ");
it++;
System.out.print("Enter the number of rows of Matrix 1 : ");
m1 = Integer.parseInt(sc.next());
while (m1 < 25) {
System.out.println("Please enter number greater than 25 ");
System.out.print("Enter the number of rows of Matrix 1 : ");
m1 = Integer.parseInt(sc.next());
}
System.out.print("Enter the number of columns of Matrix 1 : ");
n1 = Integer.parseInt(sc.next());
while (n1 < 25) {
System.out.println("Please enter number greater than 25 ");
System.out.print("Enter the number of columns of Matrix 1 : ");
n1 = Integer.parseInt(sc.next());
}
System.out.print("Enter the number of rows of Matrix 2 : ");
m2 = Integer.parseInt(sc.next());
while (m2 < 25) {
System.out.println("Please enter number greater than 25 ");
System.out.print("Enter the number of rows of Matrix 2 : ");
m2 = Integer.parseInt(sc.next());
}
System.out.print("Enter the number of columns of Matrix 2 : ");
n2 = Integer.parseInt(sc.next());
while (n2 < 25) {
System.out.println("Please enter number greater than 25 ");
System.out.print("Enter the number of columns of Matrix 2 : ");
n2 = Integer.parseInt(sc.next());
}
} while (n1 != m2);
int[][] mat1 = new int[m1][n1];
int[][] mat2 = new int[m2][n2];
Random r = new Random();
for (int i = 0; i < m1; i++) {
for (int j = 0; j < n1; j++) {
mat1[i][j] = r.nextInt(30) + 1;
}
}
for (int i = 0; i < m2; i++) {
for (int j = 0; j < n2; j++) {
mat2[i][j] = r.nextInt(30) + 1;
}
}
System.out.println("Matrix 1: ");
for (int i = 0; i < m1; i++) {
for (int j = 0; j < n1; j++) {
System.out.print(mat1[i][j] + " ");
}
System.out.println();
}
System.out.println("Matrix 2: ");
for (int i = 0; i < m2; i++) {
for (int j = 0; j < n2; j++) {
System.out.print(mat2[i][j] + " ");
}
System.out.println();
}
int[][] prod = new int[mat1.length][mat2[0].length];
long t1 = System.currentTimeMillis();
// Mutliplying Two matrices
for (int i = 0; i < mat1.length; i++) {
for (int j = 0; j < mat2[0].length; j++) {
for (int k = 0; k < mat1[0].length; k++) {
prod[i][j] += mat1[i][k] * mat2[k][j];
}
}
}
long t2 = System.currentTimeMillis();
System.out.println("Product of Matrix : ");
for (int i = 0; i < m1; i++) {
for (int j = 0; j < n2; j++) {
System.out.print(prod[i][j] + " ");
}
System.out.println();
}
System.out.println("Time taken for matrix multiplication in millis : " + (t2 - t1));
if (m1 == m2 && n1 == n2) {
int[][] sum = new int[m1][n1];
t1 = System.currentTimeMillis();
for (int i = 0; i < m1; i++) {
for (int j = 0; j < n1; j++) {
sum[i][j] = mat1[i][j] + mat2[i][j];
}
}
t2 = System.currentTimeMillis();
System.out.println("Sum of Matrix : ");
for (int i = 0; i < m1; i++) {
for (int j = 0; j < n1; j++) {
System.out.print(sum[i][j] + " ");
}
System.out.println();
}
System.out.println("Time taken for matrix addition in millis : " + (t2 - t1));
System.out.println("Do you want to repeat the program : (Y/N) ");
String op = sc.next();
if(op.equalsIgnoreCase("Y")) {
done = false;
} else {
done = true;
}
} else {
System.out.println("For addition dimensions must be equal..Please enter dimensions accordingly..");
done = false;
}
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.