Here's a good start on your problem:
#include <stdlib.h>
#include <stdio.h>
void partition(int n, int sum, int *summands, int num_summands)
{
int i;
if (sum == n) // base case of recursion
{
if (num_summands > 1) // don't print n by itself
{
for (i = 0; i < num_summands; ++i)
printf("%d ", summands[i]);
printf("\n");
}
}
else
{
/* TODO: fill in recursive case */
/* Iteratively recurse after appending one additional, varying summand to summands */
/* It might be easier to first generate all permutations of the sums */
/* and then figure out how to reduce that down to only the unique sets of summands (think sorting) */
}
}
int main(int argc, char **argv)
{
if (argc == 1)
{
printf("usage: %s <num>; where num > 1\n", argv[0]);
return 1;
}
int n = atoi(argv[1]);
if (n <= 1)
{
printf("usage: %s <num>; where num > 1\n", argv[0]);
return 1;
}
int summands[n+1]; // NOTE: +1's are to make summands[-1] always safe inside recursion
summands[0] = 1; // NOTE: make summands[-1] == 1 at top level of recursion
partition(n, 0, summands+1, 0); // NOTE: +1's are to make summands[-1] always safe inside recursion
return 0;
}
If you need a count of the sums you find, then you can add an extra parameter to partition
that is a pointer to (int) a count of the sums found so far. You'd only increment that count in your printing base case. In main, you'd pass a pointer to a zero initialized integer and in the recursion you'd just pass the pointer along. When you get back to main you can print the number of sums you found.