470,572 Members | 2,561 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

how to use recursive structure to build arbitrary levels' nested loop?

for example,
ret = 0;
for(i=0; i<3; i ++;)
{
for(j=0; j<4; j++;)
{
for(k=0; k<3; k++;)
{
for(m=0; m<4; m++;)
{
. . . //lots of groups <3 , <4 nested loops
ret ++;
}
}
}
}

how can i write a recursive function to implement this?

Aug 8 '06 #1
4 8663
so.intech wrote:
for example,
ret = 0;
for(i=0; i<3; i ++;)
{
for(j=0; j<4; j++;)
{
for(k=0; k<3; k++;)
{
for(m=0; m<4; m++;)
{
. . . //lots of groups <3 , <4 nested loops
ret ++;
}
}
}
}

how can i write a recursive function to implement this?
The usual way.

Write a function F1 for the base case.

Write a function F2 which, if we're at the base case, calls F1.
Otherwise, deal with one level of the recursion and inside
that call F2 passing the reduced case.

Write a function F3 that calls F2 with the outermost case
(and any outer context information).

Inline and/or parameterise as you see fit. You may be able
to replace the recursion with iteration plus a stack
once you have the details sorted out: or it may not be
worth it. It Depends.

[I did this, except in Java, for turning multi-triple RDF
queries into nested single-triple queries. Later I replaced
the explicit recursion with a pregenerated nest of objects;
because the objects could be specialised to their particular
triple pattern, I exposed Optimisation Opportunities, although
it still relies on the call stack. This is probably overkill for
your problem ...]

--
Chris "no problem too difficult to complicate" Dollin
A rock is not a fact. A rock is a rock.

Aug 8 '06 #2


so.intech wrote On 08/08/06 10:57,:
for example,
ret = 0;
for(i=0; i<3; i ++;)
{
for(j=0; j<4; j++;)
{
for(k=0; k<3; k++;)
{
for(m=0; m<4; m++;)
{
. . . //lots of groups <3 , <4 nested loops
ret ++;
}
}
}
}

how can i write a recursive function to implement this?
void loopit(int levels) {
if (levels <= 0) {
/* "body" of "nested loop" */
}
else {
int i;
for (i = 0; i < 10; ++i)
loopit (levels-1);
}
}

That's the essence, but minimally useful as it stands.
In a more realistic situation you might want to make
different loop indices cover different ranges, instead of
all running from zero through nine. You'd also probably
want to make the indices' current values available to the
"body." You should have no trouble figuring out such
details, but since there's a faint odor of homework about
the question I think I'll leave the figuring out to you.

--
Er*********@sun.com

Aug 8 '06 #3
so.intech said:
for example,
ret = 0;
for(i=0; i<3; i ++;)
{
for(j=0; j<4; j++;)
{
for(k=0; k<3; k++;)
{
for(m=0; m<4; m++;)
{
. . . //lots of groups <3 , <4 nested loops
ret ++;
}
}
}
}

how can i write a recursive function to implement this?
Here's one way you could do it:

#include <stdio.h>

struct for_loop_vars_
{
const char *name;
int start;
int end;
int modifier;
};

typedef struct for_loop_vars_ for_loop_vars;

void recursiveforloop(for_loop_vars *p, size_t d)
{
int i;

printf("----------- LOOP START ---------------\n");
printf("Loop name: %s\n", p->name);
printf("Begin at: %d\n", p->start);
printf("Modify by: %d\n", p->modifier);
printf("End at: %d\n\n", p->end);

for(i = p->start; i != p->end; i += p->modifier)
{
if(d 0)
{
recursiveforloop(p + 1, d - 1);
}
else
{
printf("I'm in the inmost loop - index is %d\n", i);
}
}
printf("----------- LOOP END ---------------\n");
}

int main(void)
{
for_loop_vars control[] =
{
{ "Outermost", 0, 3, 1 },
{ "Outerish", 0, 4, 1 },
{ "Innerish", 0, 3, 1 }, /* add loops to taste! */
{ "Innermost", 0, 9, 1 }
};
size_t depth_to_go = sizeof control / sizeof control[0];

recursiveforloop(control, depth_to_go - 1);

return 0;
}

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Aug 8 '06 #4
Thanks guys.
Richard Heathfield 写道:
so.intech said:
for example,
ret = 0;
for(i=0; i<3; i ++;)
{
for(j=0; j<4; j++;)
{
for(k=0; k<3; k++;)
{
for(m=0; m<4; m++;)
{
. . . //lots of groups <3 , <4 nested loops
ret ++;
}
}
}
}

how can i write a recursive function to implement this?

Here's one way you could do it:

#include <stdio.h>

struct for_loop_vars_
{
const char *name;
int start;
int end;
int modifier;
};

typedef struct for_loop_vars_ for_loop_vars;

void recursiveforloop(for_loop_vars *p, size_t d)
{
int i;

printf("----------- LOOP START ---------------\n");
printf("Loop name: %s\n", p->name);
printf("Begin at: %d\n", p->start);
printf("Modify by: %d\n", p->modifier);
printf("End at: %d\n\n", p->end);

for(i = p->start; i != p->end; i += p->modifier)
{
if(d 0)
{
recursiveforloop(p + 1, d - 1);
}
else
{
printf("I'm in the inmost loop - index is %d\n", i);
}
}
printf("----------- LOOP END ---------------\n");
}

int main(void)
{
for_loop_vars control[] =
{
{ "Outermost", 0, 3, 1 },
{ "Outerish", 0, 4, 1 },
{ "Innerish", 0, 3, 1 }, /* add loops to taste! */
{ "Innermost", 0, 9, 1 }
};
size_t depth_to_go = sizeof control / sizeof control[0];

recursiveforloop(control, depth_to_go - 1);

return 0;
}

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Aug 8 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

19 posts views Thread by Carlos Ribeiro | last post: by
10 posts views Thread by Steve Goldman | last post: by
7 posts views Thread by bearophileHUGS | last post: by
2 posts views Thread by LoserInYourFaceEngineer | last post: by
25 posts views Thread by Mike MacSween | last post: by
7 posts views Thread by pascal | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.