Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. can anybody tell me how this code works

can anybody tell me how this code works

Scheduled Pinned Locked Moved C / C++ / MFC
19 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    philiptabraham
    wrote on last edited by
    #1

    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

    C R I D 4 Replies Last reply
    0
    • P philiptabraham

      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

      C Offline
      C Offline
      CPallini
      wrote on last edited by
      #2

      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 Clarke

      modified on Friday, April 18, 2008 10:19 AM

      D T 2 Replies Last reply
      0
      • P philiptabraham

        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

        R Offline
        R Offline
        Rajesh R Subramanian
        wrote on last edited by
        #3

        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

        C 1 Reply Last reply
        0
        • R Rajesh R Subramanian

          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

          C Offline
          C Offline
          CPallini
          wrote on last edited by
          #4

          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

          R 1 Reply Last reply
          0
          • P philiptabraham

            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 Offline
            I Offline
            Iain Clarke Warrior Programmer
            wrote on last edited by
            #5

            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.

            C T 2 Replies Last reply
            0
            • I Iain Clarke Warrior Programmer

              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.

              C Offline
              C Offline
              CPallini
              wrote on last edited by
              #6

              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 Clarke

              modified on Friday, April 18, 2008 10:21 AM

              I 1 Reply Last reply
              0
              • C CPallini

                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 Clarke

                modified on Friday, April 18, 2008 10:21 AM

                I Offline
                I Offline
                Iain Clarke Warrior Programmer
                wrote on last edited by
                #7

                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.

                C 1 Reply Last reply
                0
                • I Iain Clarke Warrior Programmer

                  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.

                  C Offline
                  C Offline
                  CPallini
                  wrote on last edited by
                  #8

                  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

                  1 Reply Last reply
                  0
                  • C CPallini

                    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

                    R Offline
                    R Offline
                    Rajesh R Subramanian
                    wrote on last edited by
                    #9

                    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

                    T 1 Reply Last reply
                    0
                    • P philiptabraham

                      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

                      D Offline
                      D Offline
                      David Crow
                      wrote on last edited by
                      #10

                      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

                      T 1 Reply Last reply
                      0
                      • C CPallini

                        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 Clarke

                        modified on Friday, April 18, 2008 10:19 AM

                        D Offline
                        D Offline
                        David Crow
                        wrote on last edited by
                        #11

                        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

                        C 1 Reply Last reply
                        0
                        • C CPallini

                          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 Clarke

                          modified on Friday, April 18, 2008 10:19 AM

                          T Offline
                          T Offline
                          ThatsAlok
                          wrote on last edited by
                          #12

                          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" - Mixture

                          cheers, Alok Gupta VC Forum Q&A :- I/IV Support CRY- Child Relief and You/codeProject$$>

                          C 1 Reply Last reply
                          0
                          • I Iain Clarke Warrior Programmer

                            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.

                            T Offline
                            T Offline
                            ThatsAlok
                            wrote on last edited by
                            #13

                            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" - Mixture

                            cheers, Alok Gupta VC Forum Q&A :- I/IV Support CRY- Child Relief and You/codeProject$$>

                            1 Reply Last reply
                            0
                            • D David Crow

                              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

                              T Offline
                              T Offline
                              ThatsAlok
                              wrote on last edited by
                              #14

                              :-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" - Mixture

                              cheers, Alok Gupta VC Forum Q&A :- I/IV Support CRY- Child Relief and You/codeProject$$>

                              T 1 Reply Last reply
                              0
                              • D David Crow

                                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

                                C Offline
                                C Offline
                                CPallini
                                wrote on last edited by
                                #15

                                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

                                T 1 Reply Last reply
                                0
                                • T ThatsAlok

                                  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" - Mixture

                                  cheers, Alok Gupta VC Forum Q&A :- I/IV Support CRY- Child Relief and You/codeProject$$>

                                  C Offline
                                  C Offline
                                  CPallini
                                  wrote on last edited by
                                  #16

                                  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

                                  1 Reply Last reply
                                  0
                                  • C CPallini

                                    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

                                    T Offline
                                    T Offline
                                    ThatsAlok
                                    wrote on last edited by
                                    #17

                                    sorry 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" - Mixture

                                    cheers, Alok Gupta VC Forum Q&A :- I/IV Support CRY- Child Relief and You/codeProject$$>

                                    1 Reply Last reply
                                    0
                                    • T ThatsAlok

                                      :-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" - Mixture

                                      cheers, Alok Gupta VC Forum Q&A :- I/IV Support CRY- Child Relief and You/codeProject$$>

                                      T Offline
                                      T Offline
                                      ThatsAlok
                                      wrote on last edited by
                                      #18

                                      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" - Mixture

                                      cheers, Alok Gupta VC Forum Q&A :- I/IV Support CRY- Child Relief and You

                                      1 Reply Last reply
                                      0
                                      • R Rajesh R Subramanian

                                        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

                                        T Offline
                                        T Offline
                                        ThatsAlok
                                        wrote on last edited by
                                        #19

                                        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" - Mixture

                                        cheers, Alok Gupta VC Forum Q&A :- I/IV Support CRY- Child Relief and You

                                        1 Reply Last reply
                                        0
                                        Reply
                                        • Reply as topic
                                        Log in to reply
                                        • Oldest to Newest
                                        • Newest to Oldest
                                        • Most Votes


                                        • Login

                                        • Don't have an account? Register

                                        • Login or register to search.
                                        • First post
                                          Last post
                                        0
                                        • Categories
                                        • Recent
                                        • Tags
                                        • Popular
                                        • World
                                        • Users
                                        • Groups