Solved

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

Posted on 2011-09-28
6
653 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
Comment Utility
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
Comment Utility
        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
Comment Utility
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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 4

Expert Comment

by:Ambusy
Comment Utility
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
Comment Utility
If my solution is correct, please accept it.
0
 

Author Closing Comment

by:TrialUser
Comment Utility
the solution works great. does exactly what I want
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

744 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now