-
Sep 3rd, 2020, 01:24 PM
#1
Thread Starter
Member
All possible combinations in an integer array
Hi, i need to find a way of finding all possible combinations of an array full of numbers.
Have had a look but can't really find a true way of finding this anywhere, and i need it to not have repetition.
You guys did help out before but it did not really work as it had an element of randomisation to it.
Any help would be very much appreciated as i am not too hot with programming in general so please forgive this in advanced :-)
Cheers
-
Sep 3rd, 2020, 01:57 PM
#2
Re: All possible combinations in an integer array
The keyword that you're looking for is "permutation". A quick search on any search engine yields quite a bit of results. I just double-checked VBForums in the VB.NET Codebank and couldn't find an example (which was surprising), but there are a number of threads in the VB.NET Forum.
-
Sep 3rd, 2020, 02:16 PM
#3
Re: All possible combinations in an integer array
You also need to tell us a bit more about this array. Permutations can be a bit interesting when it comes to numbers. For example, if you wanted the permutations of all the digits from 0-9, then there is a simple way to do it. Getting rid of those with repeats makes it more difficult, and it gets more difficult if the array has only some digits, while getting weird if the array has numbers greater than 9.
My usual boring signature: Nothing
-
Sep 3rd, 2020, 03:27 PM
#4
Thread Starter
Member
Re: All possible combinations in an integer array
HI Shaggy, so the array will be very simple, just a normal array that consists of a set of numbers that i can change if needed like, {200,300,100,250} etc.
And then i need to basically test all possible combinations of them all without repetition if possible. I do work in I.T but not in a coding position as you can probably gather lol, but i did ask somebody who knows a fair bit about it and he seems to think it is not possible in VB.Net?
Cheers
Barney
-
Sep 3rd, 2020, 03:50 PM
#5
Re: All possible combinations in an integer array
Originally Posted by Veins333
HI Shaggy, so the array will be very simple, just a normal array that consists of a set of numbers that i can change if needed like, {200,300,100,250} etc.
And then i need to basically test all possible combinations of them all without repetition if possible. I do work in I.T but not in a coding position as you can probably gather lol, but i did ask somebody who knows a fair bit about it and he seems to think it is not possible in VB.Net?
Cheers
Barney
First thing is to stop going to that guy for advice.
Second thing is, given your example set, provide the exact results you are looking for. For example, you originally said combinations (order doesn't matter), and dday inserted the word permutations (order matters), which are two very different things.
You also haven't indicated if you are looking for subsets. For example, in the example set you provided, is {200} one of the results you are looking for? Or are you just looking for results using all given numbers and in all possible orders?
Given {1, 2, 3}.
Are you looking for:
{1}
{2}
{3}
{1, 2}
{1, 3}
{2, 3}
{1, 2, 3}
or:
{1, 2, 3}
{1, 3, 2}
{2, 1, 3}
{2, 3, 1}
{3, 1, 2}
{3, 2, 1}
or:
{1}
{2}
{3}
{1, 2}
{1, 3}
{2, 1}
{2, 3}
{3, 1}
{3, 2}
{1, 2, 3}
{1, 3, 2}
{2, 1, 3}
{2, 3, 1}
{3, 1, 2}
{3, 2, 1}
???
Last edited by OptionBase1; Sep 3rd, 2020 at 03:53 PM.
-
Sep 3rd, 2020, 03:56 PM
#6
Re: All possible combinations in an integer array
Sure, it's possible. There is a question of whether or not it is feasible, though. Permutations can explode very fast. If you really only want permutations of four items, then it will be manageable....though for just four items, you might as well just write them out. Presumably, you really intend this for more than just four items, and that's when things start to explode.
Consider that what you want is permutations of letters {A, B, C....etc}. For two, you get:
AB
BA
For three, you get:
ABC
ACB
BAC
BCA
CAB
CBA
So, with two you have two items, with three your have six items, and so on. The number of permutations is N!, so as N increases, the number of permutations goes wild:
N = 2 : 2
N = 3 : 6
N = 4 : 24
N = 5 : 96
and so on. As you can see, the numbers are growing fast, so you have to have a pretty small number of items or else you'll end up with a pretty large number of permutations. In fact, you can easily end up with a set of permutations that is unusable. After all, there has to be a reason to come up those permutations, even if the reason is just that you want some really unusual wallpaper. Once you have an N of 10, or so, just seeing all the permutations will be a letter salad for your eyes. Working with the permutations to do anything meaningful might become impossible, too. For example, if you want to pass each permutation to some algorithm, how long does the algorithm take to work each permutation? Can it really handle the number of permutations you are going to throw at it in a reasonable amount of time?
So, take the number of elements in your array, figure the factorial, and that will be the number of permutations you will have. For whatever purpose you intend with them, is that a reasonable number to work with, or will you be waiting forever? I ask this because when people ask for permutations, they often want a number of permutations, or want to do something with all those permutations, which would take longer than the expected lifespan of the sun. Now you can calculate the number of permutations you will end up with, is that workable?
My usual boring signature: Nothing
-
Sep 3rd, 2020, 04:25 PM
#7
Thread Starter
Member
Re: All possible combinations in an integer array
Hi Option, yes my bad for that, i did forget to mention that i need to test if any of the combinations match to a target, say 500, or a variable that i can change the value in.
-
Sep 3rd, 2020, 04:27 PM
#8
Thread Starter
Member
Re: All possible combinations in an integer array
Hey Shaggy, speed is not important, i dont care if it sits there chugging through its only for me to mess about with.
-
Sep 3rd, 2020, 08:54 PM
#9
Re: All possible combinations in an integer array
Originally Posted by Veins333
Hi Option, yes my bad for that, i did forget to mention that i need to test if any of the combinations match to a target, say 500, or a variable that i can change the value in.
Just a bit more clarification, if the target number is 51
then an array of {"1","3","5"} would match the target "5" & "1"
then an array of {"11","3","51"} would match the target "51"
or is it an array of {10, 1, 40} would match your target 40+10+1 = 51
Big difference on how these are programmed.
-
Sep 3rd, 2020, 09:09 PM
#10
Hyperactive Member
Re: All possible combinations in an integer array
here is code for permutations you'll have to modify as needed
Code:
Public Class Form1
Sub Permute(ByVal nums() As Integer, Optional ByVal start As Integer = 0)
If start = nums.Length Then
OutputArray(nums)
Else
For i As Integer = start To nums.Length - 1
Swap(nums, start, i)
Permute(nums, start + 1)
Swap(nums, start, i)
Next
End If
End Sub
Sub Swap(ByRef nums() As Integer, ByVal i As Integer, ByVal j As Integer)
Dim temp As Integer
temp = nums(i)
nums(i) = nums(j)
nums(j) = temp
End Sub
Sub OutputArray(ByVal nums() As Integer)
For Each num As Integer In nums
TextBox1.Text += (num.ToString & " ")
Next
TextBox1.Text += vbCrLf
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Permute({1, 2, 3, 4})
End Sub
End Class
this is not my code just a copy I saved to play with
-
Sep 3rd, 2020, 09:48 PM
#11
Hyperactive Member
Re: All possible combinations in an integer array
this code will give you all the possible permutations
Code:
Public Class Form1
Dim vChar() As Char
Dim vIndex() As Int32
Dim N, r, cnt As Int32
'Sub Main()
' Try
' Console.Write("How many objects? ")
' N = Int32.Parse(Console.ReadLine)
' Console.Write("How many taken at a time? ")
' r = Int32.Parse(Console.ReadLine)
' If r >= N Then Exit Sub
' Console.WriteLine("")
' permutationsNtakenR()
' Console.ReadLine()
' Catch ex As Exception
' End Try
'End Sub
Sub permutationsNtakenR()
Try
Dim i As Int32
ReDim vChar(N - 1), vIndex(r - 1)
For i = 0 To N - 1
vChar(i) = ChrW(AscW("A") + i)
Next
Dim NPerm As Int32 = N
For i = N - r + 1 To N - 1
NPerm *= i
Next
Dim i1 As Int32
Do
If check(i1) Then
cnt += 1
Dsp()
i1 = r - 1
End If
vIndex(i1) += 1
Do While vIndex(i1) = N
vIndex(i1) = 0
i1 -= 1
If i1 < 0 Then
Exit Do
End If
vIndex(i1) += 1
Loop
Loop While i1 >= 0
If cnt <> NPerm Then
'Console.WriteLine("Something went wrong")
End If
Catch ex As Exception
End Try
End Sub
Function check(ByRef ind As Int32) As Boolean
For i As Int32 = 0 To r - 1
For j As Int32 = i + 1 To r - 1
If vIndex(j) = vIndex(i) Then
ind = j
Return False
End If
Next
Next
Return True
End Function
Sub Dsp()
Dim str As String = String.Empty
For i = 0 To vIndex.Length - 1
str += vChar(vIndex(i))
Next
TextBox1.Text += (str + vbTab)
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Try
'Console.Write("How many objects? ")
N = 5
'Console.Write("How many taken at a time? ")
For a = 1 To N
r = a
'If r >= N Then Exit Sub
permutationsNtakenR()
Next
Catch ex As Exception
End Try
End Sub
End Class
again not my code
-
Sep 4th, 2020, 05:00 AM
#12
Re: All possible combinations in an integer array
Careful! In mathematics, permutations and combinations are not the same thing.
Suppose your set of things to choose from is: {1, 2, 3, 4, 5}
Then {1, 3, 5} is the same combination as {3, 5, 1}
but {1, 3, 5} is a different permutation to {3, 5, 1}.
That means that there are many more permutations possible than combinations. The order of the selected values matters in permutations, but not in combinations.
I'm not sure whether the code suggested so far provides the result you want. But if programmers don't know the difference between combinations and permutations, they could make some very nasty errors.
BB
-
Sep 4th, 2020, 05:30 AM
#13
Thread Starter
Member
Re: All possible combinations in an integer array
Hi Wes, so for what i need, it would be the second array, [11,3,51] would match the target if it was 51.
-
Sep 4th, 2020, 12:10 PM
#14
Re: All possible combinations in an integer array
Originally Posted by Veins333
Hi Wes, so for what i need, it would be the second array, [11,3,51] would match the target if it was 51.
Then you've been asking the wrong question. It has nothing to do with combinations, you want to know if a number is contained in the array. Completely different and very easy.
Code:
Dim arr = {1, 3, 5}
Dim num = 3
MessageBox.Show(arr.Contains(num).ToString)
-
Sep 5th, 2020, 04:34 PM
#15
Thread Starter
Member
Re: All possible combinations in an integer array
Hi, no sorry you i did not explain correctly. IF the target was 51 then it would be that, but if the target was 54 then it would be the combination of 3 and 51.
I want to test all the different combinations of the numbers held in the array to a set target if that makes sense.
-
Sep 5th, 2020, 07:52 PM
#16
Re: All possible combinations in an integer array
Originally Posted by Veins333
Hi, no sorry you i did not explain correctly. IF the target was 51 then it would be that, but if the target was 54 then it would be the combination of 3 and 51.
I want to test all the different combinations of the numbers held in the array to a set target if that makes sense.
Like one of the choices I gave before,
Code:
or is it an array of {10, 1, 40} would match your target 40+10+1 = 51
No your not being very clear and not putting a lot effort in trying. Now it sounds like you want to know all the numeric values that can be created from an array.
{1,2}
Possible values, 1, 2, 3
-
Sep 5th, 2020, 08:04 PM
#17
Re: All possible combinations in an integer array
Originally Posted by wes4dbt
Like one of the choices I gave before,
Code:
or is it an array of {10, 1, 40} would match your target 40+10+1 = 51
No your not being very clear and not putting a lot effort in trying. Now it sounds like you want to know all the numeric values that can be created from an array.
{1,2}
Possible values, 1, 2, 3
I had to bow out because I took the time to ask a very specific question with three complete examples seeking clarification of what, exactly, the OP was looking for and got a single sentence reply that didn't address anything I had asked.
My mindset is, if I need to spend more time seeking clarification from an poster for what, exactly, they are trying to accomplish than the poster appears to have put in to actually trying to explain and/or solve the problem themselves, then I move on.
-
Sep 6th, 2020, 05:15 AM
#18
Thread Starter
Member
Re: All possible combinations in an integer array
Not really hard to understand, basically i need to know if any of the numbers in an array, if combined or on their own add up to the value of a target.
-
Sep 6th, 2020, 05:16 AM
#19
Thread Starter
Member
Re: All possible combinations in an integer array
Cool, no need to get stressed about it if you don't know how to do it.
-
Sep 6th, 2020, 03:45 PM
#20
Re: All possible combinations in an integer array
Originally Posted by Veins333
Cool, no need to get stressed about it if you don't know how to do it.
I don't think it is a case of people getting stressed about not knowing how to do what you are asking, it is about how long it is taking for you to actually clarify exactly what you want.
When trying to solve a programming problem the code is the last part of the process, defining and understanding the problem is the first step - if you can't explain the problem clearly to someone else it is often a good indicator that you don't really understand the problem yourself. Figuring out what you actually want and being able to explain what you are looking for clearly and concisely makes it easier for everyone, you might be able to get to the answer yourself, other people will at least know what they are trying to help you with.
-
Sep 6th, 2020, 04:38 PM
#21
Thread Starter
Member
Re: All possible combinations in an integer array
What's so hard and not clear about what i said?
I need to find out if any possible combinations of the values in an integer array add to up a given target?
What's so hard to understand about that?
If people don't know the answer or the way to do it fine, no big deal but there is not need to get all defensive about it, i'm not having a go at anybody about it, seems that they are getting all pissed off about it because they think i'm questioning there intelligence or programming skills when i did nothing of the sort.
-
Sep 6th, 2020, 04:51 PM
#22
Re: All possible combinations in an integer array
Originally Posted by Veins333
What's so hard and not clear about what i said?
I need to find out if any possible combinations of the values in an integer array add to up a given target?
What's so hard to understand about that?
If people don't know the answer or the way to do it fine, no big deal but there is not need to get all defensive about it, i'm not having a go at anybody about it, seems that they are getting all pissed off about it because they think i'm questioning there intelligence or programming skills when i did nothing of the sort.
If it was so simple and easy to understand why did it take until post #15 (the 6th of your posts in this thread) for you to actually explain that? People are getting frustrated because they are trying to solve the problem you are asking and every time a solution is offered you then add another bit of clarification which negates all the effort people have made so far. How hard would it of been for you to state the problem the way you have just done, or to have given the examples that you gave in post #15 and #13 in the initial post?
-
Sep 6th, 2020, 04:54 PM
#23
Thread Starter
Member
Re: All possible combinations in an integer array
Well they seemed to understand me from the 1st and 3rd post. Why are you even commenting if you have no solution.
What's your problem? If u don't understand it or i annoy you, just move on, end of.
Last edited by Veins333; Sep 12th, 2020 at 04:31 AM.
-
Sep 7th, 2020, 01:27 AM
#24
Re: All possible combinations in an integer array
Originally Posted by Veins333
Well they seemed to understand me from the 1st and 3rd post. Why are you even commenting if you have no solution.
What's your problem? If u don't understand it or i annoy you, just move on, end or.
We'll see how far that approach gets you. I'm done with this thread.
-
Sep 7th, 2020, 05:44 AM
#25
Thread Starter
Member
Re: All possible combinations in an integer array
Probably about as far i have got so far lol.
Stressing and getting mad about something like this is crazy, i can't help it if you can't understand what i put, it's not the hardest thing to get.
If you talk to people the way you do don't start crying when you get it back.
-
Sep 7th, 2020, 05:51 AM
#26
Thread Starter
Member
Re: All possible combinations in an integer array
And this one for example, i said 51 - fifty one , if i meant 5 & 1 then i would have put it wouldn't have i?
You love to point out that there are big differences and that i do not explain stuff with enough clarity, but never actually offer a solution and rather try and degrade my intelligence of how i explain things rather that you not being able to read.
-
Sep 7th, 2020, 04:50 PM
#27
Re: All possible combinations in an integer array
This just works with single characters, but you could use variable names...
Dim A as Integer = 36
Dim B as Integer = 101
Dim C as integer = 51
Dim D as Integer = 15
' etc
http://www.scproject.biz/MRV2.0.php#8
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Sep 8th, 2020, 05:20 AM
#28
Re: All possible combinations in an integer array
Originally Posted by .paul.
The Combinations example shown in the above link gets it wrong. ABCD, ACBD, ADBC ... etc. are all permutations of the same four letters. The only difference between them is the order of the letters.
The 15 different (non-empty) combinations of 4 items {A, B, C, D} are:
4C4: {A, B, C, D}
4C3: {A, C, D}, {A, B, D}, {A, B, C}, {B, C, D}
4C2: {A, B}, {A, C}, {A, D}, {B, C}, {B, D}, {C, D}
4C1: {A}, {B}, {C}, {D}
Here, the order of the letters is irrelevant.
The letters are just for readability. For the purpose of comparing the sum of each combination to a given integer value, the collections should of course be of Integers rather than Strings.
BB
Last edited by boops boops; Sep 8th, 2020 at 05:24 AM.
-
Sep 8th, 2020, 07:41 AM
#29
Re: All possible combinations in an integer array
Originally Posted by Veins333
Hi Option, yes my bad for that, i did forget to mention that i need to test if any of the combinations match to a target, say 500, or a variable that i can change the value in.
So, you only need to test if any combination adds up to the target, so once you find one combination that does, you're done.
Or did you mean you need to find all combinations that add up to the target, in which case there could be none, but there could be more than one?
"Anyone can do any amount of work, provided it isn't the work he is supposed to be doing at that moment" Robert Benchley, 1930
-
Sep 12th, 2020, 04:30 AM
#30
Thread Starter
Member
Re: All possible combinations in an integer array
Hi Passel,
So, i have an array, integer, and i want to be able to change the values in that array so for example it could be {1,3,5,20,30} etc and then i have a target of say 500, all i need to do is see if any combinations of the values in the array add up to the target. So in the example above none of them would, so it would test 1 + 3 = target or 3 + 5 + 20 = target etc, that's all really.
Basically, do any of the values in the array, combined or on their own add up to the target. so lets say the target was 35 for example, then 5 + 30 would match the target.
I did have something like this in Java but i have lost the program and i can't find a way to do it in VB.net.
-
Sep 12th, 2020, 02:35 PM
#31
Re: All possible combinations in an integer array
A full and clear explanation (with and example) really makes it easier for us to help.
This does what you want, add a multi-line textbox to a form and a button. The text but will show all possible numbers.
Code:
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim array() As Integer = {1, 22, 33, 36}
Dim subsets As List(Of Integer()) = CreateSubsets(array)
Dim targetNumber = 34
Dim isTarget = False
For Each subset As Integer() In subsets
Dim sum As Integer = subset.Sum()
If sum = targetNumber Then
isTarget = True
End If
If Not Me.TextBox1.Lines.Contains(sum.ToString) Then
Me.TextBox1.AppendText(sum.ToString & vbNewLine)
End If
Next
If isTarget Then
MessageBox.Show("Number found in array: " & targetNumber)
Else
MessageBox.Show("Target number not found")
End If
End Sub
Function CreateSubsets(Of T)(ByVal originalArray() As T) As List(Of T())
Dim subsets As New List(Of T())
For i As Integer = 0 To originalArray.Length - 1
Dim subsetCount As Integer = subsets.Count
subsets.Add(New T() {originalArray(i)})
For j As Integer = 0 To subsetCount - 1
Dim newSubset(subsets(j).Length) As T
subsets(j).CopyTo(newSubset, 0)
newSubset(newSubset.Length - 1) = originalArray(i)
subsets.Add(newSubset)
Next
Next
Return subsets
End Function
I'd like to take credit for the code but I didn't write it. It's a very nice piece of code, I just added the target number and output display. Once I knew precisely what you wanted I was able to find the code.
-
Sep 12th, 2020, 02:39 PM
#32
Thread Starter
Member
Re: All possible combinations in an integer array
Thank you Wes, my apologies if i come off as ungrateful, but i have been trying to make this work for ages and its drove me a little mad so i really do appreciate the help.
I'll give this a go and see how i get on, cheers dude.
-
Sep 13th, 2020, 07:18 AM
#33
Thread Starter
Member
Re: All possible combinations in an integer array
Hi, is there a way to do this without having buttons so it just prints it out in console format?
I have changed the part where it says:
If is target then
messagebox.show
and i have just used console.writline which is ok but i'm not too sure how i can change the me. and button2.click to work?
-
Sep 13th, 2020, 02:02 PM
#34
Re: All possible combinations in an integer array
How do you want to initiate the code?
-
Sep 13th, 2020, 02:09 PM
#35
Thread Starter
Member
Re: All possible combinations in an integer array
So it looks like that will work, but i would just like it so that i can run it and it prints out the result to the console. It's just for me to test we right now so it does not need to be a finished program or anything.
-
Sep 14th, 2020, 05:54 PM
#36
Re: All possible combinations in an integer array
Originally Posted by passel
So, you only need to test if any combination adds up to the target, so once you find one combination that does, you're done.
Or did you mean you need to find all combinations that add up to the target, in which case there could be none, but there could be more than one?
Originally Posted by Veins333
Hi Passel,
So, i have an array, integer, and i want to be able to change the values in that array so for example it could be {1,3,5,20,30} etc and then i have a target of say 500, all i need to do is see if any combinations of the values in the array add up to the target. So in the example above none of them would, so it would test 1 + 3 = target or 3 + 5 + 20 = target etc, that's all really.
Basically, do any of the values in the array, combined or on their own add up to the target. so lets say the target was 35 for example, then 5 + 30 would match the target.
I did have something like this in Java but i have lost the program and i can't find a way to do it in VB.net.
Sooooo.... do you want ONE combination or ALL combinations? Simple question really. I'm not sure what was so hard to understand about it. IT seemed pretty clear from the get-go to me. And yet, your response still did not address the issue, but re-iterated what was already known.
What if your array contained {10, 20, 30, 40} ... and your target was 50? Do you want 10 + 40.... or 10 + 40 and 20 + 30?
Simple question. Simple answer. Acceptable response will be "A" or "B" or "the former" or "the latter" all other responses are invalid and will be considered NULL.
-tg
-
Sep 14th, 2020, 10:53 PM
#37
Thread Starter
Member
Re: All possible combinations in an integer array
Welllllll.... if you actually read the #1 post on the first sentence i did say "Hi, i need to find a way of finding all possible combinations of an array full of numbers."
Why would i want an array with multiple numbers in only to add up and test x2 of the values in the array?? All possible combinations means exactly that, unless you find it hard to understand English.
-
Sep 14th, 2020, 10:57 PM
#38
Re: All possible combinations in an integer array
Originally Posted by Veins333
Welllllll.... if you actually read the #1 post on the first sentence i did say "Hi, i need to find a way of finding all possible combinations of an array full of numbers."
Why would i want an array with multiple numbers in only to add up and test x2 of the values in the array?? All possible combinations means exactly that, unless you find it hard to understand English.
You'd understand their confusion if you had a better grasp of english yourself...
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Sep 14th, 2020, 10:58 PM
#39
Thread Starter
Member
Re: All possible combinations in an integer array
So what's so hard to understand about this:
Hi, i need to find a way of finding all possible combinations of an array full of numbers.
-
Sep 14th, 2020, 10:59 PM
#40
Re: All possible combinations in an integer array
You want the sum of each of all possible combinations of an array full of numbers, if I understand you correctly at last?
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|