I'm having difficulty finding the underlying logic for this. I know recursion would help however I don't know how to approach it. My difficulty lies when get dealing with sending/ printing the array on the base case. Everything I do I can only iterate the last digit and don't know how to go about the others.
If n is 2:
$>./a.out | cat -e
01, 02, 03, ..., 09, 12, ..., 79, 89$
If n is 3 things get harder:
012, 013, 014, ..., 123, 124, ..., 134, 135,... 234, ..., 789$
My code is far from working
#include <unistd.h>
#include <stdlib.h>
#include <stdbool.h>
void ft_putchar(char c)
{
write(1, &c, 1);
}
bool is_consecutive(int arr[], int n)
{
int last_value = 9;
if ( n <= 0 )
return false;
while ( --n ) {
if ( arr[n] != last_value-- )
return false;
}
return true;
}
void ft_print_screen(int *t, int size)
{
int i;
i = 0;
while (i < size)
{
ft_putchar(t[i] + '0');
i++;
}
if (is_consecutive(t, size) != true)
{
ft_putchar(',');
ft_putchar(' ');
}
}
void ft_print_combn(int n)
{
int i;
int tab[n];
i = 0;
if (n == 1)
while (i < 10)
{
ft_putchar(i + '0');
i++;
}
while (i < n)
{
tab[i] = 0;
i++;
}
while (tab[0] <= (10 - n) && n > 1)
{
ft_print_screen(tab, n);
tab[n - 1]++;
i = n;
while (i && n > 1)
{
i--;
if (tab[i] > 9)
{
tab[i - 1]++;
tab[i] = 0;
}
}
}
}
void main(int argc, char *argv[])
{
int x = atoi(argv[1]);
ft_print_combn(x);
}