Question recursive function

cem babaeren

New member
Joined
Feb 14, 2011
Messages
4
Programming Experience
Beginner
hi;

i am a financial analysist trying to develop windows applications in vb.net. I have been stuck with a situation that i can not resolve without the help. Here is the problem.

I have 3 variables. Lets call it "a", "b" and "c". These 3 variables can "independently" have 3 different values. Lets say: "1", "6" and "10". when you think of them as combination; there are (3^3) 27 combinations. I am told that it can be done through the recursive functions. However i am not a good developer to handle with that. Would u please help me?

- 3 variables and 3 different values are simplified. In fact; the numbers of variables and different values can vary.

- i know only the language, visual basic in .net; please dont answer in other coding languages.

additional note: i am going to put the these combinations in the string format into a collection.. for example "1;1;1", "1;6;1", "1;6;10".. and so on..
 
I read your problem but I can't figure out what you're trying to do. I appreciate that you tried to simplify it to make it easy to understand but you mention that the number of variables and values change. There has to be a way that either you know or have a "count" that can be used when looping.

Just wondering what type of variables are you using? Class,array...
 
I'm not very sure on exactly what u want to accomplish, but I'll give an example with a List(Of T) to give you some ideas.
But it can be done in other ways, I just like lists

it really depends on what u pass or not on the optional parameters and what u want to accomplish this thing can grow a lot in complexity.
Maybe you can pass the index in the list instead of the value, so u can compare it from one loop to the next, etc, etc....

VB.NET:
    Sub PrepareForTheSuperSweetSub()
        Dim myList As New List(Of Integer)
        myList.Add(1)
        myList.Add(6)
        myList.Add(10)
        mySuperSweetSub(myList)
    End Sub

    Sub mySuperSweetSub(ByVal myList As List(Of Integer), Optional ByVal current As Integer = Nothing)

        For Each i As Integer In myList
            If current = Nothing Then
                mySuperSweetSub(myList, i)
            Else
                '
                ' insert here whatever you fancy processing
                ' this part will be able to access:
                '            'current' - the value sent from the previous call
                '            'i'       - the value being looped in this call
                '


            End If
        Next i

    End Sub
 
dear ninjatalon and Budius;

first of all; sorry for the delay.. i had some internet connection problems previous days..

VB.NET:
    Public Shared Sub calculate(ByVal number_of_variables)

        Dim combination_list As New Collection
        Dim starting_value As Integer
        Dim ending_value As Integer
        Dim incremental_value As Integer
        Dim temp_value As Integer
        Dim value_list As New Collection

        starting_value = 1
        ending_value = 10
        incremental_value = 3
        temp_value = starting_value

        Do Until temp_value > ending_value
            value_list.Add(temp_value) '(1, 4, 7, 10)
            temp_value += incremental_value
        Loop

        Dim newStrings As New Collection
        Dim temp_str As String
        Dim temp_str_splitted() As String
        Dim count As Integer

        For i = 1 To value_list.Count
            newStrings.Add(value_list(i))
        Next

        For len As Integer = 2 To number_of_variables '"a","b","c","d", "e" 
            For i = 1 To newStrings.Count
                Dim str As String
                str = newStrings(i)
                For j = 1 To value_list.Count
                    Dim ch As String
                    ch = value_list(j)
                    temp_str = str & ";" & ch
                    newStrings.Add(temp_str)
                    temp_str_splitted = Split(temp_str, ";")
                    If UBound(temp_str_splitted) + 1 = number_of_variables Then
                        count += 1
                        combination_list.Add(temp_str)
                    End If

                Next
            Next
        Next
    End Sub

I am putting all combinations into the "combination_list" to use after to gerenrate new series. I used "If UBound(temp_str_splitted) + 1 = number_of_variables" since i only need the combinations having the lenght "number of variables", not lower than it.

In my example i have 5 variables and 4 different values. That makes 4'5 = 1024.

However i have a problem: i want to create billions of combinations. (let's make "incremental_value = 0.01 instead of incremental_value = 3) But it gives the errror, "out of memory exception".

How can i handle that?

I haven't tried your advice yet; Budius.. Will your code handle this problem?

thanks..
 
But it gives the errror, "out of memory exception".
I haven't tried your advice yet; Budius.. Will your code handle this problem?

my code have no memory handling whatsoever, it just do some generic recursive function as example.

You have to remember that everytime your code do a .Add it will need more memory for it, and I saw a few .Add inside loops.
I GUESS that Datasets are more memory efficient than List(Of T) and Collection, you could give a shot on it, another option is optimize the code to not .Add so much.

Furthermore, this code here
VB.NET:
        For i = 1 To value_list.Count
            newStrings.Add(value_list(i))
        Next
seems to be just copying one Collection to the other unnecessarily (why do you need this duplicate value memory allocation?) and seems to be wrong and I'm surprised it didn't throw an exception.
the index in value_list ranges from 0 to value_list.Count - 1 and it's generally good practice to do For Each myInt as Integer in value_list


good luck and happy coding...
 
Back
Top