given a matrix m*n print elements in spiral

eg

1 2
3 4
5 6  => 1,2,4,6,5,3


1
2
3 =>1,2,3

i wrote following code but for the case 2 printing 1,2,3,2
I wish to avoid keeping a counter of how many elements have been print so
far...


void spiral(int a[], int m, int n) {
  while ( (rs<(m+1)/2) &&  (cs <(n+1)/2)) {
    int r=rs;
    int c=cs;
    for (int j=c;j<n-c-1;j++) printf("%d ",a[r][j]);
    for (int i=r;i<m-r-1;i++) printf("%d ",a[i][n-c-1]);
    for (int j=n-c-1;j>c;j--) printf("%d ",a[m-r-1][j]);
    for (int i=m-r-1;i>r;i--) printf("%d ",a[i][c]);;
    rs++;cs++;
  }
}


Best Regards
Ashish Goel
"Think positive and find fuel in failure"
+919985813081
+919966006652

-- 
You received this message because you are subscribed to the Google Groups 
"Algorithm Geeks" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.

Reply via email to