Link to home
Start Free TrialLog in
Avatar of tejs1dhu
tejs1dhu

asked on

Algorithm for combination generator

Hi

I'm wondering if you could give me a hand with an algorithm.

I need to write a algorithm for generating combinations from n number of sets (of integers).

I can write code that will take my fixed number of sets and produce the combinations but Im lost on how I would be able to do this for n number of sets.

I've attached code for generating combinations from a fixed number of sets.

Will appreciate anyones help or guidance.

Thanks
int[] firstSet = new int[] { 1, 2, 3, 4, 5 };
        int[] secondSet = new int[] { 6, 7, 8, 9, 10, 11 };
        int[] thirdSet = new int[] { 12, 13, 14 };
        int[] fourthSet = new int[] { 15, 16, 17, 18 };
        int counter = 0;
 
        foreach (int firstSetVariable in firstSet)
        {
            foreach (int secondSetVariable in secondSet)
            {
                foreach (int thirdSetVariable in thirdSet)
                {
                    foreach (int fourthSetVariable in fourthSet)
                    {
                        Response.Write(counter + ": " + firstSetVariable + "," + secondSetVariable + "," + thirdSetVariable + "," + fourthSetVariable + "<br />");
                        counter += 1;
                    }
                }
            }
        }

Open in new window

Avatar of Infinity08
Infinity08
Flag of Belgium image

If n isn't too big, you could make the algorithm recursive ...
Avatar of tejs1dhu
tejs1dhu

ASKER

Hi Infinity08

Making this recursive is the aim, however I am not sure how to implement this using C#.  Any ideas?
ASKER CERTIFIED SOLUTION
Avatar of Infinity08
Infinity08
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial