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[][]){
double[][] tempArray = null;
int i,j = 0;
for(i = 0; i< doubMatrix1.length;i++)
for(j = 0; j< doubMatrix1[i].length;j++ )
{
if(doubMatrix1[i][j] == doubMatrix2[i][j])
{
tempArray = new double[i][j];
tempArray[i][j] = doubMatrix1[i][j] + doubMatrix2[i][j];
}
else
{
return tempArray = new double[0][0];
}
}
return tempArray;
}
}
so I have finished the addMatrices method can someone review if my method is correct or not
this is what i was supposed to do for that method
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