Traverse matrix in clock-wise manner

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nivedita prasad
    New Member
    • Dec 2009
    • 7

    Traverse matrix in clock-wise manner

    hi
    i want a program for
    Inward-spiral order matrix.
    i.e you have to traverse the matrix in clockwise manner and display the elements,
    for eg.

    1 2 5 4
    8 9 7 1
    9 7 6 3
    2 3 5 8

    output should be 1, 2, 5, 4, 1, 3, 8, 5, 3, 2, 9, 8, 9, 7, 6,7

    Regards
    Nivedita Prasad
  • newb16
    Contributor
    • Jul 2008
    • 687

    #2
    #include <stdio.h>
    struct Q{int S,i,j,*A;Q(int o,int*a):S(o),i (0),j(0),A(a){} ;
    Q&operator++(){ printf("%d, ",A[i*S+j]);int r(i),p(j);
    j>=i-1&&j<S-1-i&&++p;j>=S-i&&j<=i&&--p;j>=S-1-i&&i<j&&++r;j <S-i&&j<i-1&&--r;i=r;j=p;retur n *this;}
    };
    int main(){
    #define size 4
    int stuff[size*size]=
    {1, 2, 5, 4,
    8, 9, 7, 1,
    9, 7, 6, 3,
    2, 3, 5, 8};
    Q q(size,stuff);
    for(int i=0;i<size*size ;i++)++q;
    }

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      This solution a) should not have been provided as complete code solutions are not to be provided and b) the results of this one is indeterminate due to variables being modified more than once within the same sequence point.

      Comment

      • newb16
        Contributor
        • Jul 2008
        • 687

        #4
        && is the sequence point itself. And there is only one '--' or '++' between semicolons.

        Comment

        Working...