Loop of loops. Any nice solution? 
Author Message
 Loop of loops. Any nice solution?

Hello, I have a problem which I'm not sure if there is a nice solution.
I need a sequence of loops imbedded one inside the other, with variable depth.
Well, here is an example.  The following program will loop around all the
3-bit strings.
                        /****** loop3.c ********/
const int K=3;
main()
{
  int m[K],n[K],i[K], j;  for (j=0;j<K;j++) { m[j]=0; n[j]=2; }
  for (i[0]=m[0]; i[0]<n[0]; i[0]++)
        for (i[1]=m[1]; i[1]<n[1]; i[1]++)
          for (i[2]=m[2]; i[2]<n[2]; i[2]++)
                printf("Do something with %2i %2i %2i\n",i[0],i[1],i[2]);

Quote:
}

Now what if I want all the K-bit strings, where K is any integer?
Suppose I want to experiment with K=1...20.  Do I have to supply 20
versions of source code with different numbers of 'for' statements?
Obviously, the following naive 'solution' does not work:
                        /****** looploop.c *******/
const int K=3;
main()
{
  int m[K],n[K],i[K],j;  for (j=0;j<K;j++) { m[j]=0; n[j]=2; }
  for (j=0;j<K;j++)
        for (i[j]=m[j]; i[j]<n[j]; i[j]++)
          printf("Do something with %2i %2i %2i\n",i[0],i[1],i[2]);

Quote:
}

The reason this does not work can be seen easily from the following program
which is equivalent to loop3.c.
                        /******* goto3.c ********/
const int K=3;
main()
{
  int m[K],n[K],i[K],j;  for (j=0;j<K;j++) { m[j]=0; n[j]=2; }
  i[0]=m[0]; l0:
  i[1]=m[1]; l1:
  i[2]=m[2]; l2:
  printf("Do something with %2i %2i %2i\n",i[0],i[1],i[2]);
  i[2]++;if(i[2]<n[2])goto l2;
  i[1]++;if(i[1]<n[1])goto l1;
  i[0]++;if(i[0]<n[0])goto l0;

Quote:
}

That is, the labels are not variable.  However, I've found a solution
                        /****** gotoloop.c ******/
const int K=3;
main()
{
  int m[K],n[K],i[K],j, l;
  for (j=0;j<K;j++) { m[j]=0; n[j]=2; }
  l=0;
 start:
  for (j=l+1;j<K;j++) i[j]=m[j];
  { for (j=0;j<K;j++) printf("%2i ",i[j]);  printf("\n"); }
  for (l=K-1;l>=0;l--)
        {(i[l])++; if(i[l]<n[l]) goto start;}

Quote:
}

Apart from using 'goto', this program is not very illuminating, either.
(It helps the understanding a little bit by noting that l denote the current
layer of loop.)  So is there a better solution?


---

Dept of Computer Sciences and Applied Mathematics
Aston University, Birmingham B4 7ET, UK



Sat, 11 Jan 1997 04:02:33 GMT  
 Loop of loops. Any nice solution?

Quote:
>Hello, I have a problem which I'm not sure if there is a nice solution.
>I need a sequence of loops imbedded one inside the other, with variable depth.
>Well, here is an example.  The following program will loop around all the
>3-bit strings.

<fortran code deleted>

Quote:
>Now what if I want all the K-bit strings, where K is any integer?
>Suppose I want to experiment with K=1...20.  Do I have to supply 20
>versions of source code with different numbers of 'for' statements?
>Obviously, the following naive 'solution' does not work:

<more fortran code deleted>

How about:

#define K 4
main()
{
  int m [K],
       n [K],
       i [K] ;

  int j ;

  for (j=0 ; j<K ; j++)
     { i [j] = m[j]=0 ;
       n[j]=2 ;
     }

  for ( ; ; )
     { for (j = K - 1 ; j >= 0 && i[j] ++ >= n[j] ? i[j] = m[j], 1 : 0 ; j --) ;
       if (j < 0 && i[0] == m[0])
         { break ;
         }
       printf("Do something with ") ;
       for (j = 0 ; j < K ; j ++)
            { printf ("%2d ", i[j]) ;
            }
       printf("\n") ;
     }

Quote:
}

William.


Sat, 11 Jan 1997 15:21:33 GMT  
 Loop of loops. Any nice solution?

Quote:

>How about:

>  for ( ; ; )
>     { for (j = K - 1 ; j >= 0 && i[j] ++ >= n[j] ? i[j] = m[j], 1 : 0 ; j --) ;
>       if (j < 0 && i[0] == m[0])
>         { break ;
>         }
>       printf("Do something with ") ;
>       for (j = 0 ; j < K ; j ++)
>            { printf ("%2d ", i[j]) ;
>            }
>       printf("\n") ;
>     }

  This is a mistake.  I know I will get flamed, and I intend no offense
but that has got to be the oddest formatting style I have ever seen...

--
                  Dave Moore - Student at Large - Reed College

                                  --=---=--



Sat, 11 Jan 1997 21:25:52 GMT  
 Loop of loops. Any nice solution?
: Hello, I have a problem which I'm not sure if there is a nice solution.
: I need a sequence of loops imbedded one inside the other, with variable depth.
: Well, here is an example.  The following program will loop around all the
: 3-bit strings.
:                       /****** loop3.c ********/
: const int K=3;
: main()
: {
:   int m[K],n[K],i[K], j;  for (j=0;j<K;j++) { m[j]=0; n[j]=2; }
:   for (i[0]=m[0]; i[0]<n[0]; i[0]++)
:       for (i[1]=m[1]; i[1]<n[1]; i[1]++)
:         for (i[2]=m[2]; i[2]<n[2]; i[2]++)
:               printf("Do something with %2i %2i %2i\n",i[0],i[1],i[2]);
: }

You can try recursion:

int K=3;
int m[K],n[K],i[K];

void Loop(int cur)
  {
    if (cur==K)
      printf("Do something with %d %d %d\n",i[0],i[1],i[2]);
    else
      for (i[cur]=m[cur]; i[cur]<n[cur]; i[cur]++)
        Loop(cur+1);
  }

void main(void)
  {
    int j;

    for (j=0; j<K; j++) { m[j]=0; n[j]=2; }
    Loop(0);
  }

  YUAN, Feng
  HP Singapore



Sat, 11 Jan 1997 13:56:56 GMT  
 Loop of loops. Any nice solution?

Quote:


>>How about:

>>  for ( ; ; )
>>     { for (j = K - 1 ; j >= 0 && i[j] ++ >= n[j] ? i[j] = m[j], 1 : 0 ; j --) ;
>>       if (j < 0 && i[0] == m[0])
>>         { break ;
>>         }
>>       printf("Do something with ") ;
>>       for (j = 0 ; j < K ; j ++)
>>            { printf ("%2d ", i[j]) ;
>>            }
>>       printf("\n") ;
>>     }

>  This is a mistake.  I know I will get flamed, and I intend no offense
>but that has got to be the oddest formatting style I have ever seen...

Yes. It is easy to see that this code is wrong if one has ever tested it.
(Set K=2 and n[j]=1. It is wrong in two accounts).  
BTW, I just can't see why a code is more "C" than "fortran" simply because
it is both wrong and hard to understand.  It seems that C style is simply
loads of "++--&&||;;?:". :-)

However, thanks to a hint in his example, I found the following humble
"fortran code" which does the job.

  do    { for (l=0; l<K; l++) printf("%2i ", i[l]); printf("\n");
          for (l=K-1; l>=0; l--)
                { i[l]++; if(i[l]<n[l]) break; else i[l]=m[l];}
        } while (l>=0);

Other people have suggested to me of using recursion, which is neat for this
example, but is not suitable for the actual problem I intended to solve for
some other reasons.  

Thanks to every one.  I've learned a lot.

---

Dept of Computer Sciences and Applied Mathematics
Aston University, Birmingham B4 7ET, UK



Mon, 13 Jan 1997 02:09:49 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. For loops into for loops

2. Loop or Loops in "C"

3. (Newbie) My Loop Isn't Looping - Aargh!

4. SEMA - WHILE loop in a FOR loop !

5. For loops into for loops

6. wanted - C and/or Fortran source for 24-loop Livermore Loops

7. So what's so nice about nice()?

8. "nice" compiler feature about loop control variables (warning C4288)

9. My Loop Now Loops! Thanks!

10. process loops but 'context' doesn't show me where it loops

11. hate the do-while loop (Re: ugly loop; hate the ! operator)

12. For loop design ( Brief Lesson )

 

 
Powered by phpBB® Forum Software