 # how to create a function/loop with n recurrences from this codeRSS

## 2 replies

Last post Dec 28, 2017 03:48 PM by Translating-it

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

Dec 26, 2017 08:04 PM|Translating-it|LINK

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)
sumChaArry.Add(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)
sumChaArry.Add(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)

Thanks in advance,

Pascal

Dear ASP.NET, stop setting my threads to answered only cos I had no time to check em. I'm not fulltime programmer n only get to programming every 2-3 weeks but I ALWAYS answer n close solved threads, but u DON'T know if a problem is solved for TS or not!
• ### Re: how to create a function/loop with n recurrences from this code

Dec 27, 2017 06:27 AM|Cathy Zou|LINK

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

https://www.codeproject.com/Articles/32873/Recursion-made-simple

Best regards

Cathy

MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue.
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
• ### Re: how to create a function/loop with n recurrences from this code

Dec 28, 2017 03:48 PM|Translating-it|LINK

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
sumlist.Add(getSum(0, i, arry))
Else
sumlist.Add(getSum(0 + 1, k, arry))
End If
j = i + 1
Next
verteil(i, j, k, cont2, cont3)
Else
sumlist.Add(getSum(0 + 1, k, arry))
l = k + 1
sumlist.Add(getSum(l, arry.Length - 1, arry))

sums = sumlist.ToArray
sumCha = getSumCha(sums)
sumChaArry.Add(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

Dear ASP.NET, stop setting my threads to answered only cos I had no time to check em. I'm not fulltime programmer n only get to programming every 2-3 weeks but I ALWAYS answer n close solved threads, but u DON'T know if a problem is solved for TS or not!