How to create a spiral matrix?

Discussion in 'Java' started by indian_gaurav, Sep 2, 2008.

  1. Let A be an array of (n) elements. Write a procedure which will také
    this array as input and return a matrix which will be spiral in
    nature. i.e.

    if A= [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16] then


    fn(A)= B

    =

    7 8 9 10

    6 1 2 11

    5 4 3 12

    16 15 14 13

    although i could easily handle the problem for a 2 dimension matrix
    the problem has to be solved in 3 dimension as well as n dimension.

    i'm finding it difficult to visualize the same problem for 3 dimension
    and then for n dimension.
    can som1 help me?

    thank u
    Gaurav

    Plz write to
    indian_gaurav, Sep 2, 2008
    #1
    1. Advertising

  2. indian_gaurav

    Roedy Green Guest

    On Tue, 2 Sep 2008 11:39:54 -0700 (PDT), indian_gaurav
    <> wrote, quoted or indirectly quoted
    someone who said :

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


    So how did you create that? You first decided how many rows/columns
    you need. You can do that with sqrt and rounding up. See
    http://mindprod.com/jgloss/round.html

    Then you had to find the center of the matrix. That is matter of
    dividing the size by 2 with possibly a one off adjust.

    Then you have a loop that spirals outward. It starts in the bottom
    left corner and spiral clockwise

    you spiral out 0, 1, 2 , 3 etc rows. So compute the co-ordinates of
    the bottom right corner of the next layer. Now plop down the numbers
    for the left, top, right and bottom sides, incrementing your number by
    one each time. you continue with the loop for the next layer out.
    --

    Roedy Green Canadian Mind Products
    The Java Glossary
    http://mindprod.com
    Roedy Green, Sep 6, 2008
    #2
    1. Advertising

  3. indian_gaurav

    Roedy Green Guest

    On Tue, 2 Sep 2008 11:39:54 -0700 (PDT), indian_gaurav
    <> wrote, quoted or indirectly quoted
    someone who said :

    >
    >i'm finding it difficult to visualize the same problem for 3 dimension
    >and then for n dimension.


    You probably can't. But what you can do is see the obvious
    generalisation of the structure of your code. however, you will need
    the code under your nose first to do that.

    --

    Roedy Green Canadian Mind Products
    The Java Glossary
    http://mindprod.com
    Roedy Green, Sep 6, 2008
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Usman

    SPIRAL PROBLEMS

    Usman, Feb 23, 2004, in forum: C++
    Replies:
    3
    Views:
    678
    Usman
    Feb 24, 2004
  2. lvcargnini

    Matrix composed by two matrix

    lvcargnini, Jul 4, 2006, in forum: VHDL
    Replies:
    3
    Views:
    2,667
    Jonathan Bromley
    Jul 5, 2006
  3. Spiral Access of 2 d array.

    , Aug 29, 2007, in forum: C Programming
    Replies:
    8
    Views:
    408
    Keith Thompson
    Aug 29, 2007
  4. Ruby Quiz

    [QUIZ] Number Spiral (#109)

    Ruby Quiz, Jan 12, 2007, in forum: Ruby
    Replies:
    27
    Views:
    307
    William James
    Jan 20, 2007
  5. Ruby Quiz

    [SUMMARY] Number Spiral (#109)

    Ruby Quiz, Jan 18, 2007, in forum: Ruby
    Replies:
    0
    Views:
    119
    Ruby Quiz
    Jan 18, 2007
Loading...

Share This Page