do my program correct?????anyone help pls

ccy97
ccy97 used Ask the Experts™
on
when i passing two matrix number to the below procedure,it return wrong value to me.so my problem is which is the best way to do multiplication matrices.Am my code any problem there.if u know how to do matrix multiplication please show me the code or help me to change my code.
public int [][] mult(int [][] A,int [][] B) throws java.rmi.RemoteException{
      int d1 = A.length;
      int d2 = B[0].length;
      int d3 = A[0].length;
      int [][] C= new int [d1][d2];
       for (int i=0; i<d1 ; i++)
      for (int j=0; j<d2 ; j++)
      {
          for (int k=0; k<d3 ; k++)
     C[i][j]=A[i][k]*B[k][j];
      }
     return C;
 }
second thing is scalar multiplication.when i passing two matrix number to the below procedure,it return wrong value to me.so my problem is which is the best way to do multiplication matrices.Am my code any problem there.if u know how to do matrix multiplication please show me the code or help me to change my code.hits:scalar multiplication is a number multiply with a matrices.example, 5*{{1,2},{3,4}}
                   

public  int [][] scalar(int [][] A,int [][] B) throws java.rmi.RemoteException{
     int d1 = A.length;
      int d2 = B.length;
      int [][] C= new int [d1][d2];
       for (int i=0; i<d1 ; i++)
      for (int j=0; j<d2 ; j++)
     C[j][j]=A[i][i]*B[j][j];
     return C;
 }
if anyone really can help to solve the problem i had or the code u give really work then i sure increase the point to u.thanks an di do appreaciate u.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Commented:
The problem with your code is that it doesn't implement the algorithm for matrix multiplication.
Look up the definition of matrix multiplication. Here's what the code should look like (I've split it up into two methods):

//calculates a single entry in the matrix C = A * B at row <row> and column <col>
public int calculate(int[][] A, int[][] B, int row, int col) {
  int result = 0;
  for (int i = 0; i < B.length; i++)
    result += A[row][i] * B[i][col];

    return result;
  }

public int[][] mult(int [][] A,int [][] B) {
  int d1 = A.length;
  int d2 = B[0].length;
  int [][] C= new int [d1][d2];

  //fill in values for C
  for (int r = 0; r < d1; r++)
    for (int c = 0; c < d2; c++)
      C[r][c] = calculate(A, B, r, c);

  return C;
}

Note that I haven't included checks on whether A can be mulitplied with B. I leave that for you to do.

If the code doesn't make sense, think through it carefully, maybe take pen and paper and do the calculations as the program would do it.

As for scalar multiplication, I don't want to do all the work for you! Look up definition of scalar multiplication and check your code. You should see why your code won't work. Hint: where's the scalar you want to multiply with? the method should be declared like this:

public int[][] scalarMult(int[][] A, int scalar)

If you can't get it, post again and I'll help some more.

Commented:
Thanks for accepting my answer, but why did you rate it C?

I know my code works, so were my explanations not good enough?
If so, you should've let me know, then I would have been able to explain in more detail.
Or was it because I only answered half of the question?
I did that because I wanted to give you a hint first. It is much better to learn something by doing it yourself rather than just copying someone else's code.
If you had problems getting the second answer yourself, you should've said so, I would've helped (as I stated in my post).

Before giving out any more grades, please read
http://www.experts-exchange.com/Community_Support/New_to_EE/Q_20310437.html

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial