This week's book giveaway is in the HTML Pages with CSS and JavaScript forum.
We're giving away four copies of Testing JavaScript Applications and have Lucas da Costa on-line!
See this thread for details.
Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript 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
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Where is my logic error? I have to reverse the rows in 2d array

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

 
Marshal
Posts: 25682
69
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Ayugm, welcome to the Ranch!

Here's my question for you: I see a lot of code, and you seem to think there's a logic error in it. But what makes you think that? Have a look at our FAQ entry named TellTheDetails (<-- click on that link) if you don't see why I asked that question.
 
Marshal
Posts: 69894
278
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome again

I also think you are going the wrong way about writing the application. I think you shou‍ld write a utility class with methods which swap two elements in an array, reverse an array, etc. You can do the reversing with the entire array, because that isn't a 2D array. It is an array of arrays. So you treat the outer array as one array and simply write a method to reverse it. I shall give you a start: you have to declare the method as generic with the <T>, so you can pass an array of any reference type:-Once you have those two methods working, you can keep them for ever.
 
Ayugm Acharya
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Unfortunately, my prof hasn't done utilities with us at all. I created a tester class that worked, but I can't figure out how to put it into my other main program:
 
Campbell Ritchie
Marshal
Posts: 69894
278
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ayugm Acharya wrote:Unfortunately, my prof hasn't done utilities with us at all.

Maybe not, but you have asked on a reputable website and been given the suggestion. You have also been given the suggestion that swapping two elements and reversing an array merit methods of their own.

I created a tester class that worked,  . . .

Clever programming, creating a stack and using that for reversing, but what did the program do? It has not reversed the array. Reversing the array would put row₀ last and rowₙ first. You have gone one further and reversed the included arrays as well. By the way: to print your matrix, use this (not in the loop):-

Campbell's Computer wrote:java MatrixRevTester
[[0, 1, 2, 3], [4, 5, 6, 7], [8, 9, 10, 11], [12, 13, 14, 15]]
Reversing...
[[15, 14, 13, 12], [11, 10, 9, 8], [7, 6, 5, 4], [3, 2, 1, 0]]

Dividing the task into smaller stages will make the problem easier to solve. A standard programming technique which everybody shou‍ld know. If you reverse the included arrays, you will need to overload those methods to take int[]s.Once you have worked out the logic for one method, the other method will use the same logic.
 
Don't get me started about those stupid light bulbs.
    Bookmark Topic Watch Topic
  • New Topic