ok I CANT understand the logic of this recursion
the function is this:
i think the output is obvious for n!=0, i tried to see the output for n=0Code:void f(int A[], int n, int B[]){ if (!n) f(A,n-1,B); if (A[n-1]!=B[n-1]) A[n-1]=A[n-1]+B[n-1]; }
the output here isCode:#include <stdio.h> void f(int A[], int n, int B[]){ if (!n) f(A,n-1,B); if (A[n-1]!=B[n-1]) A[n-1]=A[n-1]+B[n-1]; } int main(void){ int x[3] = {1,2,3}; int y[3] = {2,3,4}; int i; f(x,0,y); for (i=0;i<3;i++){ printf("%d ",x[i]); } printf("\n"); for (i=0;i<3;i++){ printf("%d ",y[i]); } getchar(); return 0; }
I CANT understand WHY, what's the logic behind this??????????Code:1 2 3 2 3 3
but then if I change the order of x,y declaration:
the output is:Code:#include <stdio.h> void f(int A[], int n, int B[]){ if (!n) f(A,n-1,B); if (A[n-1]!=B[n-1]) A[n-1]=A[n-1]+B[n-1]; } int main(void){ int y[3] = {2,3,4}; int x[3] = {1,2,3}; // notice how i put x after y int i; f(x,0,y); for (i=0;i<3;i++){ printf("%d ",x[i]); } printf("\n"); for (i=0;i<3;i++){ printf("%d ",y[i]); } getchar(); return 0; }
what's going on!!!!!!!!!???????????Code:1 2 3 2 3 4



LinkBack URL
About LinkBacks




