Reputation: 23
public class Homework2 {
public static void main(String[] args){
int num1 = (int) (Math.random()*(10-3+1)+3);
int num2 = (int) (Math.random()*(10-3+1)+3);
double[][] doubMatrix1 = new double[num1][num2];
double[][] doubMatrix2 = new double[num1][num2];
double[][] doubMatrix3 = new double[num1][num2];
doubMatrix1 = getdoubMatrix(num1,num2);
doubMatrix2 = getdoubMatrix(num1,num2);
doubMatrix3 = addMatrices(doubMatrix1, doubMatrix2, num1, num2);
printDoubMatrix("First matrix", doubMatrix1);
printDoubMatrix("Second matrix", doubMatrix2);
printDoubMatrix("Result of adding", doubMatrix3);
doubMatrix2 =transposeMatrix(num1,num2);
}
public static double[][] getdoubMatrix(int num1,int num2){
double[][] tempArray = new double[num1][num2];
for(int i = 0;i < tempArray.length;i++)
for(int j = 0;j < tempArray[i].length;j++)
{
tempArray[i][j] = Math.random() * (100);
}
return tempArray;
}
public static double[][] addMatrices(double[][] doubMatrix1, double[][] doubMatrix2,int num1,int num2)
{
double[][] tempArray = null;
if(doubMatrix1.length == doubMatrix2.length)
if(doubMatrix1[0].length == doubMatrix2[0].length)
{
tempArray = new double[num1][num2];
for(int i = 0; i< doubMatrix1.length;i++)
for(int j = 0; j< doubMatrix1[i].length;j++ )
{
tempArray[i][j] = doubMatrix1[i][j] + doubMatrix2[i][j];
}
}
else
{
return tempArray = new double[0][0];
}
return tempArray;
}
public static void printDoubMatrix(String text,double[][] doubMatrix1){
System.out.println(text);
for(int i = 0; i< doubMatrix1.length;i++)
for(int j = 0; j< doubMatrix1[i].length;j++ )
System.out.printf("%f\n", doubMatrix1[i][j]);
}
public static double[][] transposeMatrix(int num1, int num2){
double[][] tempArray = new double[num2][num1];
for(int i = 0;i < tempArray.length;i++)
for(int j = 0;j < tempArray[i].length;j++)
{
tempArray[i][j] = tempArray[j][i];
System.out.printf("%f\n", tempArray[i][j]);
}
return tempArray;
}
}
I have a problem when running this program, there was no error but when I run it it said the array index is out of bound, the problem is at the transpose method, can anyone tell me how to fix this problem?
Upvotes: 0
Views: 627
Reputation: 213311
The assignment in the for-loop of transpose
method should be like: -
tempArray[i][j] = doubleMatrix2[j][i];
rather than: -
tempArray[i][j] = tempArray[j][i];
In the above code, you are assigning the value from a newly created array tempArray
to itself only. It doesn't make sense. It will not affect the array. Also it will throw an ArrayIndexOutOfBounds
exception if row != col
You need to use the matrix you want to transpose.
Since you are invoking this method for doubleMatrix2
doubMatrix2 =transposeMatrix(num1,num2);
And your two matrix are like: -
tempArray[][] = new double[num2][num1];
doubleMatrix[][] = new double[num1][num2];
So it makes sense to assign doubleMatrix[j][i]
to tempArray[i][j]
. Because number of rows and columns are reversed in the two matrices.
Upvotes: 3
Reputation: 2757
In the transpose matrix method, you are transposing in-place. Meaning that you are trying to replace the elements without using any new array. Further the tempArray that is initialized with size of num1 * num2 does not have any values initialized within the method transposeMatrix.
I would suggest the following:
The index out of bounds is obvious because although tempArray[i][j] is valid for all cases, while tempArray[j][j] is not
Here is a sample code you may try:
//assuming array1 is of size num1 * num2
public static double[][] transposeMatrix(int num1, int num2, double[][] array1){
double[][] tempArray = new double[num2][num1];
for(int i = 0;i < num1;i++)
for(int j = 0;j < num2;j++)
{
tempArray[j][i] = array1[i][j];
//System.out.printf("%f\n", tempArray[j][i]);
}
return tempArray;
}
Upvotes: 0
Reputation: 28753
My guess is that the problem is with this loop - and it only happens sometimes.
double[][] tempArray = new double[num2][num1];
for(int i = 0;i < tempArray.length;i++) {
for(int j = 0;j < tempArray[i].length;j++) {
tempArray[i][j] = tempArray[j][i];
System.out.printf("%f\n", tempArray[i][j]);
}
}
So what happens when num2
and num1
(or i
and j
) aren't equal? Let's flatten the loop and use constant values for num1
and num2
...
double[][] tempArray = new double[2][5];
// i=0, j=0
tempArray[0][0] = tempArray[0][0];
System.out.printf("%f\n", tempArray[0][0]);
// i=0, j=1
tempArray[0][1] = tempArray[1][0];
System.out.printf("%f\n", tempArray[0][1]);
// i=0, j=2
tempArray[0][2] = tempArray[2][0]; // Array index out of bounds!
On your third "loop", you're trying to access tempArray[2][0]
- but temp array's size is defined as a double[2][3]
. That means there is no tempArray[2]
, let alone tempArray[2][0]
.
Upvotes: 0
Reputation: 6534
I think you are assuming your 2-d array is not a ragged array, and that it contains the same number of rows and columns. Which is not always true.
Upvotes: 1
Reputation: 6572
Your problem is the following line:
tempArray[i][j] = tempArray[j][i];
The variable j can range from 0 to tempArray[i].length. You, however, are using it to index into tempArray itself (tempArray[j][i]). So if j is greater than tempArray.length, you'll get an error.
Also, the function doesn't appear to do anything since tempArray doesn't have anything in it.
Upvotes: 1
Reputation: 2029
at your transposeMatrix function you must assert that num1 and num2 have the same value. In any other way, it'll result in an ArrayOutOfBound, just check yout logic you'll see that you use "i" and "j" values in bothe dimension of the matrix, so them both mus be equal.
Upvotes: 2