• Post Reply Bookmark Topic Watch Topic
  • New Topic

Overloaded function for 2D arrays  RSS feed

 
Jeff Sak
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am having a hard time figuring out how to incorporate my sum function for a 1 dimensional array into my sum function for a 2 dimensional array. I wrote my function for a code and it does compile through, but the output doesn't come out with the sum, but outputs zero instead. I want to understand how to play with these arrays, and understand how to play around with overloaded functions. I have other aspects of arrays I want to code into this program, such as trying to flatten a 2d array into a 1d array, and also make a boolean function to see if all the elements in a 2d array are the same length, and I feel like if I can understand how to make this sum function work with the 2d array, I will be able to figure out how to do the rest with trial and error.

 
Liutauras Vilda
Sheriff
Posts: 4928
334
BSD
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, current code you posted does not compile, so it cannot output zero as you stated.

Line 22 should be looking similarly (but not the same) as line 16. Look carefully and you'll see where you did mistake.
Note: I haven't checked rectangular and flatten methods - first things first

Jeff Sak wrote:I will be able to figure out how to do the rest with trial and error.
Usually this approach is not good. Campbell says you could try 1000 attempts and you will likely find the right way to do it, OR you could think about it, and get it right sooner
 
Liutauras Vilda
Sheriff
Posts: 4928
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, if your class name wouldn't be as it is, I might wouldn't tell you, but:
  • Class names suppose to start with an upper case, that is conventional, so please to follow that.
  • In Java multidimensional arrays are not truly 2D or 3D... these are array of arrays. Look up on google for the explaining articles (in case you are curious). So better call the class MultidimensionalArray.
  •  
    Winston Gutkowski
    Bartender
    Posts: 10575
    66
    Eclipse IDE Hibernate Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Jeff Sak wrote:I wrote my function for a code and it does compile...

    No it doesn't - at least not what you've shown us - because line 33 is wrong.

    And this goes back to what Liutauras said in his post:
      first things first

    Get rid of everything that you're not ready to test, and concentrate on the two sum() methods you're having trouble with.

    When - and ONLY when - you have them working, add back your rectangular() method (which looks to me like it should be called square(); or, more correctly, isSquare()), and test that.

    And when - and ONLY when - rectangular() is working, add back your flatten() method...

    Do you get the idea? All that extra code is distracting you from the job at hand, which is to get your sum() methods working properly.

    HIH

    Winston
     
    Jeff Sak
    Ranch Hand
    Posts: 38
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    My apologies, I had copied a the wrong set of code. I have posted the code that is coming up with a zero when I run and compile it. I just tested it out and it does still come back with 0.

     
    Knute Snortum
    Sheriff
    Posts: 4287
    127
    Chrome Eclipse IDE Java Postgres Database VI Editor
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You're not doing anything with the return value in line ten, sum(r).
     
    Jeff Sak
    Ranch Hand
    Posts: 38
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I'm trying to recall my previous sum function for a 1d array into my 2d array function, but I'm not sure how to input it so that it shows the return value for sum(r). In my head the sum function should be working, but for each element in the 2D array when I recall the function, and when I input return sum, it should have the sum of all the numbers in my 2D array, but that's not the case.
     
    Liutauras Vilda
    Sheriff
    Posts: 4928
    334
    BSD
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    i being evaluated during the program run and added to sum. Each time i gets different value, right?

    This method bellow also evaluates every loop iteration, so it returns some kind of value. What do you need to do with that value in order to have an effect to your sum?
     
    Jeff Sak
    Ranch Hand
    Posts: 38
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thank you for letting me figure out the input on my own while guiding me to the answer. I input sum += sum(r) and it worked! Now time to figure out the other two functions I am trying to output.
     
    Liutauras Vilda
    Sheriff
    Posts: 4928
    334
    BSD
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Bingo, well done
     
    It is sorta covered in the JavaRanch Style Guide.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!