This is what the code should do:
Check if the first dimensions and the second dimensions of each 2-dimensionalarray are the same. If they are not the same, then return a 0x0 2-dimensional array. array, otherwise do the following;
Allocate memory for a local 2-dim. array with the same dimensions as one of the 2-dim. array parameters
Add each corresponding element in the parameter 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
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;
}
}