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. HTF? :mad:

HTF? :mad:

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestion
7 Posts 3 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.
  • _ Offline
    _ Offline
    _8086
    wrote on last edited by
    #1

    How the f***:omg::wtf: X| X| X| this is sickening.

    void fun(int);
    void main()
    {
    int a ;
    a=3;
    fun(a);
    }

    void fun(int n)
    {
    if(n==0)//orif(n==-1)
    exit(0);<--If I put this the ouput is nothing, but if I remove it, the idiot compiler is bypassing the condition (0>0),(-1>0).
    if(n>0)//3,2,1,0,-1
    {
    printf("\nTHERE!");
    fun(--n);
    printf("\n%d",n); //HOW THE **** the control comes here?!!!?? :((
    fun(--n);////HOW THE **** the control comes here?!!!?? :((

    }
    else
    {
    	printf("\\nHERE!");
    }
    

    }

    How 0,-1 can be > than 0? shit. I'm not on drugs! but :wtf: mark,led,jeron,toxcct,david someone help! help! without exit(0),it prints

    0
    1
    2
    0

    ---------------------------- 286? WOWW!:-O

    P 1 Reply Last reply
    0
    • _ _8086

      How the f***:omg::wtf: X| X| X| this is sickening.

      void fun(int);
      void main()
      {
      int a ;
      a=3;
      fun(a);
      }

      void fun(int n)
      {
      if(n==0)//orif(n==-1)
      exit(0);<--If I put this the ouput is nothing, but if I remove it, the idiot compiler is bypassing the condition (0>0),(-1>0).
      if(n>0)//3,2,1,0,-1
      {
      printf("\nTHERE!");
      fun(--n);
      printf("\n%d",n); //HOW THE **** the control comes here?!!!?? :((
      fun(--n);////HOW THE **** the control comes here?!!!?? :((

      }
      else
      {
      	printf("\\nHERE!");
      }
      

      }

      How 0,-1 can be > than 0? shit. I'm not on drugs! but :wtf: mark,led,jeron,toxcct,david someone help! help! without exit(0),it prints

      0
      1
      2
      0

      ---------------------------- 286? WOWW!:-O

      P Offline
      P Offline
      PJ Arends
      wrote on last edited by
      #2

      First off, what is the output you are expecting? With the recursive calls you are making the output you are getting makes perfect sense.


      You may be right
      I may be crazy
      -- Billy Joel --

      Within you lies the power for good, use it!!!

      _ 1 Reply Last reply
      0
      • P PJ Arends

        First off, what is the output you are expecting? With the recursive calls you are making the output you are getting makes perfect sense.


        You may be right
        I may be crazy
        -- Billy Joel --

        Within you lies the power for good, use it!!!

        _ Offline
        _ Offline
        _8086
        wrote on last edited by
        #3

        Thanks Arends, but why does it bypass this condtion when n =0, or -1 if(n>0) //(0>0) (-1>0) [after successive calls to --n & fun(n)] { but still it comes here! why? } I still can't get that point. Recursion makes multiple copies of the function? may be I'm missing this? it gets into the stack everytime we call it, so the context of "n" changes? I can somehow come around the point but still need some help to understand it. Please help dude. -- modified at 14:06 Saturday 31st March, 2007

        ---------------------------- 286? WOWW!:-O

        P CPalliniC 2 Replies Last reply
        0
        • _ _8086

          Thanks Arends, but why does it bypass this condtion when n =0, or -1 if(n>0) //(0>0) (-1>0) [after successive calls to --n & fun(n)] { but still it comes here! why? } I still can't get that point. Recursion makes multiple copies of the function? may be I'm missing this? it gets into the stack everytime we call it, so the context of "n" changes? I can somehow come around the point but still need some help to understand it. Please help dude. -- modified at 14:06 Saturday 31st March, 2007

          ---------------------------- 286? WOWW!:-O

          P Offline
          P Offline
          PJ Arends
          wrote on last edited by
          #4

          _8086 wrote:

          but why does it bypass this condtion when n =0, or -1

          It does not, the recursive function calls stop when n is zero or less. I think your confusion is coming from what n is. You have to remember that a new n is created every time fun() is called, you are not using a single n. I cleaned up your sample a bit, so lets step through it:

          void fun(int n) // start with n = 3
          {
          if (n > 0) // n is greater than zero
          {
          fun(--n); // n is now two, recursive call to fun(2)

              +void fun(int n)                     // start with n = 2
              |{
              |   if (n > 0)                       // n is greater than zero
              |   {
              |      fun(--n)                      // n is now one, another recursive call to fun(1)
              |
              |         +void fun(int n)           // start with n = 1
              |         |{
              |         |   if (n > 0)             // n is greater than zero
              |         |   {
              |         |      fun(--n)            // n is now zero, another recursive call to fun(0)
              |         |
              |         |         +void fun(int n) // start with n = 0;
              |         |         |{
              |         |         |   if (n > 0)   // n is not greater than zero
              |         |         |   {
              |         |         |      fun(--n)  // not called
              |         |         |      printf("%d\\n", n);
              |         |         |   }
              |         |         +}               // exit fun()
              |         |
              |         |      printf("%d\\n", n);  // n is zero - output "0"
              |         |   }
              |         +}                         // exit fun()
              |
              |      printf("%d\\n", n);            // n is one - output "1"
              |   }
              +}                                   // exit fun()
          
            printf("%d\\n", n);                     // n is two - output "2"
          

          }
          } // exit fun()

          and repeat ad infinitum.


          You may be right
          I may be crazy
          -- Billy Joel --

          Within you lies the power for good, use it!!!

          _ 1 Reply Last reply
          0
          • _ _8086

            Thanks Arends, but why does it bypass this condtion when n =0, or -1 if(n>0) //(0>0) (-1>0) [after successive calls to --n & fun(n)] { but still it comes here! why? } I still can't get that point. Recursion makes multiple copies of the function? may be I'm missing this? it gets into the stack everytime we call it, so the context of "n" changes? I can somehow come around the point but still need some help to understand it. Please help dude. -- modified at 14:06 Saturday 31st March, 2007

            ---------------------------- 286? WOWW!:-O

            CPalliniC Offline
            CPalliniC Offline
            CPallini
            wrote on last edited by
            #5

            Of course the compiler is working fine. Please follow me in code inspection, to make analysis shorter, let's start with fun(2):

            fun(2){
            if(2>0){
            fun(1);

            At the moment (no output yet produced) we have to stop considering fun(2) and, due to recursion, procede with the inspection of fun(1):

            fun(1){
            if(1>0){
            fun(0);

            Again (no output yet), we have to suspend considering fun(1) and go deeper in recursion with fun(0):

            fun(0){
            if(0>0){

            Here, the compiler, that is a honest guy, correctly evaluates (0>0) as false and the function returns. Have we done? No, of course, there are (in the order) fun(1) and fun(2) waiting on the stack. So let's go back to fun(1) and reprise whereever we suspended:

            printf("\\n%d",0); 
            fun(-1);
            

            }

            As you can see, this is the first time we have a number on the console, and the number is 0 (you can also see that fun(-1) is called, but it will do nothing). Of course we could go on with code inspection, but I think it's enough: we have the zero, and we also have the demonstration that the compiler is not insane. hope that: (1) the analysis is correct. (2) it helps. :)

            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.

            In testa che avete, signor di Ceprano?

            _ 1 Reply Last reply
            0
            • P PJ Arends

              _8086 wrote:

              but why does it bypass this condtion when n =0, or -1

              It does not, the recursive function calls stop when n is zero or less. I think your confusion is coming from what n is. You have to remember that a new n is created every time fun() is called, you are not using a single n. I cleaned up your sample a bit, so lets step through it:

              void fun(int n) // start with n = 3
              {
              if (n > 0) // n is greater than zero
              {
              fun(--n); // n is now two, recursive call to fun(2)

                  +void fun(int n)                     // start with n = 2
                  |{
                  |   if (n > 0)                       // n is greater than zero
                  |   {
                  |      fun(--n)                      // n is now one, another recursive call to fun(1)
                  |
                  |         +void fun(int n)           // start with n = 1
                  |         |{
                  |         |   if (n > 0)             // n is greater than zero
                  |         |   {
                  |         |      fun(--n)            // n is now zero, another recursive call to fun(0)
                  |         |
                  |         |         +void fun(int n) // start with n = 0;
                  |         |         |{
                  |         |         |   if (n > 0)   // n is not greater than zero
                  |         |         |   {
                  |         |         |      fun(--n)  // not called
                  |         |         |      printf("%d\\n", n);
                  |         |         |   }
                  |         |         +}               // exit fun()
                  |         |
                  |         |      printf("%d\\n", n);  // n is zero - output "0"
                  |         |   }
                  |         +}                         // exit fun()
                  |
                  |      printf("%d\\n", n);            // n is one - output "1"
                  |   }
                  +}                                   // exit fun()
              
                printf("%d\\n", n);                     // n is two - output "2"
              

              }
              } // exit fun()

              and repeat ad infinitum.


              You may be right
              I may be crazy
              -- Billy Joel --

              Within you lies the power for good, use it!!!

              _ Offline
              _ Offline
              _8086
              wrote on last edited by
              #6

              :-O :-O :-O :-O :jig: :jig: :beer::beer::beer: Excellent. yourNewArtile* pjaRecursionArticle = (aritcle*) yourMessage; :-D

              ---------------------------- 286? WOWW!:-O

              1 Reply Last reply
              0
              • CPalliniC CPallini

                Of course the compiler is working fine. Please follow me in code inspection, to make analysis shorter, let's start with fun(2):

                fun(2){
                if(2>0){
                fun(1);

                At the moment (no output yet produced) we have to stop considering fun(2) and, due to recursion, procede with the inspection of fun(1):

                fun(1){
                if(1>0){
                fun(0);

                Again (no output yet), we have to suspend considering fun(1) and go deeper in recursion with fun(0):

                fun(0){
                if(0>0){

                Here, the compiler, that is a honest guy, correctly evaluates (0>0) as false and the function returns. Have we done? No, of course, there are (in the order) fun(1) and fun(2) waiting on the stack. So let's go back to fun(1) and reprise whereever we suspended:

                printf("\\n%d",0); 
                fun(-1);
                

                }

                As you can see, this is the first time we have a number on the console, and the number is 0 (you can also see that fun(-1) is called, but it will do nothing). Of course we could go on with code inspection, but I think it's enough: we have the zero, and we also have the demonstration that the compiler is not insane. hope that: (1) the analysis is correct. (2) it helps. :)

                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.

                _ Offline
                _ Offline
                _8086
                wrote on last edited by
                #7

                Thanks you very much Pallini :) :beer: :beer: :beer:

                ---------------------------- 286? WOWW!:-O

                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