Spiral Number Matrices

This is a discussion on Spiral Number Matrices within the C Programming forums, part of the General Programming Boards category; Hello all, I would like to enquire what is the relationship between spiral number matrices like these : n=3 1 ...

  1. #1
    Registered User
    Join Date
    Sep 2010
    Posts
    3

    Spiral Number Matrices

    Hello all, I would like to enquire what is the relationship between spiral number matrices like these :

    n=3
    1 2 3
    8 9 4
    7 6 5
    n=7
    1 2 3 4 5 6 7
    24 25 26 27 28 29 8
    23 40 41 42 43 30 9
    22 39 48 49 44 31 10
    21 38 47 46 45 32 11
    20 37 36 35 34 33 12
    19 18 17 16 15 14 13
    I do know that there are a lot of solutions for such matrices on the web, but most of them are in C++ and just codes. What I would like to ask is whether it is actually possible to derive a general solution for every line? I came up with a few but as the number grew bigger the general solutions became wrong.

    Oh, and also, is it actually possible to solve this matrix without using arrays at all?

    Thanks

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,485
    Hey, I can use google too....

    [maths] number spiral matrix - Fanatic Networks

    Perhaps you haven't come across this yet
    How To Ask Questions The Smart Way
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User Char*Pntr's Avatar
    Join Date
    Sep 2007
    Location
    Lathrop, CA
    Posts
    198

  4. #4
    Registered User
    Join Date
    Sep 2010
    Posts
    3
    Quote Originally Posted by Salem View Post
    Hey, I can use google too....

    [maths] number spiral matrix - Fanatic Networks

    Perhaps you haven't come across this yet
    How To Ask Questions The Smart Way
    sorry but I do not get you

    Quote Originally Posted by Char*Pntr View Post
    I did. and a couple others with different matrixes and they all seemed to use different approaches, so I thought there should be different ways for different types of such matrixes. and i only have minimal c programming knowledge of syntax which basically only consists of if, else, for , while ,etc without arrays and the rest

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    If a person can solve this, and they can enumerate the steps they took to solve it, then of course, a computer program could be written to solve it.

    This problem isn't a cinch, but it's not OMG hard, either.

    If you want to print it up, without using any data structure (like a matrix), to help out, it adds another layer of difficulty to the problem. Not impossible, but unless you're REALLY into spiral matrices for some reason, not worth the bother.

  6. #6
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,831
    I don't see what the problem is. You can fill a matrix like that by simply traveling around it as the spiral does... I doubt any filling algorithm that does so row-by-row would be any faster.

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I believe the problem is that (if you follow the links), spiral matrices could go in several different ways:

    from the outside inward, top to bottom clockwise
    from the outside inward, top to bottom, counter clockwise
    from the outside inward, bottom to top, clockwise
    ...
    from the inside outward, clockwise, starting at 12 o'clock position
    from the inside outward, clockwise, starting at 3 o'clock position

    etc.

    And if that's the question, then no, I don't believe there is any algorithm to solve for all these different patterns - but there could be one made, of course. Think of a function to find the next square, and you're just passing it more parameters.

    It would help if you were rather obsessive-compulsive to do this, for sure.

  8. #8
    Registered User
    Join Date
    Sep 2010
    Posts
    3
    Quote Originally Posted by Adak View Post
    I believe the problem is that (if you follow the links), spiral matrices could go in several different ways:

    from the outside inward, top to bottom clockwise
    from the outside inward, top to bottom, counter clockwise
    from the outside inward, bottom to top, clockwise
    ...
    from the inside outward, clockwise, starting at 12 o'clock position
    from the inside outward, clockwise, starting at 3 o'clock position

    etc.
    Yes. That was what I meant.

    I don't see what the problem is. You can fill a matrix like that by simply traveling around it as the spiral does... I doubt any filling algorithm that does so row-by-row would be any faster.
    How can I do that if I can only print row by row? (with only loops like if else , for, while)

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Because if you know the dimensions of the array, and you know the type of spiral, and the direction of the spiral, and the starting point of the spiral, and etc., then you can actually figure out all the values to be put in the spiral matrix.

    You have to take a big step "back" from normal element by element type thinking - but it can all be figured out with simple arithmetic, once you get your head wrapped around the equations.

    No, I don't know them, but I can "smell 'em", cooking nearby.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can anyone help?
    By javalurnin in forum C Programming
    Replies: 11
    Last Post: 12-02-2009, 05:02 AM
  2. Prime number program problem
    By Guti14 in forum C Programming
    Replies: 11
    Last Post: 08-06-2004, 04:25 AM
  3. help with a source code..
    By venom424 in forum C++ Programming
    Replies: 8
    Last Post: 05-21-2004, 12:42 PM
  4. parsing a number
    By juancardenas in forum C Programming
    Replies: 1
    Last Post: 02-19-2003, 12:10 PM
  5. Random Number problem in number guessing game...
    By -leech- in forum Windows Programming
    Replies: 8
    Last Post: 01-15-2002, 04:00 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21