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";
            }
        }
    }
Re: converting Nested loops into recursion 80 80

that's really crappy code you posted :cheesy:

Re: converting Nested loops into recursion 80 80

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 1.19 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.