I need to iterate over an array of n
elements (dynamic size thus), for example 4, as if it were a binary number, starting with all zeros ({0,0,0,0}
). Every time this needs to increment as if it is binary, {0,0,0,0}
-> {0,0,0,1}
-> {0,0,1,0}
-> {0,0,1,1}
-> {0,1,0,0}
, etc...
I have trouble generating an algorithm that does so with array values, I thought about using recursion but cannot find the method to do so other than hard-coding in ifs. I suppose I could generate an n
-digit number and then apply any algorithm discussed in this post, but that would be inelegant; the OP asked to print n-digit numbers while I need to work with arrays.
It would be great if someone could point me in the right direction.
Context:
int n;
scans("%d", &n);
int *arr = calloc(n, sizeof(int));