Hey everyone!

I've been trying for a couple of days now to make a function and it's driving me crazy.

I need a function that takes a set of numbers in an array, for example:

int array[]={4,3,5,7}

and produce another array with all possible combinations of those numbers, or just print all the combinations. Like this:

4
43
45
47
435
437
457
4357
3
35
37
357
5
57
7

...not necessarily in that order.

I tried doing it with loops and it became too complex, then I tried recursion and it got even more complex. Anybody have any ideas?

Recommended Answers

All 8 Replies

> int array[]={4,3,5,7}
Think about recursion in terms of
4 + all combinations of 3,5,7
3 + all combinations of 4,5,7
..
..

Which becomes in the recursive step
4 + 3 + all combinations of 5,7
4 + 5 + all combinations of 3,7

When you're down to all combinations of just one value, that's when you stop.

commented: More power pills than pacman...:D - ~s.o.s~ +15
commented: Wow... I never thought of that --joeprogrammer +8

Yeah, I think that recursion is the only way to do it, I'm just not very good at it.

Your concept is fine, except that I don't need a sum of the numbers, just a list.

Grr... I hate recursion.

It's not a literal sum, more like a concatenation

4 + 3 + all combinations of 5,7
is
4,3,5,7
4,3,7,5

Gotcha.

I did a little work on it and so far this is what I got. For now I'm just trying to print each combo on a different line.

int arr[]={3,5,3,7,4},n=5;
 
void combos(int pos)
{
int i;
printf("%d",arr[pos]);
if(pos<n)
for(i=pos+1;i<n;i++)
{
printf("\n%d",arr[pos]);
combos(i);
}
else printf("%d",arr[pos]);
}
 
int main()
{
char c;
products(0);
scanf("%c",&c);
return 0;
}

I think I'm on the right track but I'm definetely missing something. Any ideas?

Sorry, double post. Don't ask me how it happened. :confused:

Anybody got any ideas? I'm extremely stuck here...

Gotcha.

I did a little work on it and so far this is what I got. For now I'm just trying to print each combo on a different line.

int arr[]={3,5,3,7,4},n=5;
 
void combos(int pos)
{
int i;
printf("%d",arr[pos]);
if(pos<n)
for(i=pos+1;i<n;i++)
{
printf("\n%d",arr[pos]);
combos(i);
}
else printf("%d",arr[pos]);
}
 
int main()
{
char c;
products(0);
scanf("%c",&c);
return 0;
}

I think I'm on the right track but I'm definetely missing something. Any ideas?

#1) Format your code. It helps readability and understanding.
#2) The function products() doesn't exist.
#3) The function combos() is never called
#4) See this about scanf() and characters. Look at the entire scanf() series.
#5) putchar() a '\n' when you output the last value of the combo. No need for a printf() for a single character. Ever.

Haha, you're quite right! I took care of all of those probs. But I have a feeling my code is way off.

int arr[]={3,5,3,7,4},n=5;
 
void combos(int pos)
{
        int i;
        printf("%d",arr[pos]);
        if(pos<n)
        for(i=pos+1;i<n;i++)
        {
                printf("\n%d",arr[pos]);
                combos(i);
        }
        else printf("%d",arr[pos]);
}
 
int main()
{
        combos(0);
        getchar();
        return 0;
}

Any suggestions would be great.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.