473,386 Members | 1,668 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

RE:Recursion

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
3 895
kenobewan
4,871 Expert 4TB
Sounds like you copied, not sure why else you don't understand. Not sure what you are trying to achieve...
Aug 3 '08 #2
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
3,256 Expert 2GB
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

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

Similar topics

5
by: Peri | last post by:
I'm trying to create Python parser/interpreter using ANTLR. Reading grammar from language refference I found: or_expr::= xor_expr | or_expr "|" xor_expr For me it looks like infinite recursion....
3
by: csx | last post by:
Hi all, Ive got a problem with recursion in Javascript. For this tree: http://www.pcm.uklinux.net/structure.jpg If you input node 3 (i.e. C) which is represented as 'values' in the array, it...
43
by: Lorenzo Villari | last post by:
I've tried to transform this into a not recursive version but without luck... #include <stdio.h> void countdown(int p) { int x;
24
by: proctor | last post by:
hello, i have a small function which mimics binary counting. it runs fine as long as the input is not too long, but if i give it input longer than 8 characters it gives RuntimeError: maximum...
10
by: elventear | last post by:
Hello everyone, I am runing into recursion limit problems. I have found that the culprit was related to the __hash__ function that I had assigned to the objects that were added to a set. ...
14
by: =?Utf-8?B?c2VlbWE=?= | last post by:
Write a function that produces the same output as MyFunction but without the use of recursion. ( perfect C# is not a requirement, you may write in pseudo code )
15
by: Gigs_ | last post by:
Can someone explain me this if l == : return else: return f(l) + l # <= cant figure this, how is all sum at the end? thanks!
8
by: smartbeginner | last post by:
The Problem is CFG grammar generation Eg: Input S->ABC A->bcd B->efg C->hij It should produce the o/p bcdefghij substituting for every Capitals found on the way recursively The C pgm I...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.