Win a copy of AWS Security this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

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

 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!

 
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Ashley Kin
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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?
 
Sheriff
Posts: 67403
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
Mohamed Sanaulla
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Let me tell you a story about a man named Jed. He made this tiny ad:
Devious Experiments for a Truly Passive Greenhouse!
https://www.kickstarter.com/projects/paulwheaton/greenhouse-1
    Bookmark Topic Watch Topic
  • New Topic