Spiral Access of 2 d array.

Discussion in 'C Programming' started by mail2sandeepnl@gmail.com, Aug 29, 2007.

  1. Guest

    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, 2007
    #1
    1. Advertising

  2. 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[cols];
    }
    for(j = cols; j > 0; j--) {
    output[k++] = array[rows][j];
    }
    for(i = rows; i > 1; i--) {
    output[k++] = array[0];
    }
    }

    Yes, it is rather crude ))
    Ivan Gotovchits, Aug 29, 2007
    #2
    1. Advertising

  3. Ivan Gotovchits wrote:

    > 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[cols];
    }
    for(j = cols; j > 0; j--) {
    output[k++] = array[rows][j];
    }
    for(i = rows; i > 1; i--) {
    output[k++] = array[0];
    }
    }
    Ivan Gotovchits, Aug 29, 2007
    #3
  4. Ivan Gotovchits said:

    > Ivan Gotovchits wrote:
    >
    >> 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
    Richard Heathfield, Aug 29, 2007
    #4
  5. "Ivan Gotovchits" <> schrieb im Newsbeitrag
    news:fb3ls7$b4c$...
    > Ivan Gotovchits wrote:
    >
    >> 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[cols];
    > }
    > for(j = cols; j > 0; j--) {
    > output[k++] = array[rows][j];
    > }
    > for(i = rows; i > 1; i--) {
    > output[k++] = array[0];
    > }
    > }
    >


    Bye, Jojo
    Joachim Schmitz, Aug 29, 2007
    #5
  6. ravi Guest

    On Aug 29, 3:11 pm, ""
    <> 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 ?


    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";
    }
    ravi, Aug 29, 2007
    #6
  7. "" <> writes:

    > 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[j]);
    }
    }
    }

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

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

    spiral(matrix);
    printf("\n");
    return 0;
    }


    --
    Ben.
    Ben Bacarisse, Aug 29, 2007
    #7
  8. Ivan Gotovchits <> writes:
    > Ivan Gotovchits wrote:
    >
    >> 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) <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"
    Keith Thompson, Aug 29, 2007
    #8
  9. ravi <> writes:
    [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) <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"
    Keith Thompson, Aug 29, 2007
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Usman

    SPIRAL PROBLEMS

    Usman, Feb 23, 2004, in forum: C++
    Replies:
    3
    Views:
    670
    Usman
    Feb 24, 2004
  2. Replies:
    2
    Views:
    556
    Victor Bazarov
    May 20, 2006
  3. indian_gaurav

    How to create a spiral matrix?

    indian_gaurav, Sep 2, 2008, in forum: Java
    Replies:
    2
    Views:
    1,200
    Roedy Green
    Sep 6, 2008
  4. Ruby Quiz

    [QUIZ] Number Spiral (#109)

    Ruby Quiz, Jan 12, 2007, in forum: Ruby
    Replies:
    27
    Views:
    296
    William James
    Jan 20, 2007
  5. Ruby Quiz

    [SUMMARY] Number Spiral (#109)

    Ruby Quiz, Jan 18, 2007, in forum: Ruby
    Replies:
    0
    Views:
    115
    Ruby Quiz
    Jan 18, 2007
Loading...

Share This Page