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";
            }
        }
    }

Recommended Answers

All 2 Replies

that's really crappy code you posted :cheesy:

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 );

}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.