Solved

generating a series for all 2 power n combinations in vb.net

Posted on 2011-09-28
6
667 Views
Last Modified: 2012-05-12
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.
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

1 1 1
1 1 2
1 2 1
2 1 1
2 1 2
1 2 2
2 2 1
2 2 2
0
Comment
Question by:TrialUser
  • 3
  • 2
6 Comments
 
LVL 12

Assisted Solution

by:jagssidurala
jagssidurala earned 100 total points
ID: 36719097
Logic is

2^0 2^0 2^0
2^0 2^0 2^1
2^0 2^1 2^0
2^0 2^1 2^1
2^1 2^0 2^0
2^1 2^0 2^1
2^1 2^1 2^0
2^1 2^1 2^1

The power to the num 2 is binary seris

0
 
LVL 4

Accepted Solution

by:
Ambusy earned 400 total points
ID: 36719344
        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) > 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 & CStr(Values(j))
            Next
            Debug.WriteLine(Result)
        Next

Open in new window

0
 

Author Comment

by:TrialUser
ID: 36814309
Thank you both for the suggestoins. Both the suggestions are great and made me understand the concept behind.

Ambusy, the code you provided just works great and does exactly what I wanted.

Just one question, if I change     HighestValue , and NrOfValues  then this logic should just work for any number of items and any number of possible values for each item right?

thanks again.
0
ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

 
LVL 4

Expert Comment

by:Ambusy
ID: 36817258
Exactly, that's why I wrote them as variables. I tested them with values in a textbox to be certain it workes.
0
 
LVL 4

Expert Comment

by:Ambusy
ID: 36896300
If my solution is correct, please accept it.
0
 

Author Closing Comment

by:TrialUser
ID: 36948593
the solution works great. does exactly what I want
0

Featured Post

ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question