I am asked to answer this question
The sum of two matrices a and b stored in the twodimensional array c is defined as follows:
c[j][k] = a[j][k] + b[j][k] for j ranging from 0 to m1 and k ranging from 0 to n1
Write a test method that initializes two 2D 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 2D array that is the sum of the two arguments, which are both 2D arrays. This call to the add method should compile:
int[][] c = add(a, ;
Make assertions to ensure that every element in the 2D 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??
Adding Matrices in Java
Page 1 of 15 Replies  17086 Views  Last Post: 27 September 2007  03:20 AM
Replies To: Adding Matrices in Java
#2
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.
is to ask the good old COMPILER.
#3
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
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?
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.
But I don't see why you don't really get it?
Quote
Write a test method that initializes two 2D 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
Re: Adding Matrices in Java
Posted 22 January 2007  05:42 PM
thanks.. I am just not confident with my code
#6
Re: Adding Matrices in Java
Posted 27 September 2007  03:20 AM
EquinoX, 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 twodimensional array c is defined as follows:
c[j][k] = a[j][k] + b[j][k] for j ranging from 0 to m1 and k ranging from 0 to n1
Write a test method that initializes two 2D 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 2D array that is the sum of the two arguments, which are both 2D arrays. This call to the add method should compile:
int[][] c = add(a, ;
Make assertions to ensure that every element in the 2D 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??
The sum of two matrices a and b stored in the twodimensional array c is defined as follows:
c[j][k] = a[j][k] + b[j][k] for j ranging from 0 to m1 and k ranging from 0 to n1
Write a test method that initializes two 2D 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 2D array that is the sum of the two arguments, which are both 2D arrays. This call to the add method should compile:
int[][] c = add(a, ;
Make assertions to ensure that every element in the 2D 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??
Page 1 of 1
