I am trying to solve this problem recursively.
Write a Function called SumXY which will sum the arithmetic series
X +(X+1) + (X+2) + ... + (Y-1) +Y
SumXY(5,9) =35
float SumXY(float X, float Y)
I am trying to solve this problem recursively.
Write a Function called SumXY which will sum the arithmetic series
X +(X+1) + (X+2) + ... + (Y-1) +Y
SumXY(5,9) =35
float SumXY(float X, float Y)
Refer to this thread.
Anyway, what ideas do you have and what have you tried? For example, have you thought about the base case and recursive step?
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
I haven't done anything to this problem yet. I really don't understand recursion at all and my teacher didn't explain it well. I was just looking for a general idea from someone that could at least point me in the right direction on what this problem means or a way to solve or understand it.
Recursion just means that a function calls itself. E.g.:
Where A and B are just some values. You should obviously choose A and B correctly. They shouldn't just be any values, but it suffices to explain the theory.Code:float SumXY(float X, float Y) { //... SumXY(A, B); //... }
For recursion, you must break the problem into a reoccurring problem. But perhaps it might help to take a look at what recursion is and some examples on how to use it first. Have you tried googling?
You also need some conditions to to end the recursive loop otherwise it will never end, and also hang your PC. It's not the best series to do recursion with, are you sure it needs to be recursive?
Yes I do have to have a recursive solution. This problem is just an example problem that is apart of a worksheet.
If you don't understand recursion at all then you should revise your initial learning material on recursion, e.g., you may have been provided examples of a recursive implementation for adding the first N natural numbers, or computing factorial, or the Fibonacci sequence.Originally Posted by Matthew323
I mentioned "base case" and "recursive step". Elysia's (inaccurate) statement that "recursion just means that a function calls itself" refers to the recursive step; jim_0's assertion that you "need some conditions to to end the recursive loop" refers to the base case. These should have been covered in your learning material.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Well, I was thinking that that is inaccurate because mutual recursion is still recursion. I suppose that you could argue that the indirect calling of itself is covered by your statement that it "calls itself", though I find the word "just" to be misleading in that context.Originally Posted by Elysia
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)