can anybody tell me how this code works
-
int compute(int n) { if(n>0) { n=compute(n-3)+compute(n-1) return n; } return 1; } void main() { x=compute(5); cout<<x; } o/p:9 can anybody explain in detail the flow
-
int compute(int n) { if(n>0) { n=compute(n-3)+compute(n-1) return n; } return 1; } void main() { x=compute(5); cout<<x; } o/p:9 can anybody explain in detail the flow
I can tell you why it doesn't work: it goes into infinite recursion. I was wrong. :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarkemodified on Friday, April 18, 2008 10:19 AM
-
int compute(int n) { if(n>0) { n=compute(n-3)+compute(n-1) return n; } return 1; } void main() { x=compute(5); cout<<x; } o/p:9 can anybody explain in detail the flow
philiptabraham wrote:
can anybody explain in detail the flow
If it works, wikipedia can explain how it works and in great detail. Read on Recursive Function[^]
Nobody can give you wiser advice than yourself. - Cicero .·´¯`·->Rajesh<-·´¯`·. Codeproject.com: Visual C++ MVP
-
philiptabraham wrote:
can anybody explain in detail the flow
If it works, wikipedia can explain how it works and in great detail. Read on Recursive Function[^]
Nobody can give you wiser advice than yourself. - Cicero .·´¯`·->Rajesh<-·´¯`·. Codeproject.com: Visual C++ MVP
Rajesh R Subramanian wrote:
If it works
Good point, indeed. :-D
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke -
int compute(int n) { if(n>0) { n=compute(n-3)+compute(n-1) return n; } return 1; } void main() { x=compute(5); cout<<x; } o/p:9 can anybody explain in detail the flow
philiptabraham wrote:
o/p:9 can anybody explain in detail the flow
1/ Fire up visual studio. 2/ Compile your program. 3/ set a breakpoint in main. 4/ Assuming VS6, press f11 to step into compute. put n in your watch window. 5/ press f11 a lot, until the program is finished. 6/ Enjoy your new shiny enlightenment. Fight evil monks on bleak mountains. Iain.
Plz sir... CPallini CPallini abuz drugz, plz plz help urgent.
-
philiptabraham wrote:
o/p:9 can anybody explain in detail the flow
1/ Fire up visual studio. 2/ Compile your program. 3/ set a breakpoint in main. 4/ Assuming VS6, press f11 to step into compute. put n in your watch window. 5/ press f11 a lot, until the program is finished. 6/ Enjoy your new shiny enlightenment. Fight evil monks on bleak mountains. Iain.
Plz sir... CPallini CPallini abuz drugz, plz plz help urgent.
Iain Clarke wrote:
5/ press f11 a lot, until the program is finished.
press f11 a lot and a lot and a lot... Since the program experiences infinite recursion. :-D I was wrong. :-O
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarkemodified on Friday, April 18, 2008 10:21 AM
-
Iain Clarke wrote:
5/ press f11 a lot, until the program is finished.
press f11 a lot and a lot and a lot... Since the program experiences infinite recursion. :-D I was wrong. :-O
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarkemodified on Friday, April 18, 2008 10:21 AM
Well, no one said the road to enlightenment would be easy! Maybe step 4.5 should have been "make yourself comfortable, with a barrel of castlemaine 6X." Iain.
Plz sir... CPallini CPallini abuz drugz, plz plz help urgent.
-
Well, no one said the road to enlightenment would be easy! Maybe step 4.5 should have been "make yourself comfortable, with a barrel of castlemaine 6X." Iain.
Plz sir... CPallini CPallini abuz drugz, plz plz help urgent.
Alcoholic! :-D
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke -
Rajesh R Subramanian wrote:
If it works
Good point, indeed. :-D
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain ClarkeNah! I said that because the OP seems to be *wondering* how it works. That code should work, BTW [insert very much unsure smiley here]. I don't have a compiler here though.
Nobody can give you wiser advice than yourself. - Cicero .·´¯`·->Rajesh<-·´¯`·. Codeproject.com: Visual C++ MVP
-
int compute(int n) { if(n>0) { n=compute(n-3)+compute(n-1) return n; } return 1; } void main() { x=compute(5); cout<<x; } o/p:9 can anybody explain in detail the flow
It won't until you fix at least two syntax errors. :rolleyes:
"Love people and use things, not love things and use people." - Unknown
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
-
I can tell you why it doesn't work: it goes into infinite recursion. I was wrong. :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarkemodified on Friday, April 18, 2008 10:19 AM
CPallini wrote:
it goes into infinite recursion.
While I don't know exactly what it computes, it does indeed terminate.
"Love people and use things, not love things and use people." - Unknown
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
-
philiptabraham wrote:
o/p:9 can anybody explain in detail the flow
1/ Fire up visual studio. 2/ Compile your program. 3/ set a breakpoint in main. 4/ Assuming VS6, press f11 to step into compute. put n in your watch window. 5/ press f11 a lot, until the program is finished. 6/ Enjoy your new shiny enlightenment. Fight evil monks on bleak mountains. Iain.
Plz sir... CPallini CPallini abuz drugz, plz plz help urgent.
that the right way to debug a program :)
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
Never mind - my own stupidity is the source of every "problem" - Mixturecheers, Alok Gupta VC Forum Q&A :- I/IV Support CRY- Child Relief and You/codeProject$$>
-
I can tell you why it doesn't work: it goes into infinite recursion. I was wrong. :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarkemodified on Friday, April 18, 2008 10:19 AM
it working fine for me.. it not going in infinite recursion!
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
Never mind - my own stupidity is the source of every "problem" - Mixturecheers, Alok Gupta VC Forum Q&A :- I/IV Support CRY- Child Relief and You/codeProject$$>
-
It won't until you fix at least two syntax errors. :rolleyes:
"Love people and use things, not love things and use people." - Unknown
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
:-D plus inclusion of header file :)
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
Never mind - my own stupidity is the source of every "problem" - Mixturecheers, Alok Gupta VC Forum Q&A :- I/IV Support CRY- Child Relief and You/codeProject$$>
-
CPallini wrote:
it goes into infinite recursion.
While I don't know exactly what it computes, it does indeed terminate.
"Love people and use things, not love things and use people." - Unknown
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
Yes, it is true, I was wrong. :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke -
it working fine for me.. it not going in infinite recursion!
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
Never mind - my own stupidity is the source of every "problem" - Mixturecheers, Alok Gupta VC Forum Q&A :- I/IV Support CRY- Child Relief and You/codeProject$$>
http://www.codeproject.com/script/Forums/View.aspx?fid=1647&msg=2513484[^] :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke -
Yes, it is true, I was wrong. :)
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarkesorry i am late :)
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
Never mind - my own stupidity is the source of every "problem" - Mixturecheers, Alok Gupta VC Forum Q&A :- I/IV Support CRY- Child Relief and You/codeProject$$>
-
:-D plus inclusion of header file :)
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
Never mind - my own stupidity is the source of every "problem" - Mixturecheers, Alok Gupta VC Forum Q&A :- I/IV Support CRY- Child Relief and You/codeProject$$>
Ok!
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
Never mind - my own stupidity is the source of every "problem" - Mixturecheers, Alok Gupta VC Forum Q&A :- I/IV Support CRY- Child Relief and You
-
Nah! I said that because the OP seems to be *wondering* how it works. That code should work, BTW [insert very much unsure smiley here]. I don't have a compiler here though.
Nobody can give you wiser advice than yourself. - Cicero .·´¯`·->Rajesh<-·´¯`·. Codeproject.com: Visual C++ MVP
Rajesh R Subramanian wrote:
I don't have a compiler here though.
If don't have .. invent one... :-)
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
Never mind - my own stupidity is the source of every "problem" - Mixturecheers, Alok Gupta VC Forum Q&A :- I/IV Support CRY- Child Relief and You