Page 1 of 1

## 5 Replies - 17655 Views - Last Post: 27 September 2007 - 03:20 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=23154&amp;s=180cc8faab1a1fe6714835b48dae6274&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 EquinoX

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

Posted 22 January 2007 - 04:18 PM

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:

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

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

• system("revolution");

Reputation: 53
• Posts: 2,335
• 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.

### #3 EquinoX

Reputation: 1
• 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

### #4 PennyBoki

• system("revolution");

Reputation: 53
• Posts: 2,335
• 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

### #5 EquinoX

Reputation: 1
• 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

### #6 lavs8765

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

## Re: Adding Matrices in Java

Posted 27 September 2007 - 03:20 AM

EquinoX, on 22 Jan, 2007 - 04:18 PM, said:

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:

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

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??