-
July 9th, 2014, 08:26 AM
#1
please explain output
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 !!
-
July 9th, 2014, 09:42 AM
#2
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.
Victor Nijegorodov
-
July 18th, 2014, 08:15 AM
#3
Re: please explain output
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);
Is your question related to IO?
Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|