473,394 Members | 1,696 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,394 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 2840
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.