[algogeeks] print spiral

2012-06-15 Thread Ashish Goel
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;jn-c-1;j++) printf(%d ,a[r][j]);
for (int i=r;im-r-1;i++) printf(%d ,a[i][n-c-1]);
for (int j=n-c-1;jc;j--) printf(%d ,a[m-r-1][j]);
for (int i=m-r-1;ir;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 algogeeks@googlegroups.com.
To unsubscribe from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.



Re: [algogeeks] print spiral

2012-06-15 Thread Guneesh Paul Singh
void spiral(int a[][],int m,int n)
{
int c=1;

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



Re: [algogeeks] print spiral

2012-06-15 Thread utsav sharma
check no. of elements printed in all inner for loop also

for (int j=c;jn-c-1  cntm*n;j++) {printf(%d ,a[r][j]); cnt++;}
for (int i=r;im-r-1  cntm*n;i++) {printf(%d ,a[i][n-c-1]); cnt++}
for (int j=n-c-1  cntm*n;jc;j--) {printf(%d ,a[m-r-1][j]); cnt++}
for (int i=m-r-1  cntm*n;ir;i--) {printf(%d ,a[i][c]); cnt++}


On Fri, Jun 15, 2012 at 2:10 PM, Guneesh Paul Singh gunees...@gmail.comwrote:

 void spiral(int a[][],int m,int n)
 {
 int c=1;


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




-- 
Utsav Sharma,
NIT Allahabad

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



Re: [algogeeks] print spiral

2012-06-15 Thread Ashish Goel
yes, using count i solved this, but IMHO, this should be done without this
count somehow..

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


On Fri, Jun 15, 2012 at 3:14 PM, utsav sharma utsav.sharm...@gmail.comwrote:

 check no. of elements printed in all inner for loop also

 for (int j=c;jn-c-1  cntm*n;j++) {printf(%d ,a[r][j]); cnt++;}
 for (int i=r;im-r-1  cntm*n;i++) {printf(%d ,a[i][n-c-1]); cnt++}
 for (int j=n-c-1  cntm*n;jc;j--) {printf(%d ,a[m-r-1][j]); cnt++}
 for (int i=m-r-1  cntm*n;ir;i--) {printf(%d ,a[i][c]); cnt++}


 On Fri, Jun 15, 2012 at 2:10 PM, Guneesh Paul Singh 
 gunees...@gmail.comwrote:

 void spiral(int a[][],int m,int n)
 {
 int c=1;


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




 --
 Utsav Sharma,
 NIT Allahabad

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


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