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

S

so.intech

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?
 
C

Chris Dollin

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?

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 ...]
 
E

Eric Sosman

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.
 
R

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;
}
 
S

so.intech

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)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,905
Latest member
Kristy_Poole

Latest Threads

Top