If we don't know how many for we should use,should we use recursive function?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: If we don't know how many for we should use,should we use recursive function?

  1. #1
    Join Date
    Oct 2009
    Posts
    40

    If we don't know how many for we should use,should we use recursive function?

    Let's say that we don't know how many "for loops" we need before the program starts and user inputs a number,should we use recursive function instead of "for loops"?Or is there a way to do it using "for loops"?

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,244

    Re: If we don't know how many for we should use,should we use recursive function?

    What you ask for can be done using a for loop, but perhaps a while loop would be more appropriate. It depends on the specifics. Without tail recursion optimisation, recursion could be worse as there is a maximum recursive depth before say, stack space runs out.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,010

    Re: If we don't know how many for we should use,should we use recursive function?

    Quote Originally Posted by AwArEnEsS View Post
    Let's say that we don't know how many "for loops" we need before the program starts and user inputs a number,should we use recursive function instead of "for loops"?Or is there a way to do it using "for loops"?
    See the discussion here: http://forums.codeguru.com/showthrea...-and-recursion
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

  4. #4
    Join Date
    May 2009
    Posts
    2,413

    Re: If we don't know how many for we should use,should we use recursive function?

    Quote Originally Posted by AwArEnEsS View Post
    Let's say that we don't know how many "for loops" we need before the program starts and user inputs a number,should we use recursive function instead of "for loops"?Or is there a way to do it using "for loops"?
    In the situation you mention there's no difference in principle. In both cases you need a stop criterion to know when to stop iterating or when to stop making recursive calls. In fact iteration and recursion are equivalent. You can always turn one into the other.

    Traditionally in procedural languages such as C++ recursion is used mostly when the problem at hand has a very pronounced recursive structure and I don't feel that's the case here. I and probably more than 99% of whoever you ask would use iteration.
    Last edited by nuzzle; December 16th, 2012 at 04:16 AM.

  5. #5
    Join Date
    Oct 2009
    Posts
    40

    Re: If we don't know how many for we should use,should we use recursive function?

    Thanks for your answers.

    I couldn't solve the problem,so I want to give the code:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    #define QUEENS 8
    #define int1 unsigned
    
    int1 number=0;
    int1 board[QUEENS];
    
    
    int1 boardOK(register int turn)
    {
        register int r;
    
    
    
        for (r=0;r<turn;r++)
        {
            if (board[turn]==board[r] ||
                board[turn]==board[r] << turn-r ||
                board[turn]==board[r] >> turn-r)
            {   //printf("\nr:%d",r);
                return 0;
            }
    
        }
        return 1;
    }
    
    
    void printsolution(void)
    {
        register int1 t,r;
    
        printf("\n\n\tSOLUTION %u\n\n",++number);
        for (r=0;r<QUEENS;r++)
        {
            for (t=1<<QUEENS-1;t>0;t>>=1)
                printf(" %c",board[r]==t?'V':'.');
            printf("\n");
        }
    
       // system("pause");
    }
    
    
    
    void placequeens(int1 turn)
    {
    
       
        if (turn==QUEENS)
            printsolution();
    
        else
    
            for (board[turn]=1;board[turn]<1<<QUEENS;board[turn]<<=1)
                if (boardOK(turn))
                    placequeens(turn+1);
    
    
    }
    
    
    
    int main()
    {
       placequeens(0);
       printf("\n there are %u solutions to %d Queens problem\n",number,QUEENS);
    }



    I am trying to turn these codes into a program which uses for loop instead of recursive function,but I couldn't.In the book it says "you can do the same using for loop instead of recursive function" but it doesn't write how we can do it.Also,I need the answer in C not C++ please.
    Last edited by AwArEnEsS; December 18th, 2012 at 09:54 PM.

  6. #6
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,010

    Re: If we don't know how many for we should use,should we use recursive function?

    Quote Originally Posted by AwArEnEsS View Post
    I couldn't solve the problem,so I want to give the code:
    Please use code tags; your code is practically unreadable without them.
    Quote Originally Posted by AwArEnEsS View Post
    I am trying to turn these codes into a program which uses for loop instead of recursive function,but I couldn't.In the book it says "you can do the same using for loop instead of recursive function" but it doesn't write how we can do it.Also,I need the answer in C not C++ please.
    See http://stackoverflow.com/a/531704
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

  7. #7
    Join Date
    Oct 2009
    Posts
    40

    Re: If we don't know how many for we should use,should we use recursive function?

    Thanks for your answer.I have looked at both the links you gave.Seems like it's a bit more complex than I thought.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center