This is version 1 of matrix multiplication program. It doesn't
work. Why?
public class MatrixMultiplication {
public static void main (String [] args){
int [] [] a = {{4, 5, 6}, {1, 0, 2}};
int [] [] b = {{7,1}, {0,1}, {1,2}};
int [] [] c = new int[b.length] [a[0].length];
if(b.length != a[0].length){
System.out.println("These matrices cannot be multiplied");
return;
}
for (int i = 0; i < a.length; i++){
int sum = 0;
int j = 0;
for (j = 0; j < a[0].length; j++){
sum += a[i][j] * b[j][i];
}
c[i][j] = sum;
}
}
}
This is an example from CSci 1211 course.