473,326 Members | 2,196 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,326 software developers and data experts.

unkown number of nested for loops

Hi, Somehow I can't figure this one out.

I have an integer variable N and this N reflects the amount of nested
for loops. I mean

if N = 1, the I have

for (int i1 = ....) {
<code>
}

if N = 2, I have

for (int i1 = ...)
for (int i2 = ...) {
<code>
}

....

if N = n I must have

for (int i1 = ...)
....
for (int in = ...) {
<code>
}
Is there a way to program this disregarding the value of N?

thanks
Sep 21 '06 #1
3 1491
In article <ee**********@gaudi2.UGent.be>,
Klaas Vantournhout <no************@spam.comwrote:
>Hi, Somehow I can't figure this one out.
>I have an integer variable N and this N reflects the amount of nested
for loops. I mean
>if N = n I must have
>for (int i1 = ...)
...
for (int in = ...) {
<code>
}
>Is there a way to program this disregarding the value of N?
int *indices = malloc(N * sizeof(int));

then indices[N-1] corresponds to iN
--
I was very young in those days, but I was also rather dim.
-- Christopher Priest
Sep 21 '06 #2
Klaas Vantournhout wrote:
Hi, Somehow I can't figure this one out.

I have an integer variable N and this N reflects the amount of nested
for loops. I mean

if N = 1, the I have

for (int i1 = ....) {
<code>
}

if N = 2, I have

for (int i1 = ...)
for (int i2 = ...) {
<code>
}

...

if N = n I must have

for (int i1 = ...)
...
for (int in = ...) {
<code>
}
Is there a way to program this disregarding the value of N?

thanks
It depends on the value of the upper bound and lower bound obviously
In case all are the same, (upper-lower)^n will do.

For instance:
for (i=0; i<10;i++)
for (j=0; j<10;j++)

<code>

is the same as

for (i=0; i<100; i++)

<code>

100=(10 - 0) ^ 2

jacob
Sep 21 '06 #3
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Klaas Vantournhout wrote:
Hi, Somehow I can't figure this one out.

I have an integer variable N and this N reflects the amount of nested
for loops. I mean

if N = 1, the I have

for (int i1 = ....) {
<code>
}

if N = 2, I have

for (int i1 = ...)
for (int i2 = ...) {
<code>
}

...

if N = n I must have

for (int i1 = ...)
...
for (int in = ...) {
<code>
}
Is there a way to program this disregarding the value of N?
You /could/ do it with recursion.

i.e.

void function(level)
{
int counter;

for (counter = 1; counter < 10; ++counter)
{
if (level == 0)
{
/* do usefull work */
}
else function(level-1);
}
}

- --
Lew Pitcher

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.3 (MingW32) - WinPT 0.11.12

iD8DBQFFEsmeagVFX4UWr64RAmqyAKChO5IIXIxnQkeIS+sLfo C1/Q2RvgCbBJSH
nvH/USZ2UWB/xno67jwEPBw=
=hmbK
-----END PGP SIGNATURE-----

Sep 21 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: dw | last post by:
Hello all. We're doing a site with teams and their members. We've got a page where we need to display people according to who belongs to a which team. I've heard that nested loops are bad, but...
7
by: Egor Shipovalov | last post by:
I'm implementing paging through search results using cursors. Is there a better way to know total number of rows under a cursor than running a separate COUNT(*) query? I think PostgreSQL is bound...
46
by: Neptune | last post by:
Hello. I am working my way through Zhang's "Teach yourself C in 24 hrs (2e)" (Sam's series), and for nested loops, he writes (p116) "It's often necessary to create a loop even when you are...
77
by: Peter Olcott | last post by:
http://www.tommti-systems.de/go.html?http://www.tommti-systems.de/main-Dateien/reviews/languages/benchmarks.html The above link shows that C# is 450% slower on something as simple as a nested loop....
9
by: Gregory Petrosyan | last post by:
I often make helper functions nested, like this: def f(): def helper(): ... ... is it a good practice or not? What about performance of such constructs?
11
by: xctide | last post by:
This project will give you more experience on nested for loops and user-defined functions. The original project came from an IT company’s programming test where they were asking for the most...
5
by: =?Utf-8?B?QUEyZTcyRQ==?= | last post by:
Could someone give me a simple example of nested scope in C#, please? I've searched Google for this but have not come up with anything that makes it clear. I am looking at the ECMA guide and...
13
by: Fredrik Lundh | last post by:
Patrol Sun wrote: so why exactly are you trying to nest 20 or 100 for-in loops? </F>
8
by: Nathan Sokalski | last post by:
I have several nested For loops, as follows: For a As Integer = 0 To 255 For b As Integer = 0 To 255 For c As Integer = 0 To 255 If <Boolean ExpressionThen <My CodeElse Exit For Next If Not...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.