public static double RecursiveMeth(double sum, int a)
{
if (a == 100000)
{
return sum;
}
else
{
return RecursiveMeth(sum + 10a, a+1);
}
}
=============================
it works if I put a == 10000
Printable View
public static double RecursiveMeth(double sum, int a)
{
if (a == 100000)
{
return sum;
}
else
{
return RecursiveMeth(sum + 10a, a+1);
}
}
=============================
it works if I put a == 10000
Simple! Use an iterative approach instead of recursive. Similar to this: http://www.codeproject.com/Articles/...ive-Approaches
I have to use a recursive method though. It's a homework.
First, generally speaking, you shouldn't seek to "bypass" exceptions - you should either handle them appropriately, or if they are unexpected, fix the bug that causes them (as in your case).
You're getting a StackOverflowException simply because you have way too many recursive calls.
What is the actual problem you're trying to solve, and also, what is the purpose of the exercise (what is you teacher trying to demonstrate)?
You're probably going wrong about it.
The first thing you should know about recursion is that there must be a way for the method to determine when it should stop calling itself. This is known as the base case - the case your recursive function knows readily how to solve. Otherwise, the you have the recursive case, and the function calls itself, passing along the subproblem it can't readily solve, to be somehow solved by recursion.
Basically, it's the following logic (or some variation of it):
*Just a way to think about it - but the path to solution is well defined.
- Figure out a way to decompose the overall problem to more manageable subproblems.
- If it's the base case - solve the simple problem and return the result.
- Else (recursive case) - call the method recursively, and let it somehow* solve the current subproblem.
- When the recursive call returns - integrate the solution of the subproblem with the current overall problem, to finally solve it.
- Return the solution - if the whole, original problem is solved, that's the end; otherwise, the calling function obtains the result and picks up at (4).
What your program does is basically a really convoluted way to write an equivalent of a for loop, and one that's not working at that.
What are you actually trying to do?
P.S. Please use [code][/code] tags when you post, in order to preserve formatting.
Like this:
(Note: the tags do not format previously unformatted code.)Code:public static double RecursiveMeth(double sum, int a)
{
if (a == 100000)
{
return sum;
}
else
{
return RecursiveMeth(sum + 10a, a+1);
}
}
As Cthulhu (I spelled it right!) stated very nicely, you cannot and should not "bypass" a StackOverflowException. It's not something that you can simply ignore and hope everything turns out ok. You have blown your stack, game over.
C# is not capable to tail call optimization. Therefore you always run the risk of blowing the stack if you go too deep. If your instructor requires a recursive solution in C# with no boundary on the size of the input (i.e., the number of recursive calls) then your professor doesn't know the language very well.