Thread: Printing 2-dimensional array spirally

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    175

    Printing 2-dimensional array spirally

    Hello,

    Following code prints the 2-dimensional array spirally. Can anybody have better version or other ideas? Especially in highlighted areas.

    Currently botton right element prints twice....

    Code:
    # include<stdio.h>
    
    void printElement(int i,int j,int maxCol,int maxRow);
    
    
    int a[4][4] =  {
    				{1,2,3,11},
    				{4,5,6,12},
    				{7,8,9,13},
    				{14,15,16,17},
    				};
    
    void main()
    {
    	int maxCol = sizeof(a[0])/sizeof(a[0][0])-1;
    	int maxRow = sizeof(a)/sizeof(a[0])-1;
    	int i = 0;
    	int j = 0;
    
    	while ( (i < maxRow) && (j < maxCol) )
    	{
    		printElement(i,j,maxCol,maxRow);
    		i++;j++;
    		maxCol--;
    		maxRow--;
    	}
    	if ( (i == maxRow) && (j == maxCol) )
    		printf("%d\t",a[j][i]);
    }
    
    
    
    void printElement(int i,int j,int maxCol,int maxRow)
    {
    	printf("%d\t",a[i][j]);
    
    	if ( j < maxCol )
    	{
    		j++;
    		printElement(i,j,maxCol,maxRow);
    		printf("%d\t",a[j][i]);
    		return;
    	}
    	if ( i < maxRow )
    	{
    		i++;
    //		if ( i == maxRow ) 
    //	                       return;
    		printElement(i,j,maxCol,maxRow);
    		printf("%d\t",a[j][i]);
    		return;
    	}
    	else
    		return;
    }

  2. #2
    ---
    Join Date
    May 2004
    Posts
    1,379
    im not sure what you mean by 'spirally'
    but i compiled this program and i dont really see any pattern at all
    please explain the output your trying to get

  3. #3
    Registered User
    Join Date
    Feb 2003
    Posts
    175
    With above program, it should print

    1,2,3,11,12,13,17,16,15,14,7,4,5,6,9,8

    Let me know, if you can provide better solution....

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Here's a fun one:
    Code:
    #include <stdio.h>
    #define YSIZE 4
    #define XSIZE 4
    int main( void )
    {
            int yadder = 0, ycoord = 0, yhsize = YSIZE-1, ylsize = 1,
                xadder = 1, xcoord = 0, xhsize = XSIZE-1, xlsize = 0,
                printed = 0;
            int array[YSIZE][XSIZE] =
            {
                    {  1,  2,  3,  4 },
                    { 12, 13, 14,  5 },
                    { 11, 16, 15,  6 },
                    { 10,  9,  8,  7 },
            };
    
            for( printed = 0; printed < YSIZE * XSIZE; printed++, xcoord += xadder, ycoord += yadder )
            {
                    printf("%d ", array[ycoord][xcoord] );
                    if( xadder > 0 && xcoord >= xhsize )
                    {
                            xhsize--;
                            xadder = 0;
                            yadder = 1;
                    }
                    else
                    if( xadder < 0 && xcoord <= xlsize )
                    {
                            xlsize++;
                            xadder = 0;
                            yadder = -1;
                    }
                    else
                    if( yadder > 0 && ycoord >= yhsize )
                    {
                            yhsize--;
                            yadder = 0;
                            xadder = -1;
                    }
                    else
                    if( yadder < 0 && ycoord <= ylsize )
                    {
                            ylsize++;
                            yadder = 0;
                            xadder = 1;
                    }
            }
            return 0;
    }
    I'll let you decide if it's a "better solution".

    [edit]
    Moved initialization from for for the sake of forum width, and other minor width-friendly editing to variable initializations.
    [/edit]

    Quzah.
    Last edited by quzah; 09-22-2004 at 11:51 PM.
    Hope is the first step on the road to disappointment.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,662
    > void main()
    I'm sure you said this wasn't going to happen again in some previous post....
    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.

  6. #6
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    It was in this post where he said he wouldn't use it anymore.
    http://cboard.cprogramming.com/showthread.php?t=56950
    Help populate a c/c++ help irc channel
    server: irc://irc.efnet.net
    channel: #c

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  2. two dimensional array
    By leisiminger in forum C Programming
    Replies: 12
    Last Post: 03-09-2008, 11:53 PM
  3. Merge sort please
    By vasanth in forum C Programming
    Replies: 2
    Last Post: 11-09-2003, 12:09 PM
  4. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM