Hi,

Sorry for another thread. I'm trying to do matrix multiplication in Java now.

Here's my code (that wont work). Please let me know if you spot anything that I should change...

``````class multiply
{

public static void main(String[] args)
{

int[][] a = {{1,2,3}, {1,2,3}, {1,2,3}};

int[][] b = {{3,2,1}, {3,2,1}, {3,2,1}};

int[][] c = new double[3][3];

int i,j,k;

for (i = 0, i < 3; i++);

for (j = 0; j < 3; j++);
{
c[i][j] = 0.0;

for (k = 0; k < 3; k++)
{
c[i][j] = c[i][j] + a[i][k] * b[k][j];
}
}
System.out.println(“a.b = “+c[i][0]+” ”+c[i][1]+” ”+c[i][2]);

}

}``````
2
Contributors
3
Replies
5
Views
7 Years
Discussion Span
Last Post by jon.kiparsky
``````for (i = 0, i < 3; i++);

for (j = 0; j < 3; j++);``````

These lines simply count to three, twice.

In NetBeans it is still giving me this error though:

``````Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - incompatible types
required: int[][]
found:    double[][]
at multiply.main(multiply.java:11)
Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)``````

Oh, yeah. That's because in this line

``int[][] c = new double[3][3];``

you declare a 2-dimensional array of ints and try to put a 2-dimensional array of doubles into it.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.