Hello,
I'm trying to write a program for matrix multiplication
I keep getting an error....
Please help

class Matrix {
private int numRows, numColumns;
private double x[][];

public Matrix(int m, int n){
	numRows = m;
	numColumns = n;
	x = new double[numRows][numColumns];
	for(int i = 0; i < numRows; i++)
		for(int j = 0; j< numColumns; j++)
			x[i][j] = 0;
	
}
public void setElement(int i, int j, double y){
	x[i-1][j-1]=y;
}
public static Matrix product(Matrix[] A){
int nRows = A[0].numRows, nColumns = A[0].numColumns;// Why must you add 0 in between brackets??
Matrix B = new Matrix(nRows,nColumns );
for (int i = 0; i < A.length;i++)
		for (int k = 0; k < nColumns;k++)
			for (int j = 0; j < nRows; j++)
			B.x[j][12-k] *= A[i].x[12-k][j];// why does A have A[i] but B doesn't?}
return B;
}


public void print(){
	System.out.println(" number of rows: " + numRows + " / number of columns: " + numColumns);
	for (int i = 0; i <numRows; i ++){
		for (int j = 0; j < numColumns; j++){
			System.out.format("%5.0f",x[i][j]);}
	System.out.println();
}

}
}





////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////
public class TestMatrix {
    static int generateNumber(int i, int j, int k){
        int n = i+j+k;
        if(n*n % 3 == 0) return n%5;
        else return 0;
    }

    public static void main(String[] args){
        final int numMatrices = 7;
        Matrix[] matrix = new Matrix[numMatrices];
        
        // create & initialize matrices
        for(int h=0; h<numMatrices; h++){
            int numRows = 13-h, numColumns = 12-h;
            
            matrix[h] = new Matrix(numRows, numColumns);
                
            for(int i=0; i<numRows; i++)
              for(int j=0; j<numColumns; j++)
                matrix[h].setElement(i+1, j+1, generateNumber(i,j,h));
        }

        Matrix prod = Matrix.product(matrix);
        prod.print();
    }
}

Recommended Answers

All 4 Replies

I keep getting an error.

please copy the full text of the error message and paste it here.

Please use code tags when posting code. Use the icon above to right of input area.

class Matrix {
private int numRows, numColumns;
private double x[][];

public Matrix(int m, int n){
	numRows = m;
	numColumns = n;
	x = new double[numRows][numColumns];
	for(int i = 0; i < numRows; i++)
		for(int j = 0; j< numColumns; j++)
			x[i][j] = 0;
	
}
public void setElement(int i, int j, double y){
	x[i][j]=y;
}

public static Matrix product(Matrix[] A){
int nRows = A[0].numRows, nColumns = A[0].numColumns;
Matrix B = new Matrix(nRows,nColumns );
for (int i = 0; i < A.length; i++)
		for (int k = 0; k < nColumns;k++)
			for (int j = 0; j < nRows; j++)
			B.x[j][k] *= A[i].x[k][j];
return B;
}


public void print(){
	System.out.println(" number of rows: " + numRows + " / number of columns: " + numColumns);
	for (int i = 0; i <numRows; i ++){
		for (int j = 0; j < numColumns; j++){
			System.out.format("%5.0f",x[i][j]);}
	System.out.println();
}

}
}





////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////
public class TestMatrix {
    static int generateNumber(int i, int j, int k){
        int n = i+j+k;
        if(n*n % 3 == 0) return n%5;
        else return 0;
    }

    public static void main(String[] args){
        final int numMatrices = 7;
        Matrix[] matrix = new Matrix[numMatrices];
        
        // create & initialize matrices
        for(int h=0; h<numMatrices; h++){
            int numRows = 13-h, numColumns = 12-h;
            
            matrix[h] = new Matrix(numRows, numColumns);
                
            for(int i=0; i<numRows; i++)
              for(int j=0; j<numColumns; j++)
                matrix[h].setElement(i, j, generateNumber(i,j,h));
        }

        Matrix prod = Matrix.product(matrix);
        prod.print();
    }
}

Error Message

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 12
at Matrix.product(TestMatrix.java:25)
at TestMatrix.main(TestMatrix.java:69)

Look at line 25 in your program.
What is the size of the array referenced on that line?
Does it have an element at index 12?

If you don't know the sizes, add some print outs to your code to display the sizes of all the arrays you are referencing on line 25 and also print out the values of the ranges of indexes you are using to see why/where your code is using an invalid index.

Here

for(int h=0; h<numMatrices; h++) {
    int numRows = 13-h, numColumns = 12-h;
    matrix[h] = new Matrix(numRows, numColumns);

you are varying the "sizes" of the matrices using the index into the Matrix array.

Here

int nRows = A[0].numRows, nColumns = A[0].numColumns;
for (int i = 0; i < A.length; i++)
    for (int k = 0; k < nColumns;k++)
        for (int j = 0; j < nRows; j++)

you are getting the dimensions of the first (and therefore largest) matrix and using those dimensions during your processing for all the matrices, therefore incurring the ArrayIndexOutOfBoundsException on the last indexing attempt on the first row of the second matrix since it will be one smaller in all dimensions than the first was.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.