• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to return the sum of a multi dimensional array?  RSS feed

 
Khaly Silverman
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what would the return be for adding two array's together,



My return is wrong...... I know, Any hints?
 
Varun Chandra Jammula
Greenhorn
Posts: 2
Mac OS X MySQL Database PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bring variable 'c' out of the for loop and change the return type to public static int[] [].
 
Paul Clapham
Sheriff
Posts: 22844
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Agreed. The sum of two arrays would be another array, right? And that's what you want to return.
 
Khaly Silverman
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I keep getting
error: incompatible types: int[][] cannot be converted to int return c;
 
Khaly Silverman
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes I would like to return the sum of the previous 2 arrays,
 
Khaly Silverman
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alright so finally I'm not getting a compile error, just a logic one.... :S Now my array is filled with 0's instead of the sum of array1 and array2
 
Khaly Silverman
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank-you everyone I am done Small little errors, make me crazy lol.
 
Jude Niroshan
Ranch Hand
Posts: 132
5
Eclipse IDE Java Postgres Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
i think you are passing two arrays which are almost exactly the same dimensions. (That how you have written the initialization of array C).



Output ::::::
5 :: 10 :: 15 :: 20 ::

5 :: 10 :: 15 :: 0 ::

5 :: 10 :: 0 :: 0 ::

5 :: 0 :: 0 :: 0 ::
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Avoid \n unless you have been asked for the LF character.
That is not how you print an array. Use this:-
System.out.println(Arrays.deepToString(myArray));
 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That adding code is brittle; it will throw Exceptions if any array pairs are different sizes.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Khaly Silverman wrote:Alright so finally I'm not getting a compile error, just a logic one.... :S Now my array is filled with 0's instead of the sum of array1 and array2

OK, so could you show us what you've written now?

Winston
 
Ashwin Rao
Ranch Hand
Posts: 89
C++ Java Python
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
int[] [] c = new int[a[0].length][b[0].length];

Can someone please tell me what a[0].length and b[0].length are doing there?
Thanks
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ashwin Rao wrote:Can someone please tell me what a[0].length and b[0].length are doing there?

Well spotted; I hadn't noticed that.

@Khaly:
1. That initialization is wrong (unless I've totally misconstrued what you're trying to do).
2. Your method currently assumes that 'b' is at least as big as 'a', but will silently miss out trailing rows or columns of 'b' if they are larger.

I think you need to back up a bit and work out precisely what you want to happen in all cases.

For example: if a row of 'b' is longer than a row of 'a', do you want its trailing elements ignored?
Or is the idea that 'a' and 'b' should be identical in size. If so, you'll need to enforce that.

Winston
 
Khaly Silverman
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:
1. That initialization is wrong (unless I've totally misconstrued what you're trying to do).
2. Your method currently assumes that 'b' is at least as big as 'a', but will silently miss out trailing rows or columns of 'b' if they are larger.





Winston, The array's will be the exact same size, in the assignment it says to assume that they will be.
 
Khaly Silverman
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Khaly Silverman wrote:Winston, The array's will be the exact same size, in the assignment it says to assume that they will be.

OK, but if that's the case, why not just write:

int[][] c = new int[a.length][a[0].length];

Using 'b' is just confusing.

And even then the declaration may still be wrong - unless there's another assumption you're allowed to make that you haven't told us about.

Have a think about what that line does, and see if you can work out why it might still be wrong for yourself.

Winston
 
Divyanand Kutagulla
Greenhorn
Posts: 18
C++ Java Suse
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Khaly Silverman wrote:
Winston Gutkowski wrote:
1. That initialization is wrong (unless I've totally misconstrued what you're trying to do).
2. Your method currently assumes that 'b' is at least as big as 'a', but will silently miss out trailing rows or columns of 'b' if they are larger.





Winston, The array's will be the exact same size, in the assignment it says to assume that they will be.


@Khaly
You can assertions to ensure that assumption
or you could also put the summing code in try-catch block and throw an exception if arrays are different sizes

 
Campbell Ritchie
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Divyanand Kutagulla wrote: . . .
@Khaly
You can assertions to ensure that assumption
or you could also put the summing code in try-catch block and throw an exception if arrays are different sizes
I don't think either approach will work. Assertions are usually disabled and are only used for testing, so they are unlikely to cause any action. A catch will more likely prevent the Exception from propagating. I would suggest you test each array pair for size and throw an IllegalArgumentException or similar if any of the sizes are different.

Now we have the additional information that the array pairs are guaranteed to be the same sizes, which we didn't know earlier.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!