I'm trying to write a function that print a positional numeric system with given base and number of digits. For example, with Base = 2 and nDigits = 3 the output have to be like this:
000
001
010
011
...
111
Now, I've tried to do something but I've only failed. Consider that I can't store the numbers, I just have to print them, that's why I use a dynamic allocation of a 'support' array. That's what I tried to do so far, obviously not doing what it's intended to... I think the only correct part is to print all the combinations for Base^nDigits. (b=base, n=nDigits).
void printNumeration(int b, int n){
int i, j=0;
int *array = calloc(n, sizeof(int));
if (array == NULL){
printf("Allocation failed.\n");
exit(0);
}
for (i=0; i<pow(b, n); i++){
for (j=0; j<n; j++){
printf("%d", array[j]);
array[j]++;
if (array[j] == n){
array[j] = 0;
}
}
printf("\n");
}
}
You can also give some tips about a better solution if mine is completely wrong.