Thread: help with a recursion function

  1. #1
    Registered User
    Join Date
    Oct 2007

    Talking help with a recursion function

    Last edited by nicz888; 11-26-2007 at 09:12 AM. Reason: i got it

  2. #2
    Registered User
    Join Date
    Sep 2007
    I can actually answer someone elses question, if you want a loop, if you want it to be a true recursive call, this isn't it and I really CAN'T help you with recursion, really!

    This code is in detail in the thread on prime numbers and stacks, but here is a shorter version:
    int main()
    	// variable declarations
    	//program banner - what the programs does and how
                    //ask the question - do you want to try your had at what the program does.
    	cin >> userResponse;    //the users responds  Y or y to continue, anything else to end
    	while (userResponse == 'Y' || userResponse == 'y')
                                   //the meat of your program
    		//ask the question again.
    		cin >> userResponse;
    	}//end while
    	return 0;
    }//end main
    as long as the user keeps answering the "do you want to go again" question with a Y or y (in this case), it will keep allowing use of the program. As soon as something (anything other than a Y or y) is entered the program terminates.

    Hope this helps with the your problem.

    Whether the user got the answer right or wrong, your functions should send control back to main and let main ask if they want to "do it again."
    Last edited by clegs; 11-25-2007 at 05:30 PM.

  3. #3
    Join Date
    Oct 2007
    Inside my computer
    Generally, if something can be done easily with a loop, then do so. Recursion is an advanced technique that sucks up stack space everytime you use it.

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    . . . assuming that the compiler couldn't optimize the recursion into iteration. You'd be surprised at how often this happens.

    That's not a good thing to depend upon, of course.

    You're aware, of course, that these both do exactly the same thing?

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell

    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ:

    My website:
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Registered User mikeman118's Avatar
    Join Date
    Aug 2007
        for(int index=1; index<=2; index++){
    		if (index == 1)
    			random_integerA = (rand()%10); 
    			random_integerB = (rand()%10);
    Just a tip, I see no point in this, besides some wasted code. It works, of course, but the much more efficient way would just be this:
    random_integerA = (rand()%10); 
    random_integerB = (rand()%10);
    Also, maybe consider better indentation? (not horrible, but could be better)

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    New Zealand
    Please read the following entry of the FAQ:
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Farncombe, Surrey, England
    whilst dwks makes a good point, you can't actually rely on recursion being optimized to a loop - you can examine the code and see if it's done that, but you can't rely on it. A typical example where it doesn't happen is if you use any of the input variables after the recursion call.

    Using explicit loops is the way to go. Recursion should only be used where the depth of the recursion can be determined. Using recursion for "boundless" repetitions will lead to a stack overflow eventually [ok, not likely in this case, as the user will probably get bored before it's got anywhere near filling the stack, but that's not a good reason to use that method].

    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    OP: Please don't delete your query after having been answered. Doing so makes it impossible for other people to find your problem and the solution in the forum, thus forcing them to ask the same thing again when it could have been answered by a forum search.
    All the buzzt!

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  3. Change this program so it uses function??
    By stormfront in forum C Programming
    Replies: 8
    Last Post: 11-01-2005, 08:55 AM
  4. Question..
    By pode in forum Windows Programming
    Replies: 12
    Last Post: 12-19-2004, 07:05 PM
  5. Replies: 5
    Last Post: 02-08-2003, 07:42 PM