Jump to content

Check out our Community Blogs

Register and join over 40,000 other developers!

Recent Status Updates

View All Updates

- - - - -

Printing array in spiral (Part 2 - going outward from center)

array printing

  • Please log in to reply
No replies to this topic

#1 fkl


    CC Devotee

  • Senior Member
  • PipPipPipPipPipPip
  • 417 posts

Posted 14 July 2011 - 06:36 AM

Problem: Given a 2D array print it in spiral shape (starting from center and going outward)

I originally posted a problem to print an array in spiral here

Basically we start from left top of array, go towards right end, then move down to the end, move left to start of last row and finally come back up to array starting position – 1 and proceed to next internal row on right.

However, I recently saw a variation of this being asked in some interview and thought of giving that a try. The statement goes like,

Starting from number 1 at the center, print in spiral shape by moving outward.

Since in console we generally are not concerned about physical location of elements so this pretty much translates into the above earlier problem with one important exception that we have to start from the center, move outwards and terminate when the entire array is printed.

First, the current problem is only restricted to odd length arrays i.e. those containing a single element in center. Even length ones won’t have a center and approach would be somewhat different.

Secondly, the movement around is not that neat i.e. given the following
1 2 3
4 5 6
7 8 9

We want to print something like 5, 6 (going right) then 9 (going down), followed by 8 and 7 (going left) and finally 4 and 1 (going up). This completes the 4 directions in a single cycle. But as you can see clearly, does not complete printing the array. We still need to print 2 and 3. This means that we need to run the first row’s printing code one extra time.

So the idea works as follows:

We assign our initial coordinates x and y to be the center of the array. Next take a variable “level” which starts with value 1 and it basically refers to distance from center. So we start printing from center and going “level” elements to the right in the first internal loop (see comments in code “go right”)

The next three internal loops are each going in the respective direction. The limits are set in such a way that going in positive direction i.e. forward or down are controlled by “center + level”, whereas going left or upwards are limited by “center - level”.

The terminating condition after the first internal loop (going left to right), basically says that we break from the main loop if we reach top right element. The reason for doing this was that:

On one complete cycle of four directions each, our top most row would be left from printing. As I mentioned above, we need one repetition of left to right loop. But have to terminate as soon as this left to right loop is done.

Since we always start from center going right, so we will always end up (our last element to print will be top right element) as long as the array is of odd length and number of rows and cols are equal.

Code follows

#define ROWS 5
#define COLS 5

int main()
    int arr[ROWS][COLS] = /*{{1,2,3},

                           {{21,22,23,24, 25},
                            {20, 7, 8, 9, 10},
                            {19, 6, 1, 2, 11},
                            {18, 5, 4, 3, 12},
                            {17,16,15,14, 13}};
    int levl;
    int c = COLS/2;
    int x,y;
    x = y = c;

    // Printing inwards to outwards. Works only for odd sized 2D array with one element in center
    for(levl=1; c+levl<=COLS; levl++)
        for(; y<=c+levl && y < COLS; y++) // go right
            cout << arr[x][y] << " ";

        // Since we always start from the center going towards right, top row (going left to right)
        // will always be the last remaining row to print
        if (x == 0 && y == COLS) // we are done

        for(x++,y--; x<=c+levl && x < COLS; x++)  // go down
            cout << arr[x][y] << " ";
        for(x--,y--; y>=c-levl ; y--)    // go left
            cout << arr[x][y] << " ";
        for(x--,y++; x>=c-levl ;x--)     // go up
            cout << arr[x][y] << " ";
    cout << endl;
    return 0;

Here is the output of the run of program on an array of 5X5 whose elements are filled such that spiral printing results in exact sequence.

  • 0
Today is the first day of the rest of my life

Also tagged with one or more of these keywords: array, printing

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download