Re: please explain output
Which line of the code snippet you have posted is unclear for you?
In few words: function crazy is called recursively until its last paremeter will be zero. And function printf prints out the values of parameters passed into crazy.
Re: please explain output
Quote:
Originally Posted by
mithlesh
Hi,
on running program below - I get this as output
9 5 11 1
3 5 6 2
3 11 9 1
2 5 1 3
5 6 9 1
2 6 3 2
2 9 5 1
Code:
void crazy(int a, int b, int m, int n){
if(n == 0)
return;
crazy(a+m, b, m+b, n-1);
printf("\n %d %d %d %d \n", a, b, m, n );
crazy(a,b+m,m+a,n-1);
}
int main(){
crazy(2,5,1,3);
}
Can anyone explain this for me ? Thanks !!
Sounds like homework. In any case, it's a simply recursive calls crazy in a tree like fashion, "in-order".
Simply tracing your code with a pen and paper would help here:
Code:
+------>crazy(20,5,16,0);
+------>crazy(9,5,11,1);
| +------>crazy(9,16,20,0);
|
+------>crazy(3,5,6,2);
| |
| | +------>crazy(12,11,20,0);
| +------>crazy(3,11,9,1);
| +------>crazy(3,20,12,0);
crazy(2,5,1,3);
|
| +------>crazy(14,6,15,0);
| +------>crazy(5,6,9,1);
| | +------>crazy(5,15,14,0);
| |
+------>crazy(2,6,3,2);
|
| +------>crazy(7,9,14,0);
+------>crazy(2,9,5,1);
+------>crazy(2,14,7,0);