compacting similar nested for loops

Discussion in 'C++' started by SplaTTer, Jul 2, 2003.

  1. SplaTTer

    SplaTTer Guest

    I have a piece of code consisting of k nested for loops, for example:

    int k = 5;
    int *p = new int[k];
    double x,y; //x is read from file


    for(p[0]=0;p[0]<=6;p[0]++)
    for(p[1]=0;p[1]<=3;p[1]++)
    for(p[2]=0;p[2]<=3;p[2]++)
    for(p[3]=0;p[3]<=3;p[3]++)
    for(p[4]=0;p[4]<=3;p[4]++)
    y = assign(x, p);

    This works, however, in a more general version k can be anywhere from
    1 to 10. Therefore p will be of array size k and range from p[0] to
    p[k-1], and I will only need k number of nested for loops. I could
    write code for all the different possible conditions and use if
    statements to select the proper one, but i would rather see if there
    is a way to do it in a more compact manner (like with a recursive
    function or even using the goto statement).
     
    SplaTTer, Jul 2, 2003
    #1
    1. Advertising

  2. "SplaTTer" <> wrote...
    > I have a piece of code consisting of k nested for loops, for example:
    >
    > int k = 5;
    > int *p = new int[k];
    > double x,y; //x is read from file
    >
    >
    > for(p[0]=0;p[0]<=6;p[0]++)
    > for(p[1]=0;p[1]<=3;p[1]++)
    > for(p[2]=0;p[2]<=3;p[2]++)
    > for(p[3]=0;p[3]<=3;p[3]++)
    > for(p[4]=0;p[4]<=3;p[4]++)
    > y = assign(x, p);
    >
    > This works, however, in a more general version k can be anywhere from
    > 1 to 10. Therefore p will be of array size k and range from p[0] to
    > p[k-1], and I will only need k number of nested for loops. I could
    > write code for all the different possible conditions and use if
    > statements to select the proper one, but i would rather see if there
    > is a way to do it in a more compact manner (like with a recursive
    > function or even using the goto statement).


    Yes, recursion should work. And I've read it many times that any
    recursion can be emulated with an array.

    void nestedLoop(int p[], int upperlimit[],
    unsigned level, unsigned maxlevel,
    double& x, double &y)
    {
    if (level < maxlevel)
    for (p[level] = 0; p[level] < upperlimit[level]; ++p[level])
    nestedLoop(p, upperlimit, level+1, maxlevel, x, y);
    else
    y = assign(x, p);
    }

    ....
    int upperlimit[] = { 6,3,3,3,3 }; // not sure of the significance
    // of the first one's being 6.
    nestedLoop(p, upperlimit, 0, k, x, y);

    (Is this right? I didn't test it)

    Victor
     
    Victor Bazarov, Jul 2, 2003
    #2
    1. Advertising

  3. SplaTTer

    SplaTTer Guest

    Thanks a bunch Victor. That worked great. There was only a small
    problem with the for loops going to one less than each of the
    upperlimits, but that was easily fixed.

    To answer your question about the significance of the first element in
    the upperlimit array = {6,3,3,3,3}: the first loop is the most
    important in my program, and 6 happens to be closer to +infinity than
    3. So in other words it was just specific to what i'm using the
    assign(x,p) function for.

    Thanks,

    -Kent
     
    SplaTTer, Jul 2, 2003
    #3
    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. Timothy
    Replies:
    1
    Views:
    835
    rajasekhar
    Jan 2, 2004
  2. Allan Bruce

    dynamic number of nested loops

    Allan Bruce, Jul 1, 2004, in forum: Java
    Replies:
    5
    Views:
    9,777
    Tor Iver Wilhelmsen
    Jul 3, 2004
  3. S N
    Replies:
    2
    Views:
    116
    Ken Schaefer
    Apr 24, 2004
  4. Replies:
    14
    Views:
    216
    Brian McCauley
    Feb 24, 2005
  5. Me
    Replies:
    2
    Views:
    247
Loading...

Share This Page