Home > Back-end >  Is there an alternative on nested loop for displaying possible combination in range of n?
Is there an alternative on nested loop for displaying possible combination in range of n?

Time:09-27

I created some code where, whenever you put number in pinCombo(x) (for example pinCombo(3)), the output will be:

000
001
002

… until it reaches 999.

So, pinCombo(4) output will be:

0000
0001
....
....
9999

Here's my code:

#include <iostream>

using namespace std;

void pinCombo(int x)
{
    int a,b,c,d,e,f,g,h,i,j;
    
    if(x>=1)
    for (a = 0;a<10;a  )
    {
        if(x>=2)
        for (b = 0;b<10;b  )
        {
            if(x>=3)
            for (c = 0;c<10;c  )
            {
                if(x>=4)
                for (d = 0;d<10;d  )
                {
                    if(x>=5)
                    for (e = 0;e<10;e  )
                    {
                        if(x>=6)
                        for (f = 0;f<10;f  )
                        {
                            if(x>=7)
                            for (g = 0;g<10;g  )
                            {
                                if(x>=8)
                                for (h = 0;h<10;h  )
                                {
                                    if(x>=9)
                                    for (i = 0;i<10;i  )
                                    {
                                        
                                        if(x>=10)
                                        for (j = 0;j<10;j  )
                                        {
                                            cout<<a<<b<<c<<d<<e<<f<<g<<h<<i<<j<<endl;
                                        }if(x==9)cout<<a<<b<<c<<d<<e<<f<<g<<h<<i<<endl;
                                    }if(x==8)cout<<a<<b<<c<<d<<e<<f<<g<<h<<endl;
                                }if(x==7)cout<<a<<b<<c<<d<<e<<f<<g<<endl;
                            }if(x==6)cout<<a<<b<<c<<d<<e<<f<<endl;
                        }if(x==5)cout<<a<<b<<c<<d<<e<<endl;
                    }if(x==4)cout<<a<<b<<c<<d<<endl;
                }if(x==3)cout<<a<<b<<c<<endl;
            }if(x==2)cout<<a<<b<<endl;
        }if(x==1)cout<<a<<endl;
    }
    
}

using namespace std;

int main()
{
pinCombo(3);
return 0;
}

Is there a way to create a program like this without using nested loops or without using many variables?

CodePudding user response:

You can achieve this using % operator:

#include <iostream>
#include <cmath>

void pinCombo(int x)
{
    int* digits = new int[x];
    int limit = std::pow(10, x);
    
    for (int n = 0; n < limit; n  ) {
        int nn = n;
        
        for (int i = 0; i < x; i  ) {
            digits[i] = nn % 10;
            nn /= 10;
        }
        
        for (int i = x-1; i >= 0; i--) {
            std::cout << digits[i];
        }
        
        std::cout << "\n";
    }

    delete[] digits;
}

int main()
{
    pinCombo(3);
}

This will output:

000
001
002
...
999

EDIT: You can achieve the same thing using <iomanip> facilities:

void pinCombo(int x)
{
    int limit = std::pow(10, x);
    for (int i = 0; i < limit; i  )
        std::cout << std::setw(x) << std::setfill('0') << i << std::endl;
}

Side note: It's considered a bad practice to use using namespace std because it pollutes the global namespace with everything you include from the C standard. Either use std:: prefix, or using std::<whataver_you_want> if you prefer shortcuts.

CodePudding user response:

Unless I'm missing something, its as short as...

#include <cmath>
#include <string>
#include <iostream>

int main()
{
    unsigned int numDigits;

    std::cout << "Enter number of digits: ";
    std::cin >> numDigits;

    auto maxNum = std::powl(10, numDigits);
    std::string zeros(numDigits, '0');

    for (unsigned i = 0; i < maxNum; i  )
    {
        std::string combination = std::to_string(i);
        std::cout << (zeros   combination).substr(combination.length(), numDigits) << '\n';
    }
}
  • Related