Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
Solved

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

Posted on 2011-09-28
Medium Priority
707 Views
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
Question by:TrialUser
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
• 3
• 2

LVL 12

Assisted Solution

jagssidurala earned 400 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

Ambusy earned 1600 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
``````
0

Author Comment

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

LVL 4

Expert Comment

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

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

Author Closing Comment

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

## Featured Post

Question has a verified solution.

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

If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
###### Suggested Courses
Course of the Month6 days, 8 hours left to enroll