By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,633 Members | 1,905 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,633 IT Pros & Developers. It's quick & easy.

RE:Recursion

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

Expand|Select|Wrap|Line Numbers
  1. class Program
  2.     {
  3.         static void Main(string[] args)
  4.         {
  5.             Program.count(1);
  6.  
  7.             Console.ReadKey();
  8.         }
  9.         static int count(int i) {
  10.  
  11.             Console.WriteLine(i);
  12.             if (i < 10) {
  13.                 Console.WriteLine(count(i+1)+(i-1));
  14.             }
  15.             return 1;
  16.         }
  17.     }
Thank you very much in advance.
Aug 3 '08 #1
Share this Question
Share on Google+
3 Replies


kenobewan
Expert 2.5K+
P: 4,871
Sounds like you copied, not sure why else you don't understand. Not sure what you are trying to achieve...
Aug 3 '08 #2

P: 14
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.
Aug 3 '08 #3

Curtis Rutland
Expert 2.5K+
P: 3,256
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.
Aug 3 '08 #4

Post your reply

Sign in to post your reply or Sign up for a free account.