public static void main(String[] args) {
int [][] A = {{1,2,3}, {4,5,6}};
int[][] B = {{1,2,3}, {4,5,6}};
int Y[][] = new int[2][2];
for(int i = 0; i < Y.length; i++);{
for(int j = 0; j < Y[i].length; j++);{
for(int k = 0; k < 2; k++)
sum = sum + A[2][k] * B[k][2];
}
Where is the variable sum declared? Also Y[2][2] might throw an index out of bounds error. Remember that array indices start at 0, and Y is a 2d array with 2 rows and 2 columns. The largest index you can call is Y[1][1].
Hope this helps
package trial;
public class Tashi {
public static void main(String[] args) {
int [][] A = {{1,2,3}, {4,5,6}};
int[][] B = {{1,2,3}, {4,5,6}};
int sum = 0;
int Y[][] = new int[1][1];
for(int i = 0; i < Y.length; i++);{
for(int j = 0; j < Y.length; j++);{
for(int k = 0; k < 6; k++){
sum = sum + A[1][k] * B[k][1];
}
}
Y[1][1] = sum;
sum=0;
{
{
System.out.print(Y[1][0] + " ");
System.out.println(Y[1][1]);
}
}
}
}
}
i tried out this one again and it did not work the error was;
(Exception in thread βmainβ java.lang.ArrayIndexOutOfBoundsException: 2
at trial.Tashi.main(Tashi.java:12))
The error is coming from k. The arrays in A and B have lengths of 3, and k can go up to 5 (based on βk<6β in the for loop).
Also you decreased the size of Y. The largest value you can call now is Y[0][0]. Remember the arrays start at index 0, so if you want to have values at Y[1][1], you need to have an array of size [2][2].