recursion

gothic130
gothic130 used Ask the Experts™
on
hi, when you have a recursive function that works like this:

call 1           i=1
call 2              i=2
call 3                 i=3
call 4                 3
call 5              2
call 6          1

I can get the value of i until the 3, but then it began to decrese, how could I avoid that? because the next value I need is i=4.

this is my function:

public void DFS(verticeGrapho tmp, int i, int j, int n, int[,] ma, int g)
{
            tmp.info = i;
            i++;
            for (int k = g; k <= n; k++)
            {
                if (ma[j, k] == 1 && tmp.info == 0)
                {
                    DFS(tmp, i, g, n, ma, g);
                }
                    tmp = tmp.next; g++;
            }
}

Hope you understand my question!!!

Thanks!!!

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Senior Systems and Integration Developer
Commented:
The value isn't decreasing, its just that your nested calls are exiting and each call is retaining the value it had before the recursive call took place.

If you want to maintain the value then you will need to pass i by reference rather than by value:

public void DFS(verticeGrapho tmp, ref int i, int j, int n, int[,] ma, int g)
{
            tmp.info = i;
            i++;
            for (int k = g; k <= n; k++)
            {
                if (ma[j, k] == 1 && tmp.info == 0)
                {
                    DFS(tmp, ref i, g, n, ma, g);
                }
                    tmp = tmp.next; g++;
            }
}

   
HI,

I'm not sure we can help without seeing the code for the verticeGrapho object and more explanation about the variables used, but something in the call must make the tmp.info member = 0. otherwise you would never come out of the recursive loop. what does tmp.next do?

BB

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial