473,396 Members | 1,693 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.

Generalizing a nested loop

I have a nested loop which generates ordered pairs, triplets, ... of integers from 0 to a maximum N.
Expand|Select|Wrap|Line Numbers
  1. for ( a[0]=0;a[0]<N;a[0]++)
  2.    for( a[1]=0;a[1]<a[0];a[1]++)
  3.       for( a[2]=0;a[2]<a[1];a[2]++)
  4.  
I would like to generalize this for a[n] for given n, or find some more creative way of generating these sets.
Any help is much appreciated.
Dec 29 '06 #1
7 1862
willakawill
1,646 1GB
I have a nested loop which generates ordered pairs, triplets, ... of integers from 0 to a maximum N.
Expand|Select|Wrap|Line Numbers
  1. for ( a[0]=0;a[0]<N;a[0]++)
  2.    for( a[1]=0;a[1]<a[0];a[1]++)
  3.       for( a[2]=0;a[2]<a[1];a[2]++)
  4.  
I would like to generalize this for a[n] for given n, or find some more creative way of generating these sets.
Any help is much appreciated.
Hi are n and N different values?
Dec 29 '06 #2
Yes they are different sorry for the confusion. N is just the maximum value allowed and n is the number of elements taken at a time 2,3,4... this is what I would like to make general.
Dec 29 '06 #3
willakawill
1,646 1GB
Yes they are different sorry for the confusion. N is just the maximum value allowed and n is the number of elements taken at a time 2,3,4... this is what I would like to make general.
OK. here you go
Expand|Select|Wrap|Line Numbers
  1.    const int n = 3;
  2.    int a[n];
  3.    int N = 100;
  4.  
  5.    for (int i = 0; i < n; i++)
  6.       a[i] = N - (i + 1);
Dec 29 '06 #4
Thank you for your reply but that is not quite what I am looking for. I need all possiblities, as produced by this code for n=3 and N=5. If I want n=4 then I need to add an additional loop
for ( a[3]=0;a[3]<a[2];a[3]++)
and having to change this for different n or n>10 i would like to avoid.

Expand|Select|Wrap|Line Numbers
  1. #include<iostream>
  2. int main()
  3. {
  4.   const int n=3;
  5.   int N=5,a[n];
  6.  
  7.   for ( a[0]=0;a[0]<N;a[0]++)
  8.     for( a[1]=0;a[1]<a[0];a[1]++)
  9.       for( a[2]=0;a[2]<a[1];a[2]++)
  10.         std::cout<<a[0]<<','<<a[1]<<','<<a[2]<<'\n';
  11.  
  12.   return 0;
  13. }
  14.  
Dec 29 '06 #5
willakawill
1,646 1GB
Thank you for your reply but that is not quite what I am looking for. I need all possiblities, as produced by this code for n=3 and N=5. If I want n=4 then I need to add an additional loop
for ( a[3]=0;a[3]<a[2];a[3]++)
and having to change this for different n or n>10 i would like to avoid.

Expand|Select|Wrap|Line Numbers
  1. #include<iostream>
  2. int main()
  3. {
  4.   const int n=3;
  5.   int N=5,a[n];
  6.  
  7.   for ( a[0]=0;a[0]<N;a[0]++)
  8.     for( a[1]=0;a[1]<a[0];a[1]++)
  9.       for( a[2]=0;a[2]<a[1];a[2]++)
  10.         std::cout<<a[0]<<','<<a[1]<<','<<a[2]<<'\n';
  11.  
  12.   return 0;
  13. }
  14.  
You have just added a line of code that completely changes your question
Dec 29 '06 #6
I should have posted this code to begin, I need to make use of the values in each loop, not just the final loop.
Dec 29 '06 #7
Well, I found a solution in a recursive function, the only thing I am not thrilled about is that instructions are now in the recursive function 'loop' after the else. Thanks willakawill for replying.

Expand|Select|Wrap|Line Numbers
  1. #include<iostream>
  2.  
  3. void loop(int a[], int n,int i){
  4.   for (a[i]=0;a[i]<a[i-1];a[i]++){
  5.     if ((i+1)<n) loop( a, n, i+1);
  6.     else std::cout<<a[0]<<','<<a[1]<<','<<a[2]<<'\n';
  7.   }
  8. }
  9.  
  10. int main()
  11. {
  12.   const int n=3;
  13.   int N=5,a[n];  
  14.  
  15.   for ( a[0]=0;a[0]<N;a[0]++)
  16.     loop(a,n,1);
  17.  
  18.   return 0;
  19. }
  20.  
  21.  
Dec 29 '06 #8

Sign in to post your reply or Sign up for a free account.

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: mark | last post by:
My problem is I need to have a "nested" repeater. I have an array which I load into a hashtable - that part works great. I can setup the second repeater to work just fine, as long as it's not...
5
by: Martin Schou | last post by:
Please ignore the extreme simplicity of the task :-) I'm new to C, which explains why I'm doing an exercise like this. In the following tripple nested loop: int digit1 = 1; int digit2 = 0;...
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...
17
by: Peter Olcott | last post by:
http://www.tommti-systems.de/go.html?http://www.tommti-systems.de/main-Dateien/reviews/languages/benchmarks.html Why is C# 500% slower than C++ on Nested Loops ??? Will this problem be solved in...
9
by: Javaman59 | last post by:
Using local declarations within a block often makes code more readable, but is it less efficient? eg... void P() { while (...) { int i = ...; bool b = ...; .... } }
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....
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...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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
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.