473,326 Members | 2,168 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Traversing an array spirally

Hi,

I would like to know how to traverse an array spirally?
i.e if I have an array as follows

1 2 3 4
5 6 7 8
9 10 11 12

I want the output to be 1 2 3 4 8 12 11 10 9 5 6 7.

Please help
Feb 2 '08 #1
4 2516

"Praveen" <rp**********@gmail.comwrote in message
news:24**********************************@k39g2000 hsf.googlegroups.com...
Hi,

I would like to know how to traverse an array spirally?
i.e if I have an array as follows

1 2 3 4
5 6 7 8
9 10 11 12

I want the output to be 1 2 3 4 8 12 11 10 9 5 6 7.
Is this homework?

Feb 2 '08 #2

"Praveen" <rp**********@gmail.comwrote in message
news:24**********************************@k39g2000 hsf.googlegroups.com...
Hi,

I would like to know how to traverse an array spirally?
i.e if I have an array as follows

1 2 3 4
5 6 7 8
9 10 11 12

I want the output to be 1 2 3 4 8 12 11 10 9 5 6 7.

Please help
Interesting exercise. But not really specific to C except array bounds have
to start at 0.

This is my effort. Probably some clever logic needed but I found it easier
to use a map of 1s and 0s to indicate which array elements have been
visited.

Ints m and n have the array dims hardcoded. Dynamic bounds are a little more
involved.

BTW I'm new to C so if this is homework... don't rely on this!

Bart
#include <stdio.h>
#include <stdlib.h>

int main(void)
{
int m=8,n=6;
int x[n][m];
int map[n][m];
int i,j,k,c,l,count,total,dir;

k=1;
c=1;
l=1;

/* print array grid */
while (1)
{ printf("%3d",k);
++k;
++c;
if (c>m)
{
printf("\n");
c = 1;
++l;
if (l>n) break;
}
}

printf("\n");

/* Set up map of 1's for each array element */
k = 1;
for (j=0; j<n; ++j)
for (i=0; i<m; ++i)
{
x[j][i] = k++; /* the grid */
map[j][i] = 1; /* the map */
};
/* now search clockwise spiral */
i = 0;
j = 0;
dir = 'R'; /* One of 4 directions R D L U */
total = m*n;
count = 0;

while (1)
{/*w1"*/

printf(" %d",x[j][i]); /* output contents of this grid element */
map[j][i] = 0; /* mark as visited */
++count;
if (count==total) break;

while (1)
{/*w2*/
switch (dir)
{/*sw*/
case 'R':
if (i<(m-1) && map[j][i+1])
{++i;
goto endwhile; /* C can't use break out of 2 levels :-) */
}
else
dir = 'D';
break;
case 'D':
if (j<(n-1) && map[j+1][i])
{++j;
goto endwhile;
}
else
dir = 'L';
break;
case 'L':
if (i>0 && map[j][i-1])
{--i;
goto endwhile;
}
else
dir = 'U';
break;
case 'U':
if (j>0 && map[j-1][i])
{--j;
goto endwhile;
}
else
dir = 'R';
break;
}; /*sw*/
}; /*w2*/
endwhile:;

}; /*w1*/

printf("\n");
}


Feb 2 '08 #3
Praveen wrote:
Hi,

I would like to know how to traverse an array spirally?
i.e if I have an array as follows

1 2 3 4
5 6 7 8
9 10 11 12

I want the output to be 1 2 3 4 8 12 11 10 9 5 6 7.

Please help
In addition to the other responses I also recommend that you do a search
of Google Groups' archive of comp.lang.c. This is a fairly regular
homework problem and there have been many good answers given over the
years.

Feb 2 '08 #4
On Sat, 2 Feb 2008 10:07:27 -0600, santosh wrote
(in article <fo**********@registered.motzarella.org>):
Praveen wrote:
>Hi,

I would like to know how to traverse an array spirally?
i.e if I have an array as follows

1 2 3 4
5 6 7 8
9 10 11 12

I want the output to be 1 2 3 4 8 12 11 10 9 5 6 7.

Please help

In addition to the other responses I also recommend that you do a search
of Google Groups' archive of comp.lang.c. This is a fairly regular
homework problem and there have been many good answers given over the
years.
Yes, by all means, help make sure that he doesn't accidentally learn
anything during this or any other course. We need more college grads
that don't understand the material on their diploma.

--
Randy Howard (2reply remove FOOBAR)
"The power of accurate observation is called cynicism by those
who have not got it." - George Bernard Shaw

Feb 2 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Ken Fine | last post by:
I would like some guidance regarding a "content scanner" I'm trying to build. This ASP widget will automatically scan remote web sites for certain kinds of content using a screen scraping component...
7
by: Mad Scientist Jr | last post by:
I have been doing this kind of thing in VB and VB.net for a long time, but am having problems in C#: First I want to split up a string by the backslash character, but this line: arrSplit =...
7
by: wallacej | last post by:
Hi I have a 2D array : imageTileArray and I would like to traverse it i.e. visit every entry in the array sequentially. I've had a go at doing it myself as follows but i get address violations:...
4
by: plmanikandan | last post by:
Hi, I am new to link list programming.I need to traverse from the end of link list.Is there any way to find the end of link list without traversing from start(i.e traversing from first to find the...
4
by: Fred!head | last post by:
Hi, Probably this is a newbie question so I appreciate you bearing with me. I've got an application where users can create forms with name= values they define. I'd like to write a script that...
8
by: mail2sandeepnl | last post by:
Hi how to spirally access 2 d array, ex: for input array 1 2 3 4 5 6 7 8 9 10 11 12 output should be like : 1,2,3,4,8,12,11,10,9,5,6,7
30
by: asit | last post by:
We kno that data can be pushed onto the stack or popped 4m it. Can stack be traversed ??
4
by: matth | last post by:
I've been working on something that deals with handling a user's selection within the DOM and I'm tripping up on one last, but crucial, detail. Forgive me for the length of the code, but my...
1
by: somcool | last post by:
I am facing an error while traversing a query in MS Access Details - When I click a button, a form which has the query opens up. There are certain fields which are in the form of combo box in the...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.