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);
}
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;
}
}


You probably should reread the specifications. “Check if the first dimensions and the second dimensions of each 2-dim. array array are the same” means that you should check if first and second matrix lengths match; it does not mean to test doubMatrix1[i][j] == doubMatrix2[i][j]. Also test sizes of doubMatrix1[0] and doubMatrix2[0]; ie besides testing doubMatrix1.length vs doubMatrix2.length, test doubMatrix1[0].length vs doubMatrix2[0].length. If the sizes don't match, then do your return tempArray = new double[0][0]; statement, or perhaps return new double[0][0];. That particular statement should be before, not inside, the nested for loops.

“Add each corresponding element in the parameter 2-dim. arrays and store the result” means to do something like your
tempArray[i][j] = doubMatrix1[i][j] + doubMatrix2[i][j]; statement. However, your code repeatedly reallocates tempArray[][], so tempArray[][] never has more than one element set in it. Allocate the array earlier in your program, perhaps after you know the sizes match.

This is Java, not C-before-C99. You can declare your variables in the loop statement, rather than before the loop statement:

int i,j = 0;
for(i = 0; i< doubMatrix1.length;i++)
for(j = 0; j< doubMatrix1[i].length;j++ )


Change that to:

for(int i = 0; i< doubMatrix1.length;i++)
for(int j = 0; j< doubMatrix1[i].length;j++ )


The first statement is entirely redundant as you never use them outside the scope of the loop.

You should format your code better. j< doubMatrix1[i].length;j++ ) is messy, and should read as j < doubMatrix1[i].length; j++)

Your indentation is messy too. Mainly, you should be consistent, and 4 spaces is the typical Java standard. This, for example, is difficult to read and see the scope automatically:

}
public static double[][] getdoubMatrix(int d1, int d2){


int i,j = 0;


That assignment only assigns j, i is unassigned. As I mentioned earlier, these are redundant anyway, but you should know about this behavior. If you want i assigned too, you have to do int i = 0, j = 0;.

• This is Java, not C. You can declare your variables in the loop statement, rather than before the loop statement: <- you can in C99 too Commented Dec 14, 2015 at 18:42