• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Spiral Matrix

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
I have a problem with reading 2D array in spiral way. It must be read from bottom left corner - to bottom right corner - to top right corner etc like spiral. I try different approaches but still get in some step ArrayIndexOutOfBoundsException. Please help me complete "output" method.

Input example:
10 9 8 7
11 16 15 6
12 13 14 5
1 2 3 4

Ouput example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16


 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

matej spac wrote:Hello,
I have a problem with reading 2D array in spiral way. It must be read from bottom left corner - to bottom right corner - to top right corner etc like spiral. I try different approaches but still get in some step ArrayIndexOutOfBoundsException. Please help me complete "output" method.


Well first, I don't see a program; I see a set of definitions and an output statement - admittedly with a loop, but that's not a program in any way I understand the word.

My advice (given many times):
1. Turn OFF your computer.
2. Get out a pencil and paper
3. Pick a 2D array size (which, presumably is a square), say 4x4, and select the "spiral" entries yourself BY HAND.
4. Now do the same thing with a 5x5 square, and work out the similarities with what you did in Step 3. (Note: One important thing about a program is to know when to stop).
5. If you still haven't got it, try with a 6x6 square, and (if need be) a 7x7.
6. Write down all the steps that you now understand about the problem in English (or your native language).
7. NOW turn your computer back on and try to write your program from your notes. If you run into trouble, come back and show us what you've done.

You've probably heard this before, but computers are stupid. They do exactly what they're told to (ibid: Captain Kirk, stardate 4231-point-3 et al), even with a wonderful language like Java.

Your task as a programmer, should you choose to accept it, is to translate problems into computer-speak; and that involves understanding the problem.
If you don't understand the solution, I can guarantee you: Java won't help you.

Winston
 
Marshal
Posts: 80616
468
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What’s a 2D array? They might have them in some languages, but they don’t in Java. There are arrays of arrays in Java™, which are better than 2D arrays anyway.
 
matej spac
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Winston Gutkowski wrote:

matej spac wrote:Hello,
I have a problem with reading 2D array in spiral way. It must be read from bottom left corner - to bottom right corner - to top right corner etc like spiral. I try different approaches but still get in some step ArrayIndexOutOfBoundsException. Please help me complete "output" method.


Well first, I don't see a program; I see a set of definitions and an output statement - admittedly with a loop, but that's not a program in any way I understand the word.


I don´t wrote "I have a program", but "I have a problem"
However, I follow your steps and it turns out that you are genius - I found solution in 15 minutes
Thanks.
 
Campbell Ritchie
Marshal
Posts: 80616
468
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, WG is good. Please tell us how you solved the problem.
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

matej spac wrote:However, I follow your steps and it turns out that you are genius - I found solution in 15 minutes
Thanks.


You're welcome. I too would be interested in what you came up with.

Winston
 
matej spac
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Matrix must be read form left bottom corner to right bottom corner etc in spiral way.
I read matrix by sides and each reading reduces items in side by 2 items.

This solution works for any type of matrix, however, one senior programmer told me "it is not ideal and you use too many variables" .



 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

matej spac wrote:This solution works for any type of matrix, however, one senior programmer told me "it is not ideal and you use too many variables" .


I'd agree with him there, but the main thing is - it WORKS, and you've thought it through.

You can always refine a program later once you've got it working.

A couple of suggestions for you:

1. Don't mix the problem (making a spiral) with its representation (how it looks, or how you print out the results). If it were me, I think I'd be looking at a method that just returns me the coordinates of each point in the spiral (possibly even all of them together as an array). You could then use them to display the spiral any way you like. Have a look at java.awt.Point to help you do this.

2. Space out your code and use indentation. Also, don't put more than one statement on a line:
if (d <= 0)
   continue;
System.out.print(matrix[delta+1+dx][dx] + " ");
flag++;

is a lot easier to read than:
if(d <= 0) continue;
System.out.print(matrix[delta+1+dx][dx] +" "); flag++;


But it's nice to see that you have a lot of comments. You might also want to read up the tutorials on using Javadoc; it is incredibly useful (one of the best programming documentation tools I've ever seen), and allows you to generate API documentation just like the ones you get for the regular Java classes.

HIH

Winston
 
Ranch Hand
Posts: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i admit i didn't read your code, but as Winston suggested the Point class might be useful to you. a long time ago i wrote a homework assignment that used a 2D array and a stack of points. i recently revisited it and found a bug and asked for help here at javaranch. you might want to take a look at how i used points to reference the elements in the array. the code is here

What’s a 2D array? They might have them in some languages, but they don’t in Java. There are arrays of arrays in Java™, which are better than 2D arrays anyway.



perhaps they are technically arrays of arrays but i also choose to call them two dimensional arrays.
 
Sheriff
Posts: 3837
66
Netbeans IDE Oracle Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Winston Gutkowski wrote:1. Don't mix the problem (making a spiral) with its representation (how it looks, or how you print out the results). If it were me, I think I'd be looking at a method that just returns me the coordinates of each point in the spiral (possibly even all of them together as an array). You could then use them to display the spiral any way you like.


Another possibility along these lines would be creating and returning an iterator that would traverse the 2D array in the required way.
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Martin Vajsar wrote:Another possibility along these lines would be creating and returning an iterator that would traverse the 2D array in the required way.


Yes, I rather like that. Very slick.

Winston
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic