too many loops

Hi,

I want to write a code which has unknown number of loops:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

int x[n];
for(int i=0; i<10; i++) {
   x[0] = i+1;
   for(int j=0; j<10; j++) 
     x[1] = j+1;
     for(int k=0; k<10; k++)
        x[2] = k+1;
        .............
        upto n loops
        do something with x[n]

     }

   }
}


Because n is unknown at the compilation time and it could be large (> 10), I want to write these loops in some compact form. Any idea how to write all these loops in compact form?

Thanks.
Put the loop in a function and call it recursively.
Well, x[n] increment is different in different loop, I am not sure it works. Could you give me an example?
1
2
3
4
5
6
7
8
int Function(int Limit, int Increment)
{
    for(int i = 0; i < Limit; i = i  + Increament)
    {
             //Code Code Code Call This Function From Some Where In Here
     }
     return something;
} 
This no different than the code that I have given above.
It potentially is, look at Line 1. Have you covered recursion in your class yet?
In general, when you don't know the number of times a loop is goint to be executed, you have to use:
do {single loop} while (condition);
or
while (condition) {single loop}

That implies you know how to express condition

Hope this helps
Topic archived. No new replies allowed.