Thread: help with a recursion function

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    37

    Talking help with a recursion function

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

  2. #2
    Registered User
    Join Date
    Sep 2007
    Location
    Arizona
    Posts
    164
    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:
    Code:
    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
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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
    Location
    Canada
    Posts
    8,057
    . . . 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?
    Code:
    		!(mathTest());
    		mathTest();
    dwk

    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: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Registered User mikeman118's Avatar
    Join Date
    Aug 2007
    Posts
    183
    Code:
        for(int index=1; index<=2; index++){
    		if (index == 1)
    			{
    			random_integerA = (rand()%10); 
    			}
    		else
    			{
    			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:
    Code:
    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
    Location
    New Zealand
    Posts
    6,318
    Please read the following entry of the FAQ:
    http://faq.cprogramming.com/cgi-bin/...&id=1043284385
    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
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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].

    --
    Mats
    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
    Posts
    8,895
    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!
    CornedBee

    "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