Home All Groups Group Topic Archive Search About
Author
12 Jul 2006 9:57 AM
andreas
Hi,
I want to do some calculation like
( t1 and t2 are known)

for i = t1 to t2
        for j = t1 to t2
               .....
                .....
                      for p = t1 to t2
                            for q = t1 to t2
                                       some actions
                           next
                     next
               .......
       next
next

but i know only the number for next in run time with the variable iTimes
Is there a solution for this problem?
Thanks for any response

Author
12 Jul 2006 10:05 AM
Herfried K. Wagner [MVP]
Show quote Hide quote
"andreas" <andr***@pandora.be> schrieb:
> I want to do some calculation like
> ( t1 and t2 are known)
>
> for i = t1 to t2
>        for j = t1 to t2
>               .....
>                .....
>                      for p = t1 to t2
>                            for q = t1 to t2
>                                       some actions
>                           next
>                     next
>               .......
>       next
> next
>
> but i know only the number for next in run time with the variable iTimes
> Is there a solution for this problem?

You want iTimes nested 'For...To' loops?

--
M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://classicvb.org/petition/>
Author
12 Jul 2006 11:50 AM
andreas
yes,  Herfried

Show quoteHide quote
"Herfried K. Wagner [MVP]" <hirf-spam-me-here@gmx.at> wrote in message
news:#2jZoqZpGHA.3564@TK2MSFTNGP03.phx.gbl...
> "andreas" <andr***@pandora.be> schrieb:
> > I want to do some calculation like
> > ( t1 and t2 are known)
> >
> > for i = t1 to t2
> >        for j = t1 to t2
> >               .....
> >                .....
> >                      for p = t1 to t2
> >                            for q = t1 to t2
> >                                       some actions
> >                           next
> >                     next
> >               .......
> >       next
> > next
> >
> > but i know only the number for next in run time with the variable iTimes
> > Is there a solution for this problem?
>
> You want iTimes nested 'For...To' loops?
>
> --
>  M S   Herfried K. Wagner
> M V P  <URL:http://dotnet.mvps.org/>
>  V B   <URL:http://classicvb.org/petition/>
Author
12 Jul 2006 10:14 AM
Patrice
And those actions are ? How do they depend on loop variables ? IMO this is
the main problem...

For exaple you could just use a single loop (doing 4 times 10 loops would be
the same than doing a loop 40 times)

--
Patrice

"andreas" <andr***@pandora.be> a écrit dans le message de news:
wI3tg.532581$rz4.12999***@phobos.telenet-ops.be...
Show quoteHide quote
> Hi,
> I want to do some calculation like
> ( t1 and t2 are known)
>
> for i = t1 to t2
>        for j = t1 to t2
>               .....
>                .....
>                      for p = t1 to t2
>                            for q = t1 to t2
>                                       some actions
>                           next
>                     next
>               .......
>       next
> next
>
> but i know only the number for next in run time with the variable iTimes
> Is there a solution for this problem?
> Thanks for any response
>
>
Author
12 Jul 2006 11:53 AM
andreas
it is not possible to change the 4 times 10 loops in 40 loops because i do
something with i, j,k ...


Show quoteHide quote
"Patrice" <scr***@chez.com> wrote in message
news:#1MA#vZpGHA.4988@TK2MSFTNGP04.phx.gbl...
> And those actions are ? How do they depend on loop variables ? IMO this is
> the main problem...
>
> For exaple you could just use a single loop (doing 4 times 10 loops would
be
> the same than doing a loop 40 times)
>
> --
> Patrice
>
> "andreas" <andr***@pandora.be> a écrit dans le message de news:
> wI3tg.532581$rz4.12999***@phobos.telenet-ops.be...
> > Hi,
> > I want to do some calculation like
> > ( t1 and t2 are known)
> >
> > for i = t1 to t2
> >        for j = t1 to t2
> >               .....
> >                .....
> >                      for p = t1 to t2
> >                            for q = t1 to t2
> >                                       some actions
> >                           next
> >                     next
> >               .......
> >       next
> > next
> >
> > but i know only the number for next in run time with the variable iTimes
> > Is there a solution for this problem?
> > Thanks for any response
> >
> >
>
>
Author
12 Jul 2006 12:03 PM
Patrice
If you do something with an unknown number of variables you could use an
array instead of using i,j,k etc... You'll just have a single loop in which
you'll increment the appropriate element of the array for use in your
actions...

If you prefer and have a maximum possible number of loops you could also use
start=stop for some of the loop so that they are done only once when
appropriate etc...

--
Patrice

"andreas" <andr***@pandora.be> a écrit dans le message de news:
3p5tg.532724$%h6.12884***@phobos.telenet-ops.be...
Show quoteHide quote
> it is not possible to change the 4 times 10 loops in 40 loops because i do
> something with i, j,k ...
>
>
> "Patrice" <scr***@chez.com> wrote in message
> news:#1MA#vZpGHA.4988@TK2MSFTNGP04.phx.gbl...
>> And those actions are ? How do they depend on loop variables ? IMO this
>> is
>> the main problem...
>>
>> For exaple you could just use a single loop (doing 4 times 10 loops would
> be
>> the same than doing a loop 40 times)
>>
>> --
>> Patrice
>>
>> "andreas" <andr***@pandora.be> a écrit dans le message de news:
>> wI3tg.532581$rz4.12999***@phobos.telenet-ops.be...
>> > Hi,
>> > I want to do some calculation like
>> > ( t1 and t2 are known)
>> >
>> > for i = t1 to t2
>> >        for j = t1 to t2
>> >               .....
>> >                .....
>> >                      for p = t1 to t2
>> >                            for q = t1 to t2
>> >                                       some actions
>> >                           next
>> >                     next
>> >               .......
>> >       next
>> > next
>> >
>> > but i know only the number for next in run time with the variable
>> > iTimes
>> > Is there a solution for this problem?
>> > Thanks for any response
>> >
>> >
>>
>>
>
>
Author
12 Jul 2006 3:45 PM
Branco Medeiros
andreas wrote:
Show quoteHide quote
> I want to do some calculation like
> ( t1 and t2 are known)
>
> for i = t1 to t2
>         for j = t1 to t2
>                .....
>                 .....
>                       for p = t1 to t2
>                             for q = t1 to t2
>                                        some actions
>                            next
>                      next
>                .......
>        next
> next
>
> but i know only the number for next in run time with the variable iTimes
<snip>

One possible approach would be to use a single function controlled by
iTimes: if iTimes was 0, it would perform the specified action,
otherwise it would call itself recursevelly, decrementing iTimes. The
matters complicate a little because you want the respective indexes of
each loop, but these can be kept in a stack and accessed by the
'action' sub.

An air-code example of this approach could be:

Class NestedLoop
  Protected ReadOnly Stack As New Stack(Of Integer)
  Protected ReadOnly Min As Integer
  Protected ReadOnly Max As Integer
  Protected Levels As Integer

  Sub New(Levels As Integer, Min As Integer, Max As Integer)
    Me.Min = Min
    Me.Max = Max
    Me.Levels = Levels
  End Sub

  Protected Sub Execute
    DoLoop(Levels)
  End Sub

  Protected Overridable Sub Action()
  End Sub

  Private Sub DoLoop(Level As Integer)
    If Level = 0 Then
      Action
    Else
      For I As Integer = Min To Max
        Stack.Push(I)
        DoLoop(Level - 1)
        I = Stack.Pop
      Next
    End If
  End Sub
End Class

Then, it would be just a matter of inheriting from the class and
overriding the method Action, adding to the derived class whatever new
properties you'd like.

  Class SimpleLoop
  Inherits NestedLoop
    Public ReadOnly Result As Integer

    Public Sub New(Levels As Integer, Min As Integer, Max As Integer)
      MyBase.New(Levels, Min, Max)
      Execute
    End Sub

    Protected Overrides Sub Action
      Result += 1
    End Sub
  End Class

Finally, to invoke the action, you could use something like this:

  Dim ActionResult As New SimpleLoop(iTimes, T1, T2)

Presto! =)

HTH.

Regards,

Branco.
Author
12 Jul 2006 4:04 PM
andreas
Branco,
Not easy for me to understand all but I think this is the solution for my
problem.
Thank you and everyone.

Show quoteHide quote
"Branco Medeiros" <branco.medei***@gmail.com> wrote in message
news:1152719111.193631.254370@35g2000cwc.googlegroups.com...
> andreas wrote:
> > I want to do some calculation like
> > ( t1 and t2 are known)
> >
> > for i = t1 to t2
> >         for j = t1 to t2
> >                .....
> >                 .....
> >                       for p = t1 to t2
> >                             for q = t1 to t2
> >                                        some actions
> >                            next
> >                      next
> >                .......
> >        next
> > next
> >
> > but i know only the number for next in run time with the variable iTimes
> <snip>
>
> One possible approach would be to use a single function controlled by
> iTimes: if iTimes was 0, it would perform the specified action,
> otherwise it would call itself recursevelly, decrementing iTimes. The
> matters complicate a little because you want the respective indexes of
> each loop, but these can be kept in a stack and accessed by the
> 'action' sub.
>
> An air-code example of this approach could be:
>
> Class NestedLoop
>   Protected ReadOnly Stack As New Stack(Of Integer)
>   Protected ReadOnly Min As Integer
>   Protected ReadOnly Max As Integer
>   Protected Levels As Integer
>
>   Sub New(Levels As Integer, Min As Integer, Max As Integer)
>     Me.Min = Min
>     Me.Max = Max
>     Me.Levels = Levels
>   End Sub
>
>   Protected Sub Execute
>     DoLoop(Levels)
>   End Sub
>
>   Protected Overridable Sub Action()
>   End Sub
>
>   Private Sub DoLoop(Level As Integer)
>     If Level = 0 Then
>       Action
>     Else
>       For I As Integer = Min To Max
>         Stack.Push(I)
>         DoLoop(Level - 1)
>         I = Stack.Pop
>       Next
>     End If
>   End Sub
> End Class
>
> Then, it would be just a matter of inheriting from the class and
> overriding the method Action, adding to the derived class whatever new
> properties you'd like.
>
>   Class SimpleLoop
>   Inherits NestedLoop
>     Public ReadOnly Result As Integer
>
>     Public Sub New(Levels As Integer, Min As Integer, Max As Integer)
>       MyBase.New(Levels, Min, Max)
>       Execute
>     End Sub
>
>     Protected Overrides Sub Action
>       Result += 1
>     End Sub
>   End Class
>
> Finally, to invoke the action, you could use something like this:
>
>   Dim ActionResult As New SimpleLoop(iTimes, T1, T2)
>
> Presto! =)
>
> HTH.
>
> Regards,
>
> Branco.
>
Author
12 Jul 2006 6:10 PM
Branco Medeiros
andreas wrote:
> Branco,
> Not easy for me to understand all but I think this is the solution for my
> problem.

It's actually quite simple.

When you create an instance of a class derived from NestedLoop, it
initializes the loop parameters with a call to MyBase.New(). The class
must then call the protected method Execute, which will actually start
the loop by calling the private method DoLoop in the base class.

The DoLopp method just checks the current nest level: if it's 0 then it
calls the virtual method Action, otherwise it starts a new loop in the
specified range and calls itself recursivelly, decrementing Level as it
goes. This way there will be N recursive DoLoop calls, corresponding to
the number of levels you specified. For all effects, they act as the
nested loops from your original post.

I guess you can see that the Action method is the one that you must
override in your derived classes. It corresponds to the methods
executed inside the most inner loop in the example you gave originally.

Now notice that the number of times Action will be executed is
exponencial to the number of levels, corresponding to ((Max - Min) + 1)
^ Levels. For instance, with Min=1, Max=10 and Levels = 10, the Action
method will be called 10^10 times, that is, 10000000000 times... =))

Regards,

Branco.
Author
13 Jul 2006 6:07 AM
andreas
Thanks Branco for your explanation

Show quoteHide quote
"Branco Medeiros" <branco.medei***@gmail.com> wrote in message
news:1152727805.142345.147540@75g2000cwc.googlegroups.com...
> andreas wrote:
> > Branco,
> > Not easy for me to understand all but I think this is the solution for
my
> > problem.
>
> It's actually quite simple.
>
> When you create an instance of a class derived from NestedLoop, it
> initializes the loop parameters with a call to MyBase.New(). The class
> must then call the protected method Execute, which will actually start
> the loop by calling the private method DoLoop in the base class.
>
> The DoLopp method just checks the current nest level: if it's 0 then it
> calls the virtual method Action, otherwise it starts a new loop in the
> specified range and calls itself recursivelly, decrementing Level as it
> goes. This way there will be N recursive DoLoop calls, corresponding to
> the number of levels you specified. For all effects, they act as the
> nested loops from your original post.
>
> I guess you can see that the Action method is the one that you must
> override in your derived classes. It corresponds to the methods
> executed inside the most inner loop in the example you gave originally.
>
> Now notice that the number of times Action will be executed is
> exponencial to the number of levels, corresponding to ((Max - Min) + 1)
> ^ Levels. For instance, with Min=1, Max=10 and Levels = 10, the Action
> method will be called 10^10 times, that is, 10000000000 times... =))
>
> Regards,
>
> Branco.
>
Author
13 Jul 2006 10:11 AM
andreas
To make my problem clear:

I want to have in a array (wich I can redim in runtime)  of strings the
following
Choice to make in runtime :
Or "11","01","10",00
Or "111","110","101","100","011","010","001","000"
Or "1111","1110",1101","1100","1011",...............
..............
Or  "111111111111","111111111110","111111111101",.....
.......
and so on
Is the method of Branco, wich I thank, the only solution?

Show quoteHide quote
"andreas" <andr***@pandora.be> wrote in message
news:wI3tg.532581$rz4.12999779@phobos.telenet-ops.be...
> Hi,
> I want to do some calculation like
> ( t1 and t2 are known)
>
> for i = t1 to t2
>         for j = t1 to t2
>                .....
>                 .....
>                       for p = t1 to t2
>                             for q = t1 to t2
>                                        some actions
>                            next
>                      next
>                .......
>        next
> next
>
> but i know only the number for next in run time with the variable iTimes
> Is there a solution for this problem?
> Thanks for any response
>
>
Author
13 Jul 2006 12:20 PM
Patrice
That is you want to print out all binary numbers for a given length ?

If yes, a simple loop going from 0 to 2^length-1 will enumerate all possible
values. Then print out each value using the binary format...

--
Patrice

"andreas" <andr***@pandora.be> a écrit dans le message de news:
G%otg.534476$O12.12861***@phobos.telenet-ops.be...
Show quoteHide quote
> To make my problem clear:
>
> I want to have in a array (wich I can redim in runtime)  of strings the
> following
> Choice to make in runtime :
> Or "11","01","10",00
> Or "111","110","101","100","011","010","001","000"
> Or "1111","1110",1101","1100","1011",...............
> .............
> Or  "111111111111","111111111110","111111111101",.....
> ......
> and so on
> Is the method of Branco, wich I thank, the only solution?
>
> "andreas" <andr***@pandora.be> wrote in message
> news:wI3tg.532581$rz4.12999779@phobos.telenet-ops.be...
>> Hi,
>> I want to do some calculation like
>> ( t1 and t2 are known)
>>
>> for i = t1 to t2
>>         for j = t1 to t2
>>                .....
>>                 .....
>>                       for p = t1 to t2
>>                             for q = t1 to t2
>>                                        some actions
>>                            next
>>                      next
>>                .......
>>        next
>> next
>>
>> but i know only the number for next in run time with the variable iTimes
>> Is there a solution for this problem?
>> Thanks for any response
>>
>>
>
>
Author
13 Jul 2006 1:43 PM
Branco Medeiros
andreas wrote:
> I want to have in a array (wich I can redim in runtime)  of strings the
> following
> Choice to make in runtime :
> Or "11","01","10",00
> Or "111","110","101","100","011","010","001","000"
> Or "1111","1110",1101","1100","1011",...............
> .............
> Or  "111111111111","111111111110","111111111101",.....
> ......
> and so on
<snip>

In this case, maybe the following function works for you:

  Function BinaryRange(ByVal Times As Integer) As String()
    Dim Max As Integer = (2 ^ Times) - 1
    Dim Result(Max) As String
    For I As Integer = 0 To Max
      Result(Max - I) = Convert.ToString(I, 2).PadLeft(Times, "0"c)
    Next
    Return Result
  End Function

It retursns an array of strings built with the binary of the numbers
from (2^Times) - 1 down to 0, i.e., for Times = 4, it would be "1111",
"1110", "1101", "1100", "1011", "1010", "1001", "1000", "0111", "0110",
"0101", "0100", "0011", "0010", "0001" and "0000".

HTH.

Regards,

Branco.
Author
13 Jul 2006 3:57 PM
andreas
Thanks to Branco and all the others
Branco, what you wrote is very simple and clever.
I feel a litle stupid, but thanks, thanks, thanks.8

Show quoteHide quote
"Branco Medeiros" <branco.medei***@gmail.com> wrote in message
news:1152798228.102342.132420@h48g2000cwc.googlegroups.com...
>
> andreas wrote:
> > I want to have in a array (wich I can redim in runtime)  of strings the
> > following
> > Choice to make in runtime :
> > Or "11","01","10",00
> > Or "111","110","101","100","011","010","001","000"
> > Or "1111","1110",1101","1100","1011",...............
> > .............
> > Or  "111111111111","111111111110","111111111101",.....
> > ......
> > and so on
> <snip>
>
> In this case, maybe the following function works for you:
>
>   Function BinaryRange(ByVal Times As Integer) As String()
>     Dim Max As Integer = (2 ^ Times) - 1
>     Dim Result(Max) As String
>     For I As Integer = 0 To Max
>       Result(Max - I) = Convert.ToString(I, 2).PadLeft(Times, "0"c)
>     Next
>     Return Result
>   End Function
>
> It retursns an array of strings built with the binary of the numbers
> from (2^Times) - 1 down to 0, i.e., for Times = 4, it would be "1111",
> "1110", "1101", "1100", "1011", "1010", "1001", "1000", "0111", "0110",
> "0101", "0100", "0011", "0010", "0001" and "0000".
>
> HTH.
>
> Regards,
>
> Branco.
>