File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Problem with declaring dimensions of a general array? (edited) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Problem with declaring dimensions of a general array? (edited)" Watch "Problem with declaring dimensions of a general array? (edited)" New topic
Author

Problem with declaring dimensions of a general array? (edited)

Ashley Kin
Ranch Hand

Joined: Oct 18, 2011
Posts: 36
Hi, I'm trying to write a method that accepts accepts a pair of two dimensional arrays of integers as parameters (matrixA and matrixB)
and returns a new array as a sum (matrixC). The sum of matrixA and matrixB is matrixC, where for every row i and column j, C(ij) = A(ij) + B(ij).
I'm having trouble initializing matrixC. How do I set it up so I can edit each "element" of the matrix every time I add?
Thanks for any help!

Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3056
    
  33

You dont have i,j initialized.
You would want to take user input regarding the size of the arrays and then store them into some variables.

Also your matrixC isn't initialized and the method matrixAdd expects another argument and you are passing only two matrices whereas it requires 3.

May be you can try using Lists so that you dont have to worry about the size, but anyways you can manage with arrays also.

And I see that in your matrixAdd you are multiplying the elements?


Mohamed Sanaulla | My Blog
Ashley Kin
Ranch Hand

Joined: Oct 18, 2011
Posts: 36
Mohamed Sanaulla wrote:You dont have i,j initialized.
You would want to take user input regarding the size of the arrays and then store them into some variables.

Also your matrixC isn't initialized and the method matrixAdd expects another argument and you are passing only two matrices whereas it requires 3.

May be you can try using Lists so that you dont have to worry about the size, but anyways you can manage with arrays also.

And I see that in your matrixAdd you are multiplying the elements?


Thanks for catching my mistakes, I edited my question with my new code.
Now I'm having some trouble initializing matrixC?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60082
    
  65

Ashley Kin wrote:
Thanks for catching my mistakes, I edited my question with my new code.

Please do not do that. Leave the code as is and post updated code in a reply. Thanks.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3056
    
  33

What is the exception/error you are getting? What does that error say?

If you are adding two matrices, what do you think will be the dimension of the resultant matrice? Accordingly you would have to initialize the matrixC
And you need to see to it that both matixA and matrixB are of similar dimension, otherwise you shouldn't proceed with the addition.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem with declaring dimensions of a general array? (edited)
 
Similar Threads
Referring to an array-HELP!
Something i'm extremely confused on
simple sorting algorithm
summing each row in a 2-dimensional array
Two dimensional array total and sort