473,231 Members | 1,605 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,231 software developers and data experts.

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 8886
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
by: Carlos Ribeiro | last post by:
Hello all, Here I am using some deeply nested, tree-like data structures. In some situations I need to traverse the tree; the old-style way to do it is to write a recursive method on the node...
10
by: Steve Goldman | last post by:
Hi, I am trying to come up with a way to develop all n-length permutations of a given list of values. The short function below seems to work, but I can't help thinking there's a better way. ...
7
by: bearophileHUGS | last post by:
(This is a repost from another python newsgroup). While using some nested data structures, I've seen that I'd like to have a function that tells me if a given data structure contains one or more...
2
by: LoserInYourFaceEngineer | last post by:
Hello All: I'm having trouble with a recursive function. The function is supposed to identify nested folders in a hierarchical folder structure. The function "searchForFolders()" is...
25
by: Mike MacSween | last post by:
Regular viewers may want to turn off now. This will be an orchestral management system. Musicians and other staff being booked/paid for jobs. A job may contain other jobs, e.g: World Tour...
2
by: Elliot | last post by:
I'm having trouble structuring the data in an investment database. The basics are: * Investors invest in Deals * Investors can be either Individuals or Entities * Entities may or may not...
20
by: Gary Manigian | last post by:
I have 2 tables, one-to-many, that contain bills of material(BOMs): tblBOM: lngBOMID (PK) strAssemblyPartNo strDescription tblBOMDetail: lngBOMDetailID (PK) lngBOMID (FK)
7
by: pascal | last post by:
Hello All, I'm a middle-low level user of MS Access 2000. I'm trying to find a solution to what appears to be a common problem. By browsing the net and the groups, I've seen several posts and...
6
by: Designing Solutions WD | last post by:
I have the following table. GO /****** Object: Table . Script Date: 05/01/2007 10:42:31 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...

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.