Results 1 to 9 of 9
Thread: matrix multiply
 09132010, 06:21 AM #1Member
 Join Date
 Sep 2010
 Posts
 5
 Rep Power
 0
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
Java 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 < bCols1; j++) { for(int k = 0; k < bCols; k++){ mult[i][j] += a[i][k]*b[k][j]; } } } } return mult; } }
 09132010, 06:33 AM #2
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.
 09132010, 06:35 AM #3Member
 Join Date
 Sep 2010
 Posts
 5
 Rep Power
 0
how do i fix that? maybe j < bCols instead of bCols 1?
 09132010, 06:37 AM #4
Noinstead of guessing, think about this logically.
Your k value is going too highit should go 2 instead of 3. Changing the j values will not fix this. Instead, you should be focused on this line:
Java Code:for(int k = 0; k < bCols; k++){
Edit: Fixed incorrect numerical values above.
 09132010, 06:41 AM #5Member
 Join Date
 Sep 2010
 Posts
 5
 Rep Power
 0
from what im understanding, i should change
Java Code:for(int j = 0; j < bCols  1; j++) { for(int k = 0; k < bCols; k++){
fJava Code:or(int j = 0; j < bCols; j++) { for(int k = 0; k < bCols1; k++){
 09132010, 06:45 AM #6
When in doubt, give it a try!
But yes, you are correct, assuming this is the desired output:
Java 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
 09132010, 06:47 AM #7Member
 Join Date
 Sep 2010
 Posts
 5
 Rep Power
 0
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
 09132010, 06:49 AM #8
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?
 09132010, 06:50 AM #9Member
 Join Date
 Sep 2010
 Posts
 5
 Rep Power
 0
Similar Threads

how to multiply numbers in rows and print it next to it
By racewithferrari in forum New To JavaReplies: 1Last Post: 01162010, 07:24 PM 
multiply two matrixes
By smart princess in forum New To JavaReplies: 8Last Post: 12062009, 07:43 PM 
display column value multiply with 100
By tiiim83 in forum JavaServer Pages (JSP) and JSTLReplies: 0Last Post: 01152009, 04:40 AM 
How to multiply two matrices
By Java Tip in forum java.langReplies: 0Last Post: 04142008, 08:50 PM 
Help with matrix
By susan in forum New To JavaReplies: 1Last Post: 08072007, 04:37 AM
Bookmarks