in Java Write a Java program that computes the dot product o
in Java, Write a Java program that computes the dot product of two matrices and stores the values in a third matrix. Print the result in tabular form. For example, to print matrix A, the print statement inside the nested for loops is: System.out.printf(\"%3d\",A[i][j]);
public class Lab5d
{
public static void main(String[] args)
{
// create the array A exactly as shown
int[][] A = { {10,55,4,89,39} , {45,9,49,98,23} , {4,8,90,23,9} , {8,32,80,2,31} };
// create the array B exactly as shown
int[][] B = { {40,1,16} , {90,3,7} , {9,2,22} , {44,35,60} , {18,67,21} };
// prepare the product array C, currently empty (filled with 0\'s)
int[][] C = new int[4][3];
int m = ____________; // number of rows in Matrix A
int n = ____________; // number of columns in Matrix A, which must equal number of rows in Matrix B
int p = ____________; // number of columns in Matrix B
// compute the dot product of A and B, store the results in C
for (int i=____; i < ____; i++)
{
for (int j=____; j < ____; j++)
{
C[i][j] = 0;
for (int k=____; k < ____; k++)
// sum the values for C
C[i][j] += A[____][____]*B[____][____];
}
}
// print A
System.out.println(\"Matrix A\ \");
for (int i=____; i <____; ____) {
for (int j=____; j < ____; ____)
System.out.printf(\"%5d\",A[____][____]);
System.out.println();
}
System.out.println();
// print B
System.out.println(\"Matrix B\ \");
for (int i=____; i< ____; ____) {
for (int j=____; j <____; ____)
System.out.printf(\"%5d\",B[____][____]);
System.out.println();
}
System.out.println();
// print C
System.out.println(\"Matrix C\ \");
for (int i=____; i< ____; ____) {
for (int j=____; j < ____; ____)
System.out.printf(\"%5d\",C[____][____j]);
System.out.println();
}
} // end main
} // end class
Solution
Hi, Please find my implementation.
Please let me know in case of any issue.
public class Lab5d
{
public static void main(String[] args)
{
// create the array A exactly as shown
int[][] A = { {10,55,4,89,39} , {45,9,49,98,23} , {4,8,90,23,9} , {8,32,80,2,31} };
// create the array B exactly as shown
int[][] B = { {40,1,16} , {90,3,7} , {9,2,22} , {44,35,60} , {18,67,21} };
// prepare the product array C, currently empty (filled with 0\'s)
int[][] C = new int[4][3];
int m = 4; // number of rows in Matrix A
int n = 5; // number of columns in Matrix A, which must equal number of rows in Matrix B
int p = 3; // number of columns in Matrix B
// compute the dot product of A and B, store the results in C
for (int i=0; i < m; i++)
{
for (int j=0; j < p; j++)
{
C[i][j] = 0;
for (int k=0; k < n; k++)
// sum the values for C
C[i][j] += A[i][k]*B[k][j];
}
}
// print A
System.out.println(\"Matrix A\ \");
for (int i=0; i <m; i++) {
for (int j=0; j < n; j++)
System.out.printf(\"%5d\",A[i][j]);
System.out.println();
}
System.out.println();
// print B
System.out.println(\"Matrix B\ \");
for (int i=0; i< n; i++) {
for (int j=0; j < p; j++)
System.out.printf(\"%5d\",B[i][j]);
System.out.println();
}
System.out.println();
// print C
System.out.println(\"Matrix C\ \");
for (int i=0; i< m; i++) {
for (int j=0; j < p; j++)
System.out.printf(\"%5d\",C[i][j]);
System.out.println();
}
} // end main
} // end class
/*
Sample Output:
Matrix A
10 55 4 89 39
45 9 49 98 23
4 8 90 23 9
8 32 80 2 31
Matrix B
40 1 16
90 3 7
9 2 22
44 35 60
18 67 21
Matrix C
10004 5911 6792
7777 5141 8224
2864 1616 3669
4566 2411 2883
*/



