import java.lang.Math;
public class Homework2 {
public static void main(String[] args){
int d1 = (int) (Math.random()*(10-3+1)+3);
int d2 = (int) (Math.random()*(10-3+1)+3);
double[][] doubMatrix1 = new double[d1][d2];
double[][] doubMatrix2 = new double[d1][d2];
double[][] doubMatrix3 = new double[d1][d2];
doubMatrix1 = getdoubMatrix(d1,d2);
doubMatrix2 = getdoubMatrix(d1,d2);
doubMatrix3 = addMatrices(doubMatrix1, doubMatrix2);
}
public static double[][] getdoubMatrix(int d1, int d2){
double[][] tempArray = new double[d1][d2];
for(int i =0; i <tempArray.length;i++ )
for(int j =0;j < tempArray[i].length;j++)
tempArray[i][j] = Math.random()*(10.0);
return tempArray;
}
public static double[][] addMatrices(double doubMatrix1[][], double doubMatrix2[][]){
for(int i = 0; i< doubMatrix1.length;i++)
for(int j = 0; j< doubMatrix1[i].length;j++ )
{
if(doubMatrix1[i][j] == doubMatrix2[i][j])
{
double[][] tempArray = new double[i][j];
}
else
{
return tempArray[0][0];
}
}
return tempArray;
}
}
I’m getting error on both return statements in the addMatrices method also I don’t think I’m doing it right
This is what i was supposed to do for the addMatrices method
In the addMatricesmethod,
· Check if the first dimensions and thesecond dimensions of each 2-dim. array array are the same — if they are NOTthe same, return a 0 X 0 2-dim. array, otherwise do the following;
· Allocate memory for a local 2-dim. arraywith the same dimensions as one of the 2-dim. array parameters
· Add each corresponding element in theparameter 2-dim. arrays and store the result in the corresponding element ofthe local 2-dim. array (use nested for loops)
· Return the local 2-dim. array
Well the first problem is that, in your else part you are returning a double value. ArrayElement, while the return type is defined as an
array of array.Secondly, you have declared your
tempArrayinside yourifand are using it outside, while returning.. It would not be visible outside yourif. Declare it in your method, outside your if and for-loop.PRIMARY MODIFICATION NEEDED: –
Change your return statment in your else to: –
And declare your
tempArrayoutside your for-loop.Well, there is lot more than the above problem. Your
addMatrixis logically not adding your matrices. I think you should check onto that code..