unkown number of nested for loops

Discussion in 'C Programming' started by Klaas Vantournhout, Sep 21, 2006.

  1. 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 = ....) {
    <code>
    }

    if N = 2, I have

    for (int i1 = ...)
    for (int i2 = ...) {
    <code>
    }

    ....

    if N = n I must have

    for (int i1 = ...)
    ....
    for (int in = ...) {
    <code>
    }


    Is there a way to program this disregarding the value of N?

    thanks
    Klaas Vantournhout, Sep 21, 2006
    #1
    1. Advertising

  2. In article <eeud9t$37o$>,
    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 = n I must have


    >for (int i1 = ...)
    >...
    >for (int in = ...) {
    ><code>
    >}


    >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
    Walter Roberson, Sep 21, 2006
    #2
    1. Advertising

  3. Klaas Vantournhout

    jacob navia Guest

    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 = ....) {
    > <code>
    > }
    >
    > if N = 2, I have
    >
    > for (int i1 = ...)
    > for (int i2 = ...) {
    > <code>
    > }
    >
    > ...
    >
    > if N = n I must have
    >
    > for (int i1 = ...)
    > ...
    > for (int in = ...) {
    > <code>
    > }
    >
    >
    > 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++)

    <code>

    is the same as

    for (i=0; i<100; i++)

    <code>

    100=(10 - 0) ^ 2

    jacob
    jacob navia, Sep 21, 2006
    #3
  4. Klaas Vantournhout

    Lew Pitcher Guest

    -----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 = ....) {
    > <code>
    > }
    >
    > if N = 2, I have
    >
    > for (int i1 = ...)
    > for (int i2 = ...) {
    > <code>
    > }
    >
    > ...
    >
    > if N = n I must have
    >
    > for (int i1 = ...)
    > ...
    > for (int in = ...) {
    > <code>
    > }
    >
    >
    > 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+sLfoC1/Q2RvgCbBJSH
    nvH/USZ2UWB/xno67jwEPBw=
    =hmbK
    -----END PGP SIGNATURE-----
    Lew Pitcher, Sep 21, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Allan Bruce

    dynamic number of nested loops

    Allan Bruce, Jul 1, 2004, in forum: Java
    Replies:
    5
    Views:
    9,761
    Tor Iver Wilhelmsen
    Jul 3, 2004
  2. Porthos
    Replies:
    3
    Views:
    482
    Joris Gillis
    Feb 7, 2005
  3. SplaTTer
    Replies:
    2
    Views:
    377
    SplaTTer
    Jul 2, 2003
  4. Peter Loftus
    Replies:
    2
    Views:
    124
    cruiserdan
    Dec 18, 2007
  5. Me
    Replies:
    2
    Views:
    241
Loading...

Share This Page