Click here to Skip to main content
15,116,653 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
I put the return in the specific condition using if statement within the method but my program call that method again buz it is recursive m+d.. that is why I ask to terminate the whole m+d in that condition without calling the m+d again..

What I have tried:

C#
public void subset(string[] A, int k, int start, int currLen, bool[] used, int tot)
        {
if(---
  return    --terminate
    if (start == A.Length)
                {
                    return;
                }
                          
                used[start] = true;

                subset(A, k, start + 1, currLen + 1, used, tot);
                

                used[start] = false;
                subset(A, k, start + 1, currLen, used, tot);
            }
        }
Posted
Updated 4-Sep-17 16:52pm
v2
Comments
Patrice T 4-Sep-17 11:19am
   
Update your question with realistic code.
Graeme_Grant 4-Sep-17 11:25am
   
I agree ... overly vague and formatting makes it difficult to read.

To be honest I am not sure what your code sample is trying to relay here.

I would have a read of this Recursive methods using C#[^]
   
If I ignore the beginning "if(---
return --terminate"

and I ignore the extra close brace "}"

Then it seems to me that your program terminates

So for example I tried:
subset(new string[] { "Abc", "Def" }, 0, 0, 0, new bool[2], 0);

and it terminated.

You however talked about m + d which I did not understand, though you call subset recursively twice...

Please be more specific in your question.
   
Comments
Thaksha 6-Sep-17 6:22am
   
Just I want stop the whole recursive call execution in one specific condition (if statement) means my program not going to call the method again after that condition, and show the result until that state.
Avi Farah 6-Sep-17 16:51pm
   
Your question is too vague.

In general to write a recursive program you need:
1. A recursive definition and
2. A terminating condition.

Your terminating condition is checked before your recursive-call and the recursive-call needs to get you closer to the terminating condition evaluating true, otherwise you will end up in an infinite recursion.

Hope this helps,
Avi
Thaksha 11-Sep-17 1:16am
   
Thank for your reply....I already know that recursive call have to have the terminating condition to finish the method. But I have the problem when I put the termination condition then it call again because the recursive function deal with the stack memory.
So I want the combinations without recursion in c#.
When I put the input like A,B,C,D
output have to be like A B C D
A B C
A B D
A C D
B C D
A B
A C
A D
B C
B D
C D
A
B
C
D
So do you have or know any codes for this.
Avi Farah 11-Sep-17 17:06pm
   
Again, your question is way too vague for me to comment on specifics. Are we still dealing with the same algorithm? How do the letters A, B, C, D apply to the algorithm.

My feeling is that you need to go after your logic, that you did not publish.

What comes to mind about your double recursive call is fibonacci series:
fibonacci[0] = f0 (a constant say 0)
fibonacci[1] = f1 (another constant say 1)
fibonacci[n] = fibonacci[n-2] + fibonacci[n-1]

This is a double recursive call (btw very computing time expensive)

However we can simplify/cheapen it: consider a 2 number tuple denoted by (x, y) then:
fib[0] = (f0, f1)
fib[n] = next(fib[n-1])

where next( (x,y) ) = (y, x + y)

and fibonacci[n] = fib[n].First

Now we succeeded to reduce a 2 recursive call to 1.

Hope this helps.
Avi

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900