Click here to Skip to main content
15,884,911 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I want to print an array in spiral order. By the Printing an Array in Spiral Order, I modified the code as C#.
C#
static void Main(string[] args)
       {
           int[,] a = new int[,]
           {
               {1,2,3,4},
               {5,6,7,8},
               {9,10,11,12},
               {13,14,15,16}
           };
           int size = 16;
           PrintInSpiral(a, size);
           Console.ReadLine();
       }
       public static void PrintInSpiral(int[,] numbers, int size)
       {
           for (int i = size - 1, j = 0; i >= 0; i--, j++)
           {
               for (int k = j; k < i; k++)
                   Console.WriteLine(numbers[j, k] + " ");
               for (int k = j; k < i; k++)
                   Console.WriteLine(numbers[k, i] + " ");
               for (int k = i; k > j; k--)
                   Console.WriteLine(numbers[i, k] + " ");
               for (int k = i; k > j; k--)
                   Console.WriteLine(numbers[k, j] + " ");
           }
       }

However it is incorrect(exception for out of index), would you please help me to check the algorithm and code?
Posted
Comments
Sergey Alexandrovich Kryukov 22-Dec-14 15:04pm    
Would you just use the debugger? From the programming standpoint, the problem is trivial, but it's easy to make a small mathematical/logical mistake.
(By the way, instead of + " ", use string.Format.)
—SA
[no name] 22-Dec-14 15:28pm    
I know the exception as the boundary was broken , but I am not sure what is wrong for the algorithm/logic.
Sergey Alexandrovich Kryukov 22-Dec-14 15:39pm    
The debugger will show you...
—SA

1 solution

 
Share this answer
 
Comments
Maciej Los 22-Dec-14 15:05pm    
Self reference, i like it!
+5!
PIEBALDconsult 22-Dec-14 15:11pm    
This is about the third spiral question I've seen in recent months.
Maciej Los 22-Dec-14 15:14pm    
Yeah, i do remember all of them ;)
[no name] 22-Dec-14 15:19pm    
oh, I don't know it. Would you please provide the links?(For compare purpose)
PIEBALDconsult 22-Dec-14 15:20pm    
Search for "spiral".

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900