By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,440 Members | 1,420 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,440 IT Pros & Developers. It's quick & easy.

Generalizing a nested loop

P: 5
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
Share this Question
Share on Google+
7 Replies


100+
P: 1,646
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

P: 5
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

100+
P: 1,646
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

P: 5
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

100+
P: 1,646
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

P: 5
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

P: 5
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

Post your reply

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