1

I wanna create all possible 5 digit numbers that can be created from the numbers (0-7). The code below achieves this, but is there any way to make this depend on user input? The number of loops equals the number of digits I want and each individual loop must be:

for(1st number;condition<=last number;1st number++)

So, for five digits, I have:

for(i=0;i<8;i++){
    for(j=0;j<8;j++){
        for(k=0;k<8;k++){
            for(m=0;m<8;m++){
                for(n=0;n<8;n++){
                    printf("%d %d %d %d %d\n",i,j,k,m,n);                   
                }
            }
        }
    }
}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
shoban
  • 27
  • 5
  • 2
    You basically want to print all the `user_input`- length permutations of the range `[0,1,...,7]`. This might be a good start: https://stackoverflow.com/questions/16989689/print-all-the-permutations-of-a-string-in-c . – NadavS May 02 '20 at 22:11
  • I remember answering or reading an answer on similar question on this forum. Sure it's possible - keep state in an array, update element in the array, once it reaches `8` update the next one, loop, iterate. – KamilCuk May 03 '20 at 06:38

2 Answers2

1

If you want variable numbers of loops, you generally need to use recursion. Say if you want n digits, with the ith digit be in the range of a[i],b[i], then you will do the following:

/* whatever */

int n;
int *a,*b,*number;

void recursion(int whichdigit){
    if (whichdigit==n){
        /* Say you managed to output number */
        return;
    }
    for (int i=a[whichdigit];i<=b[whichdigit];i++){
        number[whichdigit]=i;
        recursion(whichdigit+1);
    }
    return;
}

int main(){
    /* Say somehow you managed to obtain n */
    a=malloc(n*sizeof(int));
    b=malloc(n*sizeof(int));
    number=malloc(n*sizeof(int))
    if (!a||!b||!number){
        /* unable to allocate memory */
    }
    /* Say somehow you managed to read a[i],b[i] for all i in 0..n-1 */
    recursion(0);
    return 0;
}

Warning: if you tries to have too many digits, you will likely get a segmentation fault or stack overflow error.

user12986714
  • 741
  • 2
  • 8
  • 19
1

Keep iterators in an array and increment them manually.

#include <assert.h>
#include <stdio.h>
#include <string.h>

void callback(unsigned n, int i[n]) {
     assert(n == 5);
     printf("%d %d %d %d %d\n", i[0], i[1], i[2], i[3], i[4]);
}

void iterate(unsigned n, unsigned max, void (*callback)(unsigned n, int i[n])) {
   // VLA, use *alloc in real code
   int i[n];
   memset(i, 0, sizeof(i));
   while (1) {
      for (int j = 0; j < n; ++j) {
         // increment first number, from the back
         ++i[n - j - 1];
         // if it didn't reach max, we end incrementing
         if (i[n - j - 1] < max) {
             break;
         }
         // if i[0] reached max, return
         if (j == n - 1) {
             return;
         }
         // if the number reaches max, it has to be zeroed
         i[n - j - 1] = 0;
      }
      // call the callback
      callback(n, i);
   }
}


int main() {
   // iterate with 5 numbers to max 8
   iterate(5, 8, callback);
}

The beginning and ending of what the code prints:

0 0 0 0 0
0 0 0 0 1
...
...
7 7 7 7 6
7 7 7 7 7
KamilCuk
  • 120,984
  • 8
  • 59
  • 111