# matrix multiply

• 09-13-2010, 06:21 AM
slixtrix
matrix multiply
im doing a project for my class, and this is my code that i am using..

i am currently having errors in this code which are:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 3
at arrayMatrix.multMatrix(arrayMatrix.java:67)
at arrayMatrix.main(arrayMatrix.java:32)

i am using Eclipse to type this code, so if anyone can help me fix these problems i would greatly appreciate it

Code:

```/* This program multiplies 2 matrices times each other.  * If the matrices are not compatible, it warns the user. */ public class arrayMatrix {                 //static List list;                 public static void main(String[] args)         {                 int j, k, l;                 int a[][], b[][], ab[][];                                 System.out.println("Hello TV land!");                 j = 1;                 k = 2;                 l = 3;                                 a = makeIdent(2, 3);                 a[0][0] = 1;a[0][1] = 2;a[0][2] = 4;                 a[1][0] = 2;a[1][1] = 6;a[1][2] = 0;                 showMatrix(2, 3, a, j);                                 b = makeIdent(3, 4);                 b[0][0] = 4;b[0][1] = 1;b[0][2] = 4;b[0][3] = 3;                 b[1][0] = 0;b[1][1] = -1;b[1][2] = 3;b[1][3] = 1;                 b[2][0] = 2;b[2][1] = 7;b[2][2] = 5;b[2][3] = 2;                 showMatrix(3, 4, b, k);                                 ab = multMatrix(2, 3, a, 3, 4, b);                 showMatrix(2, 4, ab, l);                                         }                         static int[][] makeIdent(int nRows, int nCols){                 int a[][] = new int[nRows][nCols];                                 return a;         }         static void showMatrix(int nRows, int nCols, int m[][], int x){                 System.out.println("Matrix " + x + ":");                 for(int i = 0; i < nRows; i++) {                         for(int j = 0; j < nCols; j++) {                               System.out.println("Row: " + i + " Column: " + j                                                           + " = "+ m[i][j]);                     }                 }         }                                 static int[][] multMatrix(int aRows, int aCols, int a[][],                                   int bRows, int bCols, int b[][]){                 int mult[][] = new int[aRows][bCols];                                 if(aCols != bRows){                         System.out.println("These matrices are not compatible");                 }else{                         for(int i = 0; i < aRows; i++) {                               for(int j = 0; j < bCols-1; j++) {                                 for(int k = 0; k < bCols; k++){                                                                     mult[i][j] += a[i][k]*b[k][j];                                 }                               }                              }                                         }                        return mult;                         }                                                                                 }```
• 09-13-2010, 06:33 AM
Zack
a[i].length and b.length are only 3 each, and k has a value of 3, which means it's trying to read the fourth element.
• 09-13-2010, 06:35 AM
slixtrix
how do i fix that? maybe j < bCols instead of bCols -1?
• 09-13-2010, 06:37 AM
Zack

Your k value is going too high--it should go 2 instead of 3. Changing the j values will not fix this. Instead, you should be focused on this line:
Code:

`for(int k = 0; k < bCols; k++){`
You're going to want to change something in that line.

Edit: Fixed incorrect numerical values above.
• 09-13-2010, 06:41 AM
slixtrix
from what im understanding, i should change
Code:

```for(int j = 0; j < bCols - 1; j++) {                                 for(int k = 0; k < bCols; k++){```
to

f
Code:

```or(int j = 0; j < bCols; j++) {                                 for(int k = 0; k < bCols-1; k++){```
am i misunderstanding what im doing wrong?
• 09-13-2010, 06:45 AM
Zack
When in doubt, give it a try!

But yes, you are correct, assuming this is the desired output:
Code:

```Hello TV land! Matrix 1: Row: 0 Column: 0 = 1 Row: 0 Column: 1 = 2 Row: 0 Column: 2 = 4 Row: 1 Column: 0 = 2 Row: 1 Column: 1 = 6 Row: 1 Column: 2 = 0 Matrix 2: Row: 0 Column: 0 = 4 Row: 0 Column: 1 = 1 Row: 0 Column: 2 = 4 Row: 0 Column: 3 = 3 Row: 1 Column: 0 = 0 Row: 1 Column: 1 = -1 Row: 1 Column: 2 = 3 Row: 1 Column: 3 = 1 Row: 2 Column: 0 = 2 Row: 2 Column: 1 = 7 Row: 2 Column: 2 = 5 Row: 2 Column: 3 = 2 Matrix 3: Row: 0 Column: 0 = 12 Row: 0 Column: 1 = 27 Row: 0 Column: 2 = 30 Row: 0 Column: 3 = 0 Row: 1 Column: 0 = 8 Row: 1 Column: 1 = -4 Row: 1 Column: 2 = 26 Row: 1 Column: 3 = 0```
• 09-13-2010, 06:47 AM
slixtrix
when i basically swap the J and K (bCols) and (bCols - 1) i get actual values for [0][3] and [1][3] instead of the 0 that shows up on yours

I do greatly appreciate you helping me put my brain back on the tracks on what i need to fix
• 09-13-2010, 06:49 AM
Zack
Ah, yes, I didn't notice the change in the j statement (just the k statement). My apologies. Is the program now working as you expected?
• 09-13-2010, 06:50 AM
slixtrix
yes it is, Thanks for your help