Dynamic loop nesting

H

Hardrock

I encountered some difficulty in implementing dynamic loop nesting.
I.e. the number of nesting in a for(...) loop is determined at run
time. For example

void f(int n)
{
For(i[1]=0; i[1]<=K[1]; i[1]++)
For(i[2]=0; i[2]<=K[2]; i[2]++)
.
.
.
For(i[n]=0; i[n]<=k[n]; i[n]++)
{
...
}

}


Here n is an argument of funtion f(). The problem is that since n is a
variable, so I don't know how many for() loops I need to write in my
source code.

Any input to help me solve this problem will be deeply appreciated.


Hardtrock
 
J

Joona I Palaste

Hardrock said:
I encountered some difficulty in implementing dynamic loop nesting.
I.e. the number of nesting in a for(...) loop is determined at run
time. For example
void f(int n)
{
For(i[1]=0; i[1]<=K[1]; i[1]++)
For(i[2]=0; i[2]<=K[2]; i[2]++)
.
.
.
For(i[n]=0; i[n]<=k[n]; i[n]++)
{
...
}


Here n is an argument of funtion f(). The problem is that since n is a
variable, so I don't know how many for() loops I need to write in my
source code.
Any input to help me solve this problem will be deeply appreciated.

Use a recursive function with a for loop inside it. Pass a parameter to
the function telling how deep in the recursion you are in. That's the
way I have done it. If you want further help I'll give it tomorrow when
I have got some sleep.
 
C

Christopher Benson-Manica

Hardrock said:
I encountered some difficulty in implementing dynamic loop nesting.
I.e. the number of nesting in a for(...) loop is determined at run
time. For example
void f(int n)
{
For(i[1]=0; i[1]<=K[1]; i[1]++)
For(i[2]=0; i[2]<=K[2]; i[2]++)
.
.
.
For(i[n]=0; i[n]<=k[n]; i[n]++)
{
...
}
}

void f( int n )
{
int idx; /* i is a preferable name, but you're already using it */

for( idx=1; idx <= n; idx++ ) {
for( i[idx]=0; i[idx] <= K[idx]; i[idx]++ ) {
...
}
}
}

See how it works?
 
D

Dave Vandervies

I encountered some difficulty in implementing dynamic loop nesting.
I.e. the number of nesting in a for(...) loop is determined at run
time. For example

void f(int n)
{
For(i[1]=0; i[1]<=K[1]; i[1]++)
For(i[2]=0; i[2]<=K[2]; i[2]++)
.
.
.
For(i[n]=0; i[n]<=k[n]; i[n]++)
{
...
}

}


Here n is an argument of funtion f(). The problem is that since n is a
variable, so I don't know how many for() loops I need to write in my
source code.

Any input to help me solve this problem will be deeply appreciated.

What are you really trying to do?


This looks just enough like homework that I'm going to just give you
hints instead of posting code.

Start by working out how you would loop through all possible values of
an N-digit decimal number.
(Hint: Add in the least significant digit, then normalize all the
digit values. Be sure to keep track of your termination condition.)

Once you've worked that out, it should be a simple exercise to dynamically
vary the digit values based on what you get in k[] instead of fixing
them all at 10.


dave
 
S

SM Ryan

(e-mail address removed) (Hardrock) wrote:
# I encountered some difficulty in implementing dynamic loop nesting.
# I.e. the number of nesting in a for(...) loop is determined at run
# time. For example
#
# void f(int n)
# {
# For(i[1]=0; i[1]<=K[1]; i[1]++)
# For(i[2]=0; i[2]<=K[2]; i[2]++)
# .
# .
# .
# For(i[n]=0; i[n]<=k[n]; i[n]++)
# {
# ...
# }
#
# }

empty = 0;
for (j=0; !empty && j<n; j++) {
i[j] = 0; empty = i[j]>=k[j];
}
if (!empty)
for (;;) {
...
loop body
...
for (j=n-1; j>=0; j--) {
i[j]++;
if (i[j]>=k[j]) i[j] = 0;
else break;

}
if (j<0) break;
}
 
R

RoSsIaCrIiLoIA

void f(int n)
{
For(i[1]=0; i[1]<=K[1]; i[1]++)
For(i[2]=0; i[2]<=K[2]; i[2]++)
.
.
.
For(i[n]=0; i[n]<=k[n]; i[n]++)
{
...
}

}

Un paio di volte ho risolto utilizzando un sistema simile di for(;;).
Dove l'hai preso questo pezzo di codice?
 
H

Hardrock

Joona I Palaste said:
Hardrock said:
I encountered some difficulty in implementing dynamic loop nesting.
I.e. the number of nesting in a for(...) loop is determined at run
time. For example
void f(int n)
{
For(i[1]=0; i[1]<=K[1]; i[1]++)
For(i[2]=0; i[2]<=K[2]; i[2]++)
.
.
.
For(i[n]=0; i[n]<=k[n]; i[n]++)
{
...
}


Here n is an argument of funtion f(). The problem is that since n is a
variable, so I don't know how many for() loops I need to write in my
source code.
Any input to help me solve this problem will be deeply appreciated.

Use a recursive function with a for loop inside it. Pass a parameter to
the function telling how deep in the recursion you are in. That's the
way I have done it. If you want further help I'll give it tomorrow when
I have got some sleep.

Actually I once think of using recursive call, but in my senario it is
not the best, since I'm working on a large n-dimensional array,
recurcive call will cause too much time and space, so I try to use
loop nesting in a single function. I would appreciate if you could
give me some tips on how to accomplish it in a single function.

Thanks
 
H

Hardrock

Hi, Dave

Yes, the ideas are very similar with working through a N-digit number.
I actually working on a very large n-dimensional array, in a senario
that the manipulation on the Nth dimension largely depend on the
result of the previous n-1 dimensions. Actually I can implement it
using recursive call, but in my senario it is not the best, since
recurcive call will cause too much time and space, so I try to use
loop nesting in a single function. So the diffcult thing is the number
of nesting loops is dynamic and is not fixed, so how could I implement
it in a single function.

Thanks


Hi, Folks

Actually I'm working on a n-dimensional array, So a possible way is to
use recurcive function, but I then think that's very costly both on
time and space. I try to avoid it and try to use loop nesting in a
single function.



I encountered some difficulty in implementing dynamic loop nesting.
I.e. the number of nesting in a for(...) loop is determined at run
time. For example

void f(int n)
{
For(i[1]=0; i[1]<=K[1]; i[1]++)
For(i[2]=0; i[2]<=K[2]; i[2]++)
.
.
.
For(i[n]=0; i[n]<=k[n]; i[n]++)
{
...
}

}


Here n is an argument of funtion f(). The problem is that since n is a
variable, so I don't know how many for() loops I need to write in my
source code.

Any input to help me solve this problem will be deeply appreciated.

What are you really trying to do?


This looks just enough like homework that I'm going to just give you
hints instead of posting code.

Start by working out how you would loop through all possible values of
an N-digit decimal number.
(Hint: Add in the least significant digit, then normalize all the
digit values. Be sure to keep track of your termination condition.)

Once you've worked that out, it should be a simple exercise to dynamically
vary the digit values based on what you get in k[] instead of fixing
them all at 10.


dave
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top