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? 4 8920
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.
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
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 recursiveforloo p(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)
{
recursiveforloo p(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];
recursiveforloo p(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)
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 recursiveforloo p(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)
{
recursiveforloo p(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];
recursiveforloo p(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)
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 class, as in:
def walk(self):
"""old-style recursive tree traversal"""
child.do_something
for child in childs:
|
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. Not being a computer scientist, I find
recursive functions to be frightening and unnatural. I'd appreciate if
anyone can tell me the pythonic idiom to accomplish this.
Thanks for your help,
|
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 cyclic references (a way to recognise a cycle in a graph is to do
a depth-first search, marking vertices along the way. An already marked
vertex means a cycle.)
Do you know where I can find a function like this?
To be more explicit about this...
|
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 supposed to traverse sibling
nodes in each iteration, and for each sibling node, it calls itself
again, to see if there are child nodes of the current sibling.
|
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 contains
US leg and Europe leg (and others)
US leg contains State tours (and others)
| |
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 contain individual Members
* Entity Members can be either Individuals or Entities
* An Investor in one Deal may be a Member of an Entity that is an
Investor in another (or even the same) Deal.
* There is no limit to the depth of this "tree"
|
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)
|
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 solutions to that problem
but I've not been able to find a ready to use query or module directly
working into MS Access.
The problem is the standard BOM problem.
I have a BOM structure setup in 2 tables:
|
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
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |