470,831 Members | 1,717 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,831 developers. It's quick & easy.

Dynamic loop nesting

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]++)
Nov 14 '05 #1
8 2675
Hardrock <sh***********@yahoo.com> scribbled the following:
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.

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"All that flower power is no match for my glower power!"
- Montgomery Burns
Nov 14 '05 #2
Hardrock <sh***********@yahoo.com> spoke thus:
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?

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Nov 14 '05 #3
Christopher Benson-Manica <at***@nospam.cyberspace.org> spoke thus:
See how it works?


On second thought, no, I didn't. Sorry for another bone-headed post.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Nov 14 '05 #4
In article <20**************************@posting.google.com >,
Hardrock <sh***********@yahoo.com> 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]++)
{
...
}

}
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

--
Dave Vandervies dj******@csclub.uwaterloo.ca
However, not liking dogs, eating one has a certain appeal for me.
I think that it would be better than using live terriers for skeet-shooting.
--David Evans in uw.general
Nov 14 '05 #5
sh***********@yahoo.com (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;
}

--
SM Ryan http://www.rawbw.com/~wyrmwif/
Where do you get those wonderful toys?
Nov 14 '05 #6
On 2 Sep 2004 13:14:41 -0700, (Hardrock) wrote:
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?

Nov 14 '05 #7
Joona I Palaste <pa*****@cc.helsinki.fi> wrote in message news:<ch**********@oravannahka.helsinki.fi>...
Hardrock <sh***********@yahoo.com> scribbled the following:
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
Nov 14 '05 #8
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.

dj******@csclub.uwaterloo.ca (Dave Vandervies) wrote in message news:<ch**********@rumours.uwaterloo.ca>...
In article <20**************************@posting.google.com >,
Hardrock <sh***********@yahoo.com> 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]++)
{
...
}

}
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

Nov 14 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

32 posts views Thread by Toby Newman | last post: by
23 posts views Thread by Mitchell Vincent | last post: by
4 posts views Thread by Iain | last post: by
13 posts views Thread by rn5a | last post: by
reply views Thread by mihailmihai484 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.