Hi,
I have to write a recursive function that prints all the partitions for a given integer number,for example, the patitions of 4 are:
4
3 1
2 2
2 1 1
1 1 1 1
I created a function but there is a little problem that I can not realize. The output that I receive is:
4
3 1
2 2
1 1 <-- !!!!!!!
1 1 1 1
It seem like it misses a part of calculation.
Can somebody help me find the problem in my function or suggest me altrnative one.
My function:
Code:
void Partition(int n, int limit, int value[Npartition])
{
int i, k=1, min;
if (n < limit)
min = n;
else
min = limit;
if (n>0)
{
for (i=min; i>0; i--)
{
for (k=0; k<Npartition; k++)
{
if (value[k] == 0)
{
value[k] = i;
break;
}
}
Partition(n-i, i, value);
}
}
else
{
for (i=0; i<Npartition; i++)
if (value[i] != 0)
printf("%2d", value[i]);
printf("\n");
for (i=0; i<Npartition; i++)
value[i] = 0;
}
}
Initial function call is Partition(N, N, ValueArray)
Thank you.