There is a problem like

I have 3 arrays i,j, & k ( the array count can be anything)

i j k
_____________
2 4 7
3 5 8
6 9

A combination has to be created like

(2,4,7) , (2,4,8) , (2,4,9) , (2,5,7) , (2,5,8) , (2,5,9) , (2,6,7) ,(2,6,8) , (2,6,9)

(3,4,7) , (3,4,8) , (3,4,9) , (3,5,7) , (3,5,8) , (3,5,9) , (3,6,7) ,(3,6,8) , (3,6,9)

But the problem here is , i will have the count of the no of arrays at runtime, but to get the combination described above , the depth of looping is not known , so how to create dynamic loops .
If possible through recursion, how ?Any idea

You can use foreach loops nested within each other for this.

Personally I'm not a fan of foreach (because it always gives me a headache).

I would do something like this:

string outCombos = "";
for (int a = 0; a < i.Length; a++) // cycle through array "i"
{
    for (int b = 0; b < j.Length; b++) // cycle through array "j"
    {
        for (int c = 0; c < k.Length; c++) // cycle through array "k"
        {
            // append new combination values to string "outCombos"
            outCombos += "(" + i[a].ToString() + "," + j[b].ToString() + "," + k[c].ToString() + "),";
        }
    }
}
outCombos.Remove(outCombos.Length - 2); // clips off the last character (",")

Hope this helps :) Please mark solved if this resolves your issue.

((I was going to post this an hour ago but my phone company decided it would be a good time to perform a "DSL Conversion" for my entire area effectively killing my phone, internet and TV for an hour :@ ))

((I was going to post this an hour ago but my phone company decided it would be a good time to perform a "DSL Conversion" for my entire area effectively killing my phone, internet and TV for an hour :@ ))

@Lusiphur:
Meanwhile the poor student was wondering how to do his homework. Ah well, you did it for him in the end.

commented: For pointing out that I'm good at doing peoples' homework for them :P +1

hahaha Too true... In this case I felt some pity (because this type of nested looping CAN be a bit of a headache for a newbie) but not in this case over here. :)

Hi , the real problem is that the number of array count is not known .
It is known only at runtime.
I know this solution given .
But how to you achieve this when array count is not known at coding.

Um... what I provided is how you achieve it when the count is not known at coding...

If you read my loops you see that they are generated based on variable figures (ie: i.Length) meaning that the loops will determine how many items are in the array and loop the according number of times to completely cover all iterations if the array.

If you knew the count ahead of time you'd do something more like

for (int a = 0; a < 16; a++)

instead of

for (int a = 0; a < i.Length; a ++)

Hope this helps make more sense of it :) Please remember to mark as solved if your issue is resolved.

I didn't mean count is Array count(not size of the array) but the number of different arrays(i,j,k,l,m,n etc ) for combination.

I see... well.. unless you have a collection of items (read: collection of arrays) this becomes difficult.

What I might recommend for you to do is look up setting up collection classes. What you will likely need to do is create a collection of arrays. The joy of collections is they can be expanded dynamically based on the number of items added to them so that at that stage your initial loop will become a loop through the collection to determine how many arrays there are and set up the required additional loops dynamically based on that information.

As for the coding of that... it's 4:17am and I'm not up for it :)

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.