# how to create a function/loop with n recurrences from this code • ### Question

• User2146470223 posted

Hi,

Is it possible to create an loop/function with n recurrences from this code?

I would like to use it for n containers (when not knowing how many containers there will be). I have some knowledge about creating such n recurrences for simple loops but not with a loop nesting more loops. So if you could give me some help or hints here, that would be great.

starting from these 2 functions: (if needed I could add more functions for higher amount of containers)

the first one is for 3 containers:

```			Dim cont As Integer = 3
For i = 0 To arry.Length - cont
sum1 = getSum(0, i, arry)
j = i + 1
For k = j To arry.Length - 2
sum2 = getSum(i + 1, k, arry)
l = k + 1
sum3 = getSum(l, arry.Length - 1, arry)
sums = {sum1, sum2, sum3}
sumCha = getSumCha(sums)
myResultArry.Add(New MyResult With {.i = i, .j = j, .k = k, .l = l, .sum1 = sum1, .sum2 = sum2, .sum3 = sum3, .sumCha = sumCha})
Next
Next```

the second one is for 4 containers:

```			Dim cont As Integer = 4
For i = 0 To arry.Length - cont
sum1 = getSum(0, i, arry)
j = i + 1
For k = j To arry.Length - 3
sum2 = getSum(i + 1, k, arry)
l = k + 1
For m = l To arry.Length - 2
sum3 = getSum(k + 1, m, arry)
n = m + 1
sum4 = getSum(n, arry.Length - 1, arry)
sums = {sum1, sum2, sum3, sum4}
sumCha = getSumCha(sums)
myResultArry.Add(New MyResult With {.i = i, .j = j, .k = k, .l = l, .m = m, .n = n, .sum1 = sum1, .sum2 = sum2, .sum3 = sum3, .sum4 = sum4, .sumCha = sumCha})
Next
Next
Next```

The difference between each additional container is:

• an additional loop where the arry.length - x part takes into account the higher amount of containers
• 2 new integer variables at beginning of new for loop and to line myResultArry
• an additional sumx variable added to line sums =   and to line myResultArry

my thoughts that far:

• instead of using i, j, k aso. I guess it would make things easier to use i1, i2, i3, aso. This would make it easier to go up to n * 2 new variables.
• for the arry.length - x my thought was to increment a variable for each new loop (e.g. y) use … to arry.length - (cont - y)

Pascal

Tuesday, December 26, 2017 8:04 PM

### All replies

• User-707554951 posted

Hi lakshmiprasanna,

As far as I know, A Method can call another methods but it can also call itself. When a mathod calls itself, it'll be named recursive method.

A Recursive usuallly, has the two specifications:

Recursive method calls itself so many times until being satisfied.

Recursive method has parameter(s) and calls itself with new parameter values.

So, I suggest you could consider to use it:

https://www.dotnetperls.com/recursion-vbnet

https://docs.microsoft.com/en-us/dotnet/visual-basic/programming-guide/language-features/procedures/recursive-procedures

Best regards

Cathy

Wednesday, December 27, 2017 6:27 AM
• User2146470223 posted

Hi Cathy,

Thanks for the reference, that one is quite helpful. I did not know what to look for.

With this reference this is what I got that far, but as I said I never tried to set up such a complex function before yet, so I don't get the result needed. Here is what I got so far:

```	Public Function verteil(ByVal i As Integer, ByVal j As Integer, ByVal k As Integer, ByVal cont2 As Integer, ByVal cont3 As Integer) As Integer()
If cont2 > 0 Then
cont2 = cont2 - 1
cont3 = cont3 + 1
For i = 0 To arry.Length - cont - cont3
If cont3 = 0 Then
Else
End If
j = i + 1
Next
verteil(i, j, k, cont2, cont3)
Else
l = k + 1

sums = sumlist.ToArray
sumCha = getSumCha(sums)
myResultArry.Add(New MyResult With {.i = i, .j = j, .k = k, .l = l, .sum1 = sum1, .sum2 = sum2, .sum3 = sum3, .sumCha = sumCha})
End If
getMinCha = sumChaArry.Min()
final = myResultArry.Where(Function(a) a.sumCha = getMinCha).FirstOrDefault()
ToPrint(0, final.i, arry)
ToPrint(final.j, final.k, arry)
ToPrint(final.l, arry.Length - 1, arry)
End Function```

Am I on the right track and/or did I miss something?

the lines

```			myResultArry.Add(New MyResult With {.i = i, .j = j, .k = k, .l = l, .sum1 = sum1, .sum2 = sum2, .sum3 = sum3, .sumCha = sumCha})
```

and

```		ToPrint(0, final.i, arry)
ToPrint(final.j, final.k, arry)
ToPrint(final.l, arry.Length - 1, arry)
```

were not touched yet, as I just want to test the function with 3 containers first and if I get the wanted result, I will try to adapt these lines too.

Regards,

Pascal

Thursday, December 28, 2017 3:48 PM