Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

unpredictable number of nested loops

Tags:

c++

recursion

I'm trying to make a program that requires nested loops to work properly. But the number of nested loops depend on the number of character the user inputs also the character to output.

This is my code so far.

#include<iostream>
using namespace std;


int main(){
    string str;
    cout<<"Enter some string: ";
    cin>>str;

    // for two characters
    for(int i = 0; i < 2; i++){
        for(int j = 0; j < 2 ; j++){
            cout<<str[i]<<str[j]<<endl;
        }
    };

    // for four characters
    for(int i = 0; i<4; i++){
        for(int j=0;j<4;j++){
            for(int k =0;k<4;k++){
                for(int z=0;z<4;z++)
                    cout<<str[i]<<str[j]<<str[k]<<str[z]<<endl;
                }
        }
    }
    return 0;
}

So, is there any way to solve this issue.

like image 837
Zullu Avatar asked Dec 02 '18 07:12

Zullu


People also ask

How many nested loops is too many?

There are no limits. Or, more precisely, there are no maximum limits on the user. There are minimum limits on the compiler, however. So you can have 127 nested loops/if/switch and 12-dimensional arrays and be sure that any compliant compiler will be able to compile you.

Can you have 3 nested loops?

In the simple cases, yes. In more complex cases, when loop indexes start at numbers indicated by other indexes, the calculations are more complex.

How many times nested loops run?

The number of times a nested loop executes is the number of times the outer loop executes (3) times the number of the times the inner loop executes (4) so that is 3 * 4 = 12. This would be true if both were range(0,4).

Is it bad to have nested for loops?

Nested loops are frequently (but not always) bad practice, because they're frequently (but not always) overkill for what you're trying to do. In many cases, there's a much faster and less wasteful way to accomplish the goal you're trying to achieve.


1 Answers

You need to do it dynamically:

std::vector<unsigned int> offsets(s.size());

bool isContinue;
do
{
    for(auto offset : offsets)
    {
        std::cout << s[offset];
    }
    std::cout << std::endl;

    isContinue = false;
    for(auto offset = offsets.rbegin(); offset != offsets.rend(); ++offset)
    {
        if(++*offset < s.size())
        {
            isContinue = true;
            break;
        }

        *offset = 0;
    }
}
while(isContinue);

The idea behind is like upcounting numbers (decimally): Once you've reached 9, you increment next digit. Alike, each offset in the vector stands for one loop variable, on 'overflow', increment next offset, and as soon as most significant offset 'overflows', we are done.

High performance variant (using goto, sparing one comparison and the condition variable):

std::vector<unsigned int> offsets(s.size());

NEXT:
for(auto offset : offsets)
{
    std::cout << s[offset];
}
std::cout << std::endl;

for(auto offset = offsets.rbegin(); offset != offsets.rend(); ++offset)
{
    if(++*offset < s.size())
        goto NEXT;

    *offset = 0;
}
like image 58
Aconcagua Avatar answered Sep 19 '22 00:09

Aconcagua