Welcome to Dream.In.Code
Become a Java Expert!

Join 150,382 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,195 people online right now. Registration is fast and FREE... Join Now!




Matrix problem

 
Reply to this topicStart new topic

Matrix problem

geoalesolo
23 Jan, 2008 - 07:00 PM
Post #1

New D.I.C Head
*

Joined: 23 Jan, 2008
Posts: 1

I want to multiply this array
1 2 -2 0
-3 4 7 2
6 0 3 1
with this array
-1 3
0 9
1 -11
4 -5
to get this
-3 43
18 -60
1 -20

But whenever i run it i get this error ArrayIndexOutOfBoundsException:2
Can you please help me fix this
CODE

public class tester
{
    public static void main(String[] args)  {
    int array[][] = {{1,2,-2,0},{-3,4,7,2},{6,0,3,1},};
    int array1[][] = {{-1,3},{0,9},{1,-11},{4,-5}};
    int array2[][] = new int[3][2];
    int x= array.length;
    System.out.println("Matrix 1 : ");
      for(int i = 0; i < x; i++) {
      for(int j = 0; j <= x; j++) {
      }
    }  
    int y= array1.length;
      for(int i = 0; i < y; i++) {
      for(int j = 0; j < y-1; j++) {
      }  
    }
    
      for(int i = 0; i < x; i++) {
      for(int j = 0; j < y-1; j++) {
        for(int k = 0; k < y; k++){
          
          array2[i][j] += array[i][k]*array1[k][j]; //the error highlights this line
        }
      }  
     }
    for(int i = 0; i < x; i++) {
      for(int j = 0; j < y-1; j++) {
        System.out.print(array2[i][j]);
      }  
    }
  }
}

User is offlineProfile CardPM
+Quote Post

Nayana
RE: Matrix Problem
23 Jan, 2008 - 08:30 PM
Post #2

DIC Hawk - 나야나 नयन:
Group Icon

Joined: 14 Nov, 2007
Posts: 824



Thanked: 5 times
Dream Kudos: 175
My Contributions
-deleted-

This post has been edited by Nayana: 24 Jan, 2008 - 09:53 PM
User is offlineProfile CardPM
+Quote Post

PennyBoki
RE: Matrix Problem
24 Jan, 2008 - 11:27 AM
Post #3

system("revolution");
Group Icon

Joined: 11 Dec, 2006
Posts: 2,011



Thanked: 7 times
Dream Kudos: 500
Expert In: Java,C++,C

My Contributions
geoalesolo, you have to be careful with the values of x, y, k.

I suggest you carefully examine your code:
CODE

for(int i = 0; i < x; i++) {
      for(int j = 0; j < y-1; j++) {
        for(int k = 0; k < y; k++){
          
          array2[i][j] += array[i][k]*array1[k][j]; //the error highlights this line
        }
      }  
     }

User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 04:26PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month