# Generating every combination oif values from Jagged Array [Answered]RSS

## 4 replies

Last post Oct 31, 2012 05:22 AM by mayerwin

• chilluk

Member

404 Points

498 Posts

### Generating every combination oif values from Jagged Array

Oct 15, 2009 09:37 AM|chilluk|LINK

I have a scenario where I am trying to generate every combination of value from a jagged array - for example :

Array 1 :

Womens
Ladies
Womans

Array 2 :

Overalls
Boiler Suit
Boilersuit
Coveralls

So I want to generate 12 strings in total e.g :

Womens Overalls
Womens Boilersuit
...
Womans Coveralls

So all in first array associated with all in second array.

I have created my jagged array with the values but I can't work out how to iterate through it and create all variations - I can iterate, but only get each value once at present.

Also on a related note this could do with working in the case of more than 2 arrays - i.e. a multiple number of arrays within the jagged array.

Thanks

• SGWellens

All-Star

124308 Points

10142 Posts

Moderator

### Re: Generating every combination oif values from Jagged Array

Oct 15, 2009 10:06 AM|SGWellens|LINK

You want two loops, one nested inside the other.

The outer loop iterates the items in Array 1, the inner loop iterates over the items in Array 2.

Steve Wellens

My blog
• chilluk

Member

404 Points

498 Posts

### Re: Generating every combination oif values from Jagged Array

Oct 15, 2009 12:28 PM|chilluk|LINK

What if it's a flexible number of arrays - say 3 or 4 - I need a looping system that will draw out the values in each and create the resultant strings of all variations. One saving grace is that the combinations can be in order - i.e. the position in the resulting strings is dictated by which array is storing the value.

• Shengqing Ya...

All-Star

40457 Points

2952 Posts

### Re: Generating every combination oif values from Jagged Array

Oct 19, 2009 05:58 AM|Shengqing Yang - MSFT|LINK

#### chilluk

What if it's a flexible number of arrays - say 3 or 4 - I need a looping system that will draw out the values in each and create the resultant strings of all variations. One saving grace is that the combinations can be in order - i.e. the position in the resulting strings is dictated by which array is storing the value.

Hi,

It is hard to handle the case that if it's a flexible number of arrays. Please have a read at this article talking about Backtracking Algorithms: http://www.cse.ohio-state.edu/~gurari/course/cis680/cis680Ch19.html#QQ1-51-133. This will help.

Best Regards,
Shengqing Yang

Please mark the replies as answers if they help or unmark if not.
If you have any feedback about my replies, please contact msdnmg@microsoft.com.
Microsoft One Code Framework
• mayerwin

None

0 Points

1 Post

### Re: Generating every combination oif values from Jagged Array

Oct 31, 2012 05:22 AM|mayerwin|LINK

Hi, I have posted an algorithm that addresses these requirements here: http://erwinmayer.com/2010/12/14/recursive-algorithm-to-generate-all-combinations-of-elements-in-arrays/.