views:

125

answers:

2
int numbs[4][4] = 
     1,  2,  3,  4
     5,  6,  7,  8
     9, 10, 11, 12
    13, 14, 15, 16;

When i print it, it should print like this.

1 2 3 4, 8, 12,16, 15, 14, 13, 9, 5, 6, 7, 11, 10, (ie clockwise direction spiral):

---\ //first right, then down, left, up and repeat
/-\|
|-/|
\--/
+3  A: 

Do you have any specific technique that you are supposed to use for this exercise? Otherwise you can just write code that does that:

Console.WriteLine(theArray[0,0]);
Console.WriteLine(theArray[1,0]);
Console.WriteLine(theArray[2,0]);
Console.WriteLine(theArray[3,0]);
Console.WriteLine(theArray[3,1]);
Console.WriteLine(theArray[3,2]);
Console.WriteLine(theArray[3,3]);
Console.WriteLine(theArray[2,3]);
Console.WriteLine(theArray[1,3]);
Console.WriteLine(theArray[0,3]);
Console.WriteLine(theArray[0,2]);
Console.WriteLine(theArray[0,1]);
Console.WriteLine(theArray[1,1]);
Console.WriteLine(theArray[2,1]);
Console.WriteLine(theArray[2,2]);
Console.WriteLine(theArray[1,2]);
Guffa
I wonder why this answer has 6 votes? Obviously the question requires a technique. Anyone could do it with 200 WriteLines!!!
bits
@bits: Perhaps because it's the simplest and most straight forward solution. (Oh, and you don't need 200 lines to print a 4x4 array, you only need 16 lines.)
Guffa
_At least_ do it in a loop over the respective coordinates.
Svante
@Svante: There are plenty of ways to complicate the code, but the purpose was to show the simplest way.
Guffa
The idea must have been NxM array. 4x4 was just a trivial example. Anyone should understand that much.
bits
@bits: You are assuming that it has to be more complicated than what the OP asked for. It might be so, but it might as well be just as simple as it seems. I answered the actual question, you may interpret it as a more complicated question if you like, and answer that.
Guffa
@bits, I think it's a safe assumption that the matrix is square.
Blindy
I just can't believe how can you guys assume that its going to be a square and that too 4x4. What the questioner gave was just a trivial example. Well, anyway the question is closed. No point in arguing here.
bits
@bits: Because the OP explicitly **said so**. You can assume that the OP doesn't mean what he said, but that seems more far fetched than assuming that he does.
Guffa
I am trying to be polite here, but I find the mindset that sees rolling this into a loop as "complication" inappropriate for a programmer.
Svante
@Svante: You failed.
Guffa
+3  A: 

Here's my stab at it:

    static void Spiral(int[,] m)
    {
        int n = m.GetUpperBound(0);
        for (int i = 0; i < n / 2; ++i)
        {
            for (int j = i; j <= n - i; ++j)
                Console.Write(m[i, j] + " ");
            for (int j = i + 1; j <= n - i; ++j)
                Console.Write(m[j, n - i] + " ");
            for (int j = i + 1; j <= n - i; ++j)
                Console.Write(m[n - i, n - j] + " ");
            for (int j = i + 1; j < n - i; ++j)
                Console.Write(m[n - j, i] + " ");
        }

        Console.Write(m[n / 2, n / 2]+" ");
        if (n % 2 == 1)
        {
            Console.Write(m[n / 2, n / 2+1] + " ");
            Console.Write(m[n / 2+1, n / 2+1] + " ");
            Console.Write(m[n / 2+1, n / 2] + " ");
        }
    }

    static void Main(string[] args)
    {
        int[,] myArray = new int[,]{
            {11, 12, 13, 14, 15},
            {21, 22, 23, 24, 25},
            {31, 32, 33, 34, 35},
            {41, 42, 43, 44, 45},
            {51, 52, 53, 54, 55}
        };
        Spiral(myArray);
    }

The output is:

11 12 13 14 15 25 35 45 55 54 53 52 51 41 31 21 22 23 24 34 44 43 42 32 33

edit: Works for both even and odd sized square matrices now.

Blindy
Thanks it works.
Pritam Karmakar