5 Replies - 17097 Views - Last Post: 27 September 2007 - 03:20 AM Rate Topic: -----

#1 EquinoX  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 63
  • Joined: 19-January 07

Adding Matrices in Java

Posted 22 January 2007 - 04:18 PM

I am asked to answer this question

The sum of two matrices a and b stored in the two-dimensional array c is defined as follows:

c[j][k] = a[j][k] + b[j][k] for j ranging from 0 to m-1 and k ranging from 0 to n-1

Write a test method that initializes two 2-D arrays named a and b that have the same number of rows and the same number of columns. Implement a method named add to returns a 2-D array that is the sum of the two arguments, which are both 2-D arrays. This call to the add method should compile:

int[][] c = add(a, B);

Make assertions to ensure that every element in the 2-D array referenced by c has the correct value.


Below is my answer:

private int[][] add(int a[][], int b[][]){
int[][]result = new int[a.length][a[0].length];
for (int j=0; j<a.length; j++){
for(int k = 0; k<a[j].length; k++){
result[j][k] = a[j][k] + b[j][k];
}
}
return result;
}




Just want to know if the code I write is right??

Is This A Good Question/Topic? 0
  • +

Replies To: Adding Matrices in Java

#2 PennyBoki  Icon User is offline

  • system("revolution");
  • member icon

Reputation: 53
  • View blog
  • Posts: 2,334
  • Joined: 11-December 06

Re: Adding Matrices in Java

Posted 22 January 2007 - 04:37 PM

Don't wanna be rude but the best way to find out
is to ask the good old COMPILER.
Was This Post Helpful? 0
  • +
  • -

#3 EquinoX  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 63
  • Joined: 19-January 07

Re: Adding Matrices in Java

Posted 22 January 2007 - 04:47 PM

the compiler works perfectly fine but I just want to ask you guys if the code I made does what the question wants?? Cause I don't really get the question and this is the best of what I understand from the question
Was This Post Helpful? 0
  • +
  • -

#4 PennyBoki  Icon User is offline

  • system("revolution");
  • member icon

Reputation: 53
  • View blog
  • Posts: 2,334
  • Joined: 11-December 06

Re: Adding Matrices in Java

Posted 22 January 2007 - 05:16 PM

You did OK with the function.
But I don't see why you don't really get it?

Quote

Write a test method that initializes two 2-D arrays named a and b that have the same number of rows and the same number of columns


This means that m=n i.e. j and k are ranging
from 0 to a.length or b.length.

At the end of the day it's only adding numbers with same indexes.

This post has been edited by PennyBoki: 22 January 2007 - 05:17 PM

Was This Post Helpful? 0
  • +
  • -

#5 EquinoX  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 63
  • Joined: 19-January 07

Re: Adding Matrices in Java

Posted 22 January 2007 - 05:42 PM

thanks.. I am just not confident with my code
Was This Post Helpful? 0
  • +
  • -

#6 lavs8765  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 1
  • Joined: 27-September 07

Re: Adding Matrices in Java

Posted 27 September 2007 - 03:20 AM

View PostEquinoX, on 22 Jan, 2007 - 04:18 PM, said:

I am asked to answer this question

The sum of two matrices a and b stored in the two-dimensional array c is defined as follows:

c[j][k] = a[j][k] + b[j][k] for j ranging from 0 to m-1 and k ranging from 0 to n-1

Write a test method that initializes two 2-D arrays named a and b that have the same number of rows and the same number of columns. Implement a method named add to returns a 2-D array that is the sum of the two arguments, which are both 2-D arrays. This call to the add method should compile:

int[][] c = add(a, B);

Make assertions to ensure that every element in the 2-D array referenced by c has the correct value.


Below is my answer:

private int[][] add(int a[][], int b[][]){
int[][]result = new int[a.length][a[0].length];
for (int j=0; j<a.length; j++){
for(int k = 0; k<a[j].length; k++){
result[j][k] = a[j][k] + b[j][k];
}
}
return result;
}




Just want to know if the code I write is right??

Was This Post Helpful? 1

Page 1 of 1