954,506 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

converting Nested loops into recursion

I need to convert a code having arbitary number of nested loops using tail recursion. can any one provide me its solution in c/c++.

For example:

for ( int a = 0; a < 3; a++ )
    {
        for ( int b = 0; b < 3; b++ )
        {
            for ( int c = 0; c < 3; c++ )
            {
                cout << crap[a] <<" "<<crap[b]<<" "<<crap[c];
                cout <<"\n";
            }
        }
    }
tgmtgm1
Newbie Poster
1 post since Jan 2007
Reputation Points: 10
Solved Threads: 0
 

that's really crappy code you posted :cheesy:

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

you can use this, it is simple. :)

void vProcess( int a, int b, int c )
{
    if( c>=3 ) 
    {
        b++;
        c=0;
        vProcess( a, b, c );
        return;
    }
    if( b>=3 ) 
    {
        a++;
        b=0;
        vProcess( a, b, c );
        return;
    }
    if( a>=3 ) return; 

    cout << crap[a] <<" "<<crap[b]<<" "<<crap[c];
    cout <<"\n";
    
    c++;
    vProcess( a, b, c );

}
VatooVatoo
Light Poster
44 posts since Jan 2007
Reputation Points: 20
Solved Threads: 2
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You