473,324 Members | 2,246 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,324 software developers and data experts.

Spiral Access of 2 d array.

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
i thought of some iterative method, but not satisfied, is there any
recursive solution for this kind of problem ?

Aug 29 '07 #1
8 2759
ma************@gmail.com wrote:
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
i thought of some iterative method, but not satisfied, is there any
recursive solution for this kind of problem ?
Not sure, and not tested, but try something like this:

void get_frame(int **array, int *output, int rows, int cols);

int
main(void)
{
int array[X][Y];
int result[X+Y];
int x = X, y = Y;

while(x && y) {
get_frame(array, result, x, y);
result += (2*x + 2*y);
x--;
y--;
}
}

void
get_frame(int **array, int *output, int rows, int cols)
{
int i,j,k = 0;

for(j = 0; j < cols; j++) {
output[k++] = array[0][j];
}
for(i = 0; i < rows; i++) {
output[k++] = array[i][cols];
}
for(j = cols; j 0; j--) {
output[k++] = array[rows][j];
}
for(i = rows; i 1; i--) {
output[k++] = array[0][i];
}
}

Yes, it is rather crude ))
Aug 29 '07 #2
Ivan Gotovchits wrote:
ma************@gmail.com wrote:
<snip>
Yes, it is rather crude ))
This varian is more correct ...
#define X = 10
#define Y = 10
void get_frame(int **array, int *output, int rows, int cols);

int
main(void)
{
int array[X][Y];
int result[X+Y];
int x = X, y = Y;
int i = 0, j = 0;

while(x && y) {
array = &array[i++][j++];
get_frame(array, result, x, y);
result += (2*x + 2*y);
x--;
y--;
}
}

void
get_frame(int **array, int *output, int rows, int cols)
{
int i,j,k = 0;

for(j = 0; j < cols; j++) {
output[k++] = array[0][j];
}
for(i = 0; i < rows; i++) {
output[k++] = array[i][cols];
}
for(j = cols; j 0; j--) {
output[k++] = array[rows][j];
}
for(i = rows; i 1; i--) {
output[k++] = array[0][i];
}
}

Aug 29 '07 #3
Ivan Gotovchits said:
Ivan Gotovchits wrote:
>ma************@gmail.com wrote:
<snip>
>Yes, it is rather crude ))
This varian is more correct ...
Yet it is not correct. The compiler must issue a diagnostic message for
the code you supplied. For example, my own compiler of choice issues
the following output when presented with your code:

foo.c: In function `main':
foo.c:8: parse error before `='
foo.c:9: parse error before `='
foo.c:10: parse error before `='
foo.c:13: `y' undeclared (first use in this function)
foo.c:13: (Each undeclared identifier is reported only once
foo.c:13: for each function it appears in.)
foo.c:14: `array' undeclared (first use in this function)
foo.c:15: `result' undeclared (first use in this function)
foo.c:20: warning: control reaches end of non-void function
make: *** [foo.o] Error 1

<snip>

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
Aug 29 '07 #4
"Ivan Gotovchits" <iv*************@auriga.ruschrieb im Newsbeitrag
news:fb**********@aioe.org...
Ivan Gotovchits wrote:
>ma************@gmail.com wrote:
<snip>
>Yes, it is rather crude ))
This varian is more correct ...
#define X = 10
#define Y = 10
#define X 10
#define Y 10
void get_frame(int **array, int *output, int rows, int cols);

int
main(void)
{
int array[X][Y];
int result[X+Y];
int x = X, y = Y;
int i = 0, j = 0;

while(x && y) {
array = &array[i++][j++];
get_frame(array, result, x, y);
result += (2*x + 2*y);
x--;
y--;
}
return 0;
}

void
get_frame(int **array, int *output, int rows, int cols)
{
int i,j,k = 0;

for(j = 0; j < cols; j++) {
output[k++] = array[0][j];
}
for(i = 0; i < rows; i++) {
output[k++] = array[i][cols];
}
for(j = cols; j 0; j--) {
output[k++] = array[rows][j];
}
for(i = rows; i 1; i--) {
output[k++] = array[0][i];
}
}
Bye, Jojo
Aug 29 '07 #5
On Aug 29, 3:11 pm, "mail2sandee...@gmail.com"
<mail2sandee...@gmail.comwrote:
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
i thought of some iterative method, but not satisfied, is there any
recursive solution for this kind of problem ?
int Spiral(int n,int x,int y,int i)
{
while(1)
-----{
----------if(x==0)
----------{
---------------return (i+y);
----------}
----------else if(y==n-1)
----------{
---------------return (i+n+x);
----------}
----------else if(x==n-1)
----------{
---------------return (i+ 3(n-1)-y);
----------}
----------else if(y==n-1)
----------{
---------------return(i+ 4(n-1) -x);
----------}
----------else
----------{
--------------- x=x-1;
--------------- y=y-1;
--------------- i=i+ 4(n-1);
--------------- n=n-2;
----------}
-----}
}

//Now Call the function for each x,y;

int s=1;

//The No. from which you want your spiral to begin;
eg. for s=5,n=3 the spiral will be

5 6 7
12 13 8
11 10 9

for(int i=0;i<n;i++)
{
-----for(int j=0;j<n;j++)
-----{
----------cout<<Spiral(n,i,j,s)<<" ";
-----}
-----cout<<"\n";
}

Aug 29 '07 #6
"ma************@gmail.com" <ma************@gmail.comwrites:
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
i thought of some iterative method, but not satisfied, is there any
recursive solution for this kind of problem ?
In C, recursive functions on 2D arrays are not common. Since C can't
slice an array in anything but very simple ways, recursion is less
useful than with other parameter types. I can image (but these days
could not code!) nice recursive solutions in Algol 68.

This may be homework, but the following unlikely to score many marks
but it probably is how I'd do it. With all the right comments, it is
probably not a bad solution.

#include <stdio.h>

#define N 5
#define M 3

int min(int a, int b)
{
return a < b ? a : b;
}

void spiral(int mat[M][N])
{
int n = N, d = +1, i = 0, j = -1;
int s, segl = N + M - 1;
for (s = 0; s < min(N, M); s++, d = -d, segl -= 2, n--) {
int c, *ip = &j;
for (c = 0; c < segl; c++) {
if (c == n)
ip = &i;
*ip += d;
printf("%3d", mat[i][j]);
}
}
}

int main(void)
{
int matrix[M][N];
int i, j;
for (i = 0; i < M; i++)
for (j = 0; j < N; j++)
matrix[i][j] = i * N + j + 1;

for (i = 0; i < M; i++) {
for (j = 0; j < N; j++)
printf("%3d", matrix[i][j]);
printf("\n");
}

spiral(matrix);
printf("\n");
return 0;
}
--
Ben.
Aug 29 '07 #7
Ivan Gotovchits <iv*************@auriga.ruwrites:
Ivan Gotovchits wrote:
>ma************@gmail.com wrote:
<snip>
>Yes, it is rather crude ))
This varian is more correct ...
#define X = 10
#define Y = 10
Dropping the '=' characters will fix *some* of your problems.
void get_frame(int **array, int *output, int rows, int cols);

int
main(void)
{
int array[X][Y];
int result[X+Y];
int x = X, y = Y;
int i = 0, j = 0;

while(x && y) {
array = &array[i++][j++];
You can't assign to an array.
get_frame(array, result, x, y);
The first parameter to get_frame() is of type int**. You're passing
it an argument of type int (*)[10].
result += (2*x + 2*y);
Again, you can't assign to an array.

[snip]

Please don't post code, particularly a sizable program like this,
unless you've compiled it yourself.

If your errors were the result of fundamental misunderstandings, I
suggest reading section 6 of the comp.lang.c FAQ,
<http://www.c-faq.com/>.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Aug 29 '07 #8
ravi <dc**********@gmail.comwrites:
[snip]
for(int i=0;i<n;i++)
{
-----for(int j=0;j<n;j++)
-----{
----------cout<<Spiral(n,i,j,s)<<" ";
-----}
-----cout<<"\n";
}
What is the purpose of the rows of hyphens on each line, other than to
make the code impossible to compile?

Dropping the '-'s, this code is C++, not C.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Aug 29 '07 #9

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

Similar topics

2
by: Usman | last post by:
Hello Everyone. Can some one help me on this question , I studied a lot but still am unable to do this I need to print the following pattern using recursion in C++ , the end term is ' n '...
8
by: mytfein | last post by:
Hi Everyone, Background: Another department intends to ftp a .txt file from the mainframe, for me to process. The objective is to write a vb script that would be scheduled to run daily to...
2
by: sid1 | last post by:
hi i have devised the source code for generating spiral matrix in C++..its compiling properly but i run it it is giving some segmentation fault plz help me how shud i remove this error. thanx
2
by: MBeckford05 | last post by:
Hello There ! I would like help, in writing a Java program to output an Archimedean Spiral to the screen. I have read on the net what a Archiemedian Spiral is. But I'm not too familiar with...
3
by: MBeckford05 | last post by:
Hi Everyone, I am placed with a problem. How to write a program in Java to produce the Archimeadean Spiral. The progam should look like an Archimedean spiral on the screen. Any help in hoe to...
4
by: sumuka | last post by:
Hello , Im doing a java project in java 1.5 version and i need to draw the spiral . The problem is the user has to draw a box or rectangle of any size on an image and the central point of that box...
1
by: sumuka | last post by:
Hello, Can Anybody tell me how to draw a simple spiral in java ? Thanks in anticipation,
1
by: sumuka | last post by:
Hello, I'm doing a java project in which I have a rectangle which is drawn by the user by dragging the mouse and hence the size of the rectangle can vary for each execution. Now i need to draw a...
4
Thekid
by: Thekid | last post by:
Hi, I'm not very good with javascript but I wanted to know if there was a simple way to change this code so instead of printing out the numbers & letters in a spiral on the page, it can be changed to...
0
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...
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: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
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: 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
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
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.