Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > unkown number of nested for loops

Reply
Thread Tools

unkown number of nested for loops

 
 
Klaas Vantournhout
Guest
Posts: n/a
 
      09-21-2006
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
 
Reply With Quote
 
 
 
 
Walter Roberson
Guest
Posts: n/a
 
      09-21-2006
In article <eeud9t$37o$(E-Mail Removed)>,
Klaas Vantournhout <(E-Mail Removed)> 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 = 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
 
Reply With Quote
 
 
 
 
jacob navia
Guest
Posts: n/a
 
      09-21-2006
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
 
Reply With Quote
 
Lew Pitcher
Guest
Posts: n/a
 
      09-21-2006
-----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-----

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Need help finding installers for unkown drivers OasisPriest Hardware 1 02-02-2009 05:34 PM
how to pass unkown length of parameters into a class method Peter Loftus Ruby 2 12-18-2007 06:05 PM
Loops with loops using html-template Me Perl Misc 2 01-12-2006 05:07 PM
nested loops and conditional statements Porthos XML 3 02-07-2005 08:47 PM
dynamic number of nested loops Allan Bruce Java 5 07-03-2004 09:12 PM



Advertisments