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

compacting similar nested for loops

I have a piece of code consisting of k nested for loops, for example:

int k = 5;
int *p = new int[k];
double x,y; //x is read from file
for(p[0]=0;p[0]<=6;p[0]++)
for(p[1]=0;p[1]<=3;p[1]++)
for(p[2]=0;p[2]<=3;p[2]++)
for(p[3]=0;p[3]<=3;p[3]++)
for(p[4]=0;p[4]<=3;p[4]++)
y = assign(x, p);

This works, however, in a more general version k can be anywhere from
1 to 10. Therefore p will be of array size k and range from p[0] to
p[k-1], and I will only need k number of nested for loops. I could
write code for all the different possible conditions and use if
statements to select the proper one, but i would rather see if there
is a way to do it in a more compact manner (like with a recursive
function or even using the goto statement).
Jul 19 '05 #1
2 2771
"SplaTTer" <sp******@mailcity.com> wrote...
I have a piece of code consisting of k nested for loops, for example:

int k = 5;
int *p = new int[k];
double x,y; //x is read from file
for(p[0]=0;p[0]<=6;p[0]++)
for(p[1]=0;p[1]<=3;p[1]++)
for(p[2]=0;p[2]<=3;p[2]++)
for(p[3]=0;p[3]<=3;p[3]++)
for(p[4]=0;p[4]<=3;p[4]++)
y = assign(x, p);

This works, however, in a more general version k can be anywhere from
1 to 10. Therefore p will be of array size k and range from p[0] to
p[k-1], and I will only need k number of nested for loops. I could
write code for all the different possible conditions and use if
statements to select the proper one, but i would rather see if there
is a way to do it in a more compact manner (like with a recursive
function or even using the goto statement).


Yes, recursion should work. And I've read it many times that any
recursion can be emulated with an array.

void nestedLoop(int p[], int upperlimit[],
unsigned level, unsigned maxlevel,
double& x, double &y)
{
if (level < maxlevel)
for (p[level] = 0; p[level] < upperlimit[level]; ++p[level])
nestedLoop(p, upperlimit, level+1, maxlevel, x, y);
else
y = assign(x, p);
}

....
int upperlimit[] = { 6,3,3,3,3 }; // not sure of the significance
// of the first one's being 6.
nestedLoop(p, upperlimit, 0, k, x, y);

(Is this right? I didn't test it)

Victor
Jul 19 '05 #2
Thanks a bunch Victor. That worked great. There was only a small
problem with the for loops going to one less than each of the
upperlimits, but that was easily fixed.

To answer your question about the significance of the first element in
the upperlimit array = {6,3,3,3,3}: the first loop is the most
important in my program, and 6 happens to be closer to +infinity than
3. So in other words it was just specific to what i'm using the
assign(x,p) function for.

Thanks,

-Kent
Jul 19 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

25
by: chad | last post by:
I am writing a program to do some reliability calculations that require several nested for-loops. However, I believe that as the models become more complex, the number of required for-loops will...
0
by: Xah Lee | last post by:
# -*- coding: utf-8 -*- # Python # David Eppstein of the Geometry Junkyard fame gave this elegant # version for returing all possible pairs from a range of n numbers. def combo2(n): return...
4
by: dw | last post by:
Hello all. We're doing a site with teams and their members. We've got a page where we need to display people according to who belongs to a which team. I've heard that nested loops are bad, but...
46
by: Neptune | last post by:
Hello. I am working my way through Zhang's "Teach yourself C in 24 hrs (2e)" (Sam's series), and for nested loops, he writes (p116) "It's often necessary to create a loop even when you are...
77
by: Peter Olcott | last post by:
http://www.tommti-systems.de/go.html?http://www.tommti-systems.de/main-Dateien/reviews/languages/benchmarks.html The above link shows that C# is 450% slower on something as simple as a nested loop....
9
by: Gregory Petrosyan | last post by:
I often make helper functions nested, like this: def f(): def helper(): ... ... is it a good practice or not? What about performance of such constructs?
5
by: =?Utf-8?B?QUEyZTcyRQ==?= | last post by:
Could someone give me a simple example of nested scope in C#, please? I've searched Google for this but have not come up with anything that makes it clear. I am looking at the ECMA guide and...
13
by: Fredrik Lundh | last post by:
Patrol Sun wrote: so why exactly are you trying to nest 20 or 100 for-in loops? </F>
8
by: Nathan Sokalski | last post by:
I have several nested For loops, as follows: For a As Integer = 0 To 255 For b As Integer = 0 To 255 For c As Integer = 0 To 255 If <Boolean ExpressionThen <My CodeElse Exit For Next If Not...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
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,...
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...
0
tracyyun
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.