automate nested loop

  • Thread starter Thread starter tony collier
  • Start date Start date
T

tony collier

Hi

i want to get a user to input x which then creates an nested loop with x
levels. each level of the loop has the same number of iterations e.g.

for the case where x=3, the following code is executed

int [] i=new int[x-1];

for (i[2]=0;i[2]<limit;i[2]++)
{
for (i[1]=0;i[1]<limit;i[1]++)
{
for (i[0]=0;i[0]<limit;i[0]++)
{
newLowTotal=Sum(i[0],i[1],i[2]);
}
}
}



for the case where x=4, the above would be wrapped by

for (i[3]=0;i[3]<limit;i[3]++)
{

.......
newLowTotal=Sum(i[1],i[2],i[3],i[4]);

.......
}



and notice the Sum array changes as well!

My initial thoughts are an arraylist might solve the varied size of Sum
array and iteration rather than for-looping might solve varied looping
size. But I can't really work out how. Anyone got any ideas?
 
Hi

i want to get a user to input x which then creates an nested loop with
x levels. each level of the loop has the same number of iterations
e.g.

for the case where x=3, the following code is executed

int [] i=new int[x-1];

for (i[2]=0;i[2]<limit;i[2]++)
{
for (i[1]=0;i[1]<limit;i[1]++)
{
for (i[0]=0;i[0]<limit;i[0]++)
{

newLowTotal=Sum(i[0],i[1],i[2]);
}
}
}



for the case where x=4, the above would be wrapped by

for (i[3]=0;i[3]<limit;i[3]++)
{

......
newLowTotal=Sum(i[1],i[2],i[3],i[4]);

......
}



and notice the Sum array changes as well!

My initial thoughts are an arraylist might solve the varied size of
Sum array and iteration rather than for-looping might solve varied
looping size. But I can't really work out how. Anyone got any ideas?

sorry - the 2nd newLowTotal statement should read:

newLowTotal=Sum(i[0],i[1],i[2],i[3]);
 
Hi

i want to get a user to input x which then creates an nested loop
with x levels. each level of the loop has the same number of
iterations e.g.

for the case where x=3, the following code is executed

int [] i=new int[x-1];

for (i[2]=0;i[2]<limit;i[2]++)
{
for (i[1]=0;i[1]<limit;i[1]++)
{
for (i[0]=0;i[0]<limit;i[0]++)
{

newLowTotal=Sum(i[0],i[1],i[2]);
}
}
}



for the case where x=4, the above would be wrapped by

for (i[3]=0;i[3]<limit;i[3]++)
{

......
newLowTotal=Sum(i[1],i[2],i[3],i[4]);

......
}



and notice the Sum array changes as well!

My initial thoughts are an arraylist might solve the varied size of
Sum array and iteration rather than for-looping might solve varied
looping size. But I can't really work out how. Anyone got any
ideas?

sorry - the 2nd newLowTotal statement should read:

newLowTotal=Sum(i[0],i[1],i[2],i[3]);
I must drink coffee - i meant recursive function , not iteration
 
Back
Top