424,949 Members | 1,086 Online Need help? Post your question and get tips & solutions from a community of 424,949 IT Pros & Developers. It's quick & easy.

unkown number of nested for loops

 P: n/a Hi, Somehow I can't figure this one out. I have an integer variable N and this N reflects the amount of nested for loops. I mean if N = 1, the I have for (int i1 = ....) { } if N = 2, I have for (int i1 = ...) for (int i2 = ...) { } .... if N = n I must have for (int i1 = ...) .... for (int in = ...) { } Is there a way to program this disregarding the value of N? thanks Sep 21 '06 #1
3 Replies

 P: n/a In article , Klaas Vantournhout Hi, Somehow I can't figure this one out. >I have an integer variable N and this N reflects the amount of nestedfor loops. I mean >if N = n I must have >for (int i1 = ...)...for (int in = ...) {} >Is there a way to program this disregarding the value of N? int *indices = malloc(N * sizeof(int)); then indices[N-1] corresponds to iN -- I was very young in those days, but I was also rather dim. -- Christopher Priest Sep 21 '06 #2

 P: n/a Klaas Vantournhout wrote: Hi, Somehow I can't figure this one out. I have an integer variable N and this N reflects the amount of nested for loops. I mean if N = 1, the I have for (int i1 = ....) { } if N = 2, I have for (int i1 = ...) for (int i2 = ...) { } ... if N = n I must have for (int i1 = ...) ... for (int in = ...) { } Is there a way to program this disregarding the value of N? thanks It depends on the value of the upper bound and lower bound obviously In case all are the same, (upper-lower)^n will do. For instance: for (i=0; i<10;i++) for (j=0; j<10;j++) is the same as for (i=0; i<100; i++) 100=(10 - 0) ^ 2 jacob Sep 21 '06 #3

 P: n/a -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Klaas Vantournhout wrote: Hi, Somehow I can't figure this one out. I have an integer variable N and this N reflects the amount of nested for loops. I mean if N = 1, the I have for (int i1 = ....) { } if N = 2, I have for (int i1 = ...) for (int i2 = ...) { } ... if N = n I must have for (int i1 = ...) ... for (int in = ...) { } Is there a way to program this disregarding the value of N? You /could/ do it with recursion. i.e. void function(level) { int counter; for (counter = 1; counter < 10; ++counter) { if (level == 0) { /* do usefull work */ } else function(level-1); } } - -- Lew Pitcher -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.3 (MingW32) - WinPT 0.11.12 iD8DBQFFEsmeagVFX4UWr64RAmqyAKChO5IIXIxnQkeIS+sLfo C1/Q2RvgCbBJSH nvH/USZ2UWB/xno67jwEPBw= =hmbK -----END PGP SIGNATURE----- Sep 21 '06 #4 