Im having some trouble getting the following code to run Let
I\'m having some trouble getting the following code to run. Let me know if it would be benefcial to also post the changes I\'ve made.
public class
{
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[____][____];
}
}
System.out.println(\"Matrix A\ \");
for (int i=____; i <____; ____) {
for (int j=____; j < ____; ____)
System.out.printf(\"%5d\",A[____][____]);
System.out.println();
}
System.out.println();
System.out.println(\"Matrix B\ \");
for (int i=____; i< ____; ____) {
for (int j=____; j <____; ____)
System.out.printf(\"%5d\",B[____][____]);
System.out.println();
}
System.out.println();
System.out.println(\"Matrix C\ \");
for (int i=____; i< ____; ____) {
for (int j=____; j < ____; ____)
System.out.printf(\"%5d\",C[____][____j]);
System.out.println();
}
}
}
Solution
public class MatrixMul {
public static void main(String a[]){
int[][] A = { {10,55,4,89,39} , {45,9,49,98,23} , {4,8,90,23,9} , {8,32,80,2,31} };
int[][] B = { {40,1,16} , {90,3,7} , {9,2,22} , {44,35,60} , {18,67,21} };
int[][] C = new int[4][3];
int m=4; //No.of rows of Matrix A
int n=5; //No.of Columns of Matrix A
int p=3; //No.of Columns of Matrix B
int q=5; //No.of rows of Matrix B
System.out.println(\"Matrix A:\");
for (int i = 0; i < m; i++)
{
for (int j = 0; j < n; j++)
{
System.out.print(A[i][j]+\" \");
}
System.out.println(\" \");
}
System.out.println(\"Matrix B:\");
for (int i = 0; i <q; i++)
{
for (int j = 0; j < p; j++)
{
System.out.print(B[i][j]+\" \");
}
System.out.println(\" \");
}
for(int i=0;i<m;i++){
for(int j=0;j<p;j++){
for(int k=0;k<q;k++){
C[i][j] = C[i][j] + A[i][k] * B[k][j];
}
}
}
System.out.println(\"The product is:\");
for (int i = 0; i < m; i++)
{
for (int j = 0; j < p; j++)
{
System.out.print(C[i][j] + \" \");
}
System.out.println();
}
}
}

