Need help in C++

amir19

New member
Need help in C++

Hello guys, I need your help. I have the following Matrix: 6 5 2 1 7 8 3 4 10 9 14 13 11 12 15 16 I need to write a recursive function to print it from the lowest number to the highest. Here is my code: void snail(int a[n1][n1], int i, int j,int index, int index_i, int index_j) { if ((i==0) && (j==0)) cout<<a[j]<<" "<<a[j+1]<<" "<<a[i+1][j+1]<<" "<<a[i+1][j]<<" "; else { if ((index % 2 !=0 ) && (index_j % 2 !=0)) snail(a, i, j+2, index+1,index_i,index_j+1); else if ((index % 2 !=0 ) && (index_j % 2 ==0)) snail(a, i, j-2, index+1,index_i,index_j+1); else if ((index % 2 == 0) && (index_i % 2 != 0)) snail(a, i-2, j, index+1,index_i++,index_j); else if ((index % 2 == 0) && (index_i % 2 == 0)) { snail(a, i-2, j, index++,index_i+1,index_j); } } if ((i!=0) && (j!=0)) cout<<a[j]<<" "<<a[j+1]<<" "<<a[i+1][j+1]<<" "<<a[i+1][j]<<" "; } void main() { int index=1, index1=1, index2=1, i=n-1, j=0; int Matrix1[n1][n1] = {{1,2,5,6},{4,3,8,7},{13,14,9,10},{16,15,12,11}}; for (i=0;i<n1;i++) { for (j=0;j<n1;j++) cout<<Matrix[j]<<"\t "; cout<<endl; } snail(Matrix1, n1-2, 0,index, index1, index2); } Any great solution would be more than welcome!!
 

neko

New member
כתוב את ההודעה מחדש, הפעם שים

את הסימן [הקוד] לפני הקוד ו[סקוד] אחריו (החלף את ה][ [] בקוים | שנמצאים ליד הBACKSPACE). ככה הקוד יצא קריא ומוצמד לשמאל.
 
למעלה