Thread: c++ recursion help

  1. #1
    Registered User
    Join Date
    Apr 2015
    Location
    Searcy, Arkansas, United States
    Posts
    3

    c++ recursion help

    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)

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Apr 2015
    Location
    Searcy, Arkansas, United States
    Posts
    3
    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.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Recursion just means that a function calls itself. E.g.:

    Code:
    float SumXY(float X, float Y)
    {
    //...
    SumXY(A, B);
    //...
    }
    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.
    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?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Nov 2013
    Posts
    107
    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?

  6. #6
    Registered User
    Join Date
    Apr 2015
    Location
    Searcy, Arkansas, United States
    Posts
    3
    Yes I do have to have a recursive solution. This problem is just an example problem that is apart of a worksheet.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Matthew323
    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.
    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.

    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by laserlight View Post
    ...Elysia's (inaccurate) statement that "recursion just means that a function calls itself" refers to the recursive step...
    I don't think it's very inaccurate. A recursive function is a function that accomplishes its work by calling itself. Is it enough to just call itself to implement a recursive function? No, but then again, I wasn't referring to how to implement one anyway.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Elysia
    I don't think it's very inaccurate. A recursive function is a function that accomplishes its work by calling itself.
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Ah, I see. Fair enough.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. recursion
    By rakeshkool27 in forum C Programming
    Replies: 9
    Last Post: 04-19-2010, 11:59 PM
  2. Recursion
    By mikeman in forum C++ Programming
    Replies: 4
    Last Post: 03-29-2010, 12:26 PM
  3. recursion in C, need some help
    By -EquinoX- in forum C Programming
    Replies: 4
    Last Post: 04-14-2008, 08:19 PM
  4. Recursion
    By scottmanc in forum C++ Programming
    Replies: 3
    Last Post: 03-05-2003, 03:53 PM
  5. Recursion
    By MethodMan in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 09-30-2002, 08:33 PM