RE:Recursion

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • lordango
    New Member
    • Jul 2008
    • 14

    RE:Recursion

    can someone explain to me the program flow of this code:

    Code:
    class Program
        {
            static void Main(string[] args)
            {
                Program.count(1);
    
                Console.ReadKey();
            }
            static int count(int i) {
    
                Console.WriteLine(i);
                if (i < 10) {
                    Console.WriteLine(count(i+1)+(i-1));
                }
                return 1;
            }
        }
    Thank you very much in advance.
    Last edited by kenobewan; Aug 3 '08, 01:25 PM. Reason: Please use code tags
  • kenobewan
    Recognized Expert Specialist
    • Dec 2006
    • 4871

    #2
    Sounds like you copied, not sure why else you don't understand. Not sure what you are trying to achieve...

    Comment

    • lordango
      New Member
      • Jul 2008
      • 14

      #3
      The output is:
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      9
      8
      7
      6
      5
      4
      3
      2
      1

      I did the code (trial and error) i did not copied it. I saw the problem on the internet to display 1-10 then 10-1 so i did.
      I can follow from 1-10 but after that im confused because i tried changing the value of return 0 and the output changes. I just want to know how does the return 0 affects the output.

      Can you please show me the program flow?

      I tried to use F11 + addwatch but still i can trace the part where the recursion happens.
      Did the function stored the return values while it was recursing or is it using the return 0?

      Thank you. and again i did not copy it. I did it by trial and error.

      Comment

      • Curtis Rutland
        Recognized Expert Specialist
        • Apr 2008
        • 3264

        #4
        So what about this thread where someone has already helped you step through a different recursive program?

        The experts here are not necessarily teachers...I suggest that you get a recursion tutorial (recursion is just logic, so it can be in any language) and read it. Once you understand the theory behind recursion, these problems aren't as confusing.

        Comment

        Working...