473,221 Members | 2,115 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,221 software developers and data experts.

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 2831
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: JMCN | last post by:
help! i'm caught in the endless of "compile error message: do without loop." i thought that i closed all of my statments but it appears not. does anyone know why my structure is incorrect? what...
32
by: Toby Newman | last post by:
At the page: http://www.strath.ac.uk/IT/Docs/Ccourse/subsection3_8_3.html#SECTION0008300000000000000 or http://tinyurl.com/4ptzs the author warns: "The for loop is frequently used, usually...
23
by: Mitchell Vincent | last post by:
Is there any way to "skip" iterations in a for loop? Example : for x = 1 to 10 if something = 1 next endif
4
by: Iain | last post by:
Hi all, I am try to split the some data out of a nested xml file and get all the data in one new xml file. I have two XSLT files one takes the personal information and one takes the data out of...
16
by: Claudio Grondi | last post by:
Sometimes it is known in advance, that the time spent in a loop will be in order of minutes or even hours, so it makes sense to optimize each element in the loop to make it run faster. One of...
2
by: assgar | last post by:
Hi Developemnt on win2003 server. Final server will be linux Apache,Mysql and PHP is being used. I use 2 scripts(form and process). The form displays multiple dynamic rows with chechboxs,...
13
by: rn5a | last post by:
In a shopping cart app, suppose a user has placed 5 orders, I want to show him 5 LinkButtons (one for each order) so that when he clicks the first LinkButton, he would be shown the details of his...
0
by: tickle | last post by:
Need to convert this PL/SQL script to Dynamic SQL Method 2 * copybook - celg02u3.sql SIR 24265 * * updates dt_deny for all rows in * * ...
10
by: Jonathan | last post by:
Hi all, I have a file consisting fixed width records from which I need to extract only those lines meeting certain conditions. These conditions do change and I find myself recoding/compiling...
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.