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=m; i<n; i++)
for (i=m; i<n; i++)
for (i=m; i<n; i++)
printf("Do something with %2i %2i %2i\n",i,i,i);

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,i,i);

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=m; l0:
i=m; l1:
i=m; l2:
printf("Do something with %2i %2i %2i\n",i,i,i);
i++;if(i<n)goto l2;
i++;if(i<n)goto l1;
i++;if(i<n)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>

#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 == m)
{ 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:

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

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,i,i);
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:

>>  for ( ; ; )
>>     { for (j = K - 1 ; j >= 0 && i[j] ++ >= n[j] ? i[j] = m[j], 1 : 0 ; j --) ;
>>       if (j < 0 && i == m)
>>         { 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

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

 Page 1 of 1 [ 5 post ]

Relevant Pages