Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I presented a solution in Experts Exchange, which you may not be able to see (if you never payed them) so I copy it for you:</p> <p>Question was: I have n items and each item can be assigned a 1 or a 2. So I would like to get the matrix result that would generate all possible combinations.</p> <p>For eg. if n= 3 , then the possible outcomes are : I need an algorithm that can generate this series for n . Please help thanks. ideally i would like to store the result in a datatable</p> <pre><code> 1 1 1 1 1 2 1 2 1 2 1 1 2 1 2 1 2 2 2 2 1 2 2 2 </code></pre> <p>Answer:</p> <pre><code> Dim HighestValue As Integer = 2 ' max value Dim NrOfValues As Integer = 3 ' nr of values in one result Dim Values(NrOfValues) As Integer Dim i As Integer For i = 0 To NrOfValues - 1 Values(i) = 1 Next Values(NrOfValues - 1) = 0 ' to generate first as ALL 1 For i = 1 To HighestValue ^ NrOfValues Values(NrOfValues - 1) += 1 For j As Integer = NrOfValues - 1 To 0 Step -1 If Values(j) &gt; HighestValue Then Values(j) = 1 Values(j - 1) += 1 End If Next Dim Result As String = "" For j As Integer = 0 To NrOfValues - 1 Result = Result &amp; CStr(Values(j)) Next Debug.WriteLine(Result) Next </code></pre> <p>Ok Here's the solution, you just need to change the Debug.Writeline with a write to your file</p> <pre><code> Dim HighestValue As Integer = 3 ' max value Dim NrOfValues As Integer = 3 ' nr of values in one result Dim Values(NrOfValues) As Integer Dim i As Integer For i = 0 To NrOfValues - 1 Values(i) = 1 Next Values(NrOfValues - 1) = 0 ' to generate first as ALL 1 For i = 1 To HighestValue ^ NrOfValues Values(NrOfValues - 1) += 1 For j As Integer = NrOfValues - 1 To 0 Step -1 If Values(j) &gt; HighestValue Then Values(j) = 1 Values(j - 1) += 1 End If Next Dim Result As String = "" For j As Integer = 0 To NrOfValues - 1 If Values(j) = 1 Then Result = Result &amp; "d" If Values(j) = 2 Then Result = Result &amp; "o" If Values(j) = 3 Then Result = Result &amp; "n" 'Result = Result &amp; CStr(Values(j)) Next Debug.WriteLine(Result) Next </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload