Tell me more ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.
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

share|improve this question

1 Answer

up vote 3 down vote accepted

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.