# Yet another array problem :P

Brent Lancelot
Greenhorn
Posts: 8
well heres some more code with an array and im trying to multiply the matrixes of two arrays, i think its when i try to assign values to the array d and pass it back. it says the array is out of bounds but ive made sure to check everything and it seems just fine all the array have 5 elements and im not trying to access nething over that, it also comes up excepttion in thread main
yay i love programming

package Chapter5;
//Ex 5.23
public class MultiplyMatrix {
public static void main(String[] args) {
int[][] a = {{3,4,7,3,2},{4,6,2,9,1},{7,8,1,2,6},{1,3,2,9,7},
{6,5,8,5,1}};
int[][] b = {{7,2,9,6,1},{4,8,1,3,9},{1,5,9,2,7},{8,4,1,6,2},
{4,4,5,9,8}};
int[][] c = new int[4][4];
int x=0, y=0;

c = mMatrix(a,b);
for(x=0; x<5; x++)
{
System.out.println();
for(y=0; y<5; y++)
{
System.out.print(c[x][y] + " ");
}
}
}

public static int[][] mMatrix(int[][] a, int[][] b)
{
int[][] d = new int[4][4];
int i=0, j=0, z=0;

for(i=0; i <5; i++)
{
for(j=0;j<5;j++)
{
z = (a[i][0] * b[0][j]) + (a[i][1] * b[1][i]) + (a[i][2] * b[2][i]) +
(a[i][3] * b[3][i]) + (a[i][4] * b[4][i]);
d[i][j] = z;
}
}
return d;
}
}

Brent Lancelot
Greenhorn
Posts: 8
oh no nevermind its the array my bad

Henry Wong
author
Marshal
Posts: 21719
85

The 4 specifies the number of elements. This is an array of 4 arrays with 4 ints each.

Henry

Brent Lancelot
Greenhorn
Posts: 8
yeah i keep thinking when i declare an array i need to count 0 as an element