Loops

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • cplusplusquestion@gmail.com

    #1

    Loops

    For the loops:

    for(int i=0; i<FIRST; i++){
    for(int j=0; j<SECOND; j++){
    //some code here
    .......
    }
    }

    Is it possible to change it as one loop?

  • Eric Pruneau

    #2
    Re: Loops


    <cplusplusquest ion@gmail.coma écrit dans le message de news:
    8a837489-b118-4027-bb20-9ceeceac4431...le groups.com...
    For the loops:
    >
    for(int i=0; i<FIRST; i++){
    for(int j=0; j<SECOND; j++){
    //some code here
    .......
    }
    }
    >
    Is it possible to change it as one loop?
    >
    you mean something like

    for(int i=0; i< FIRST*SECOND; ++i)
    {
    ...
    }


    yeah... it is possible

    -----------------

    Eric Pruneau


    Comment

    • ManicQin

      #3
      Re: Loops

      On Jun 23, 9:18 am, "Eric Pruneau" <eric.prun...@c gocable.cawrote :
      <cplusplusquest ...@gmail.coma écrit dans le message de news:
      8a837489-b118-4027-bb20-9ceeceac4...@w1 g2000prd.google groups.com...
      >
      For the loops:
      >
      for(int i=0; i<FIRST; i++){
        for(int j=0; j<SECOND; j++){
            //some code here
            .......
        }
      }
      >
      Is it possible to change it as one loop?
      >
      you mean something like
      >
      for(int i=0; i< FIRST*SECOND; ++i)
      {
          ...
      >
      }
      >
      yeah...  it is possible
      >
      -----------------
      >
      Eric Pruneau
      Why would you like to write it any other way?
      The first way is more clear and easier to understand.
      I hope you understand that Eric's answer only works in 3D arrays and
      not with classes...

      You better leave it the first way, there is no "Better" way that will
      give you better results.

      Comment

      • Kai-Uwe Bux

        #4
        Re: Loops

        ManicQin wrote:
        On Jun 23, 9:18 am, "Eric Pruneau" <eric.prun...@c gocable.cawrote :
        ><cplusplusques t...@gmail.coma écrit dans le message de news:
        >8a837489-b118-4027-bb20-9ceeceac4...@w1 g2000prd.google groups.com...
        >>
        For the loops:
        >>
        for(int i=0; i<FIRST; i++){
        for(int j=0; j<SECOND; j++){
        //some code here
        .......
        }
        }
        >>
        Is it possible to change it as one loop?
        >>
        >you mean something like
        >>
        >for(int i=0; i< FIRST*SECOND; ++i)
        >{
        >...
        >>
        >}
        >>
        >yeah...  it is possible
        With some caveats: FIRST and SECOND might be macros (after all, they are all
        upper case). So, maybe (FIRST)*(SECOND ) is slightly better. And even then,
        the product might overflow.
        >>
        >-----------------
        >>
        >Eric Pruneau
        >
        Why would you like to write it any other way?
        The first way is more clear and easier to understand.
        Right.
        I hope you understand that Eric's answer only works in 3D arrays and
        not with classes...
        Huh? I don't see any 3D-arrays around. And why would there be any problem
        with classes (e.g., inside the loop)? In short, I don't understand what you
        mean. Could you explain?

        [snip]


        Best

        Kai-Uwe Bux


        Comment

        • joseph  cook

          #5
          Re: Loops

          Huh? I don't see any 3D-arrays around. And why would there be any problem
          with classes (e.g., inside the loop)? In short, I don't understand what you
          mean. Could you explain?
          I suppose he could have meant that FIRST is not of int type:
          i.e.
          class FIRST
          {
          public:
          operator int() { ... }
          .... operator++() {...}
          ....
          };

          if the operator* isn't defined, then the suggested change won't be
          equivalant.

          Of course, this is a ridiculous scenario.

          Joe Cook

          Comment

          • ManicQin

            #6
            Re: Loops

            On Jun 23, 9:14 am, cplusplusquest. ..@gmail.com wrote:
            For the loops:
            >
            for(int i=0; i<FIRST; i++){
               for(int j=0; j<SECOND; j++){
                   //some code here
                   .......
               }
            >
            }
            >
            Is it possible to change it as one loop?
            I meant,
            cplusplusquest' s example doesnt say if he is using
            tArr[i,j] <- 3D array
            or
            cVector[i].MemberClassVec tor[j]
            in the first example you can use Eric's post and just

            for(int iMulj=0; iMulj< FIRST*SECOND; ++iMulj)
            tArr[iMulj]

            Because he didnt wrote nothing inside the brackets It could be even
            cout << "Hello world!!! " << i;

            Comment

            • Pete Becker

              #7
              Re: Loops

              On 2008-06-22 21:48:31 -0400, Kai-Uwe Bux <jkherciueh@gmx .netsaid:
              >
              With some caveats: FIRST and SECOND might be macros (after all, they are all
              upper case). So, maybe (FIRST)*(SECOND ) is slightly better.
              If FIRST or SECOND isn't properly parenthesized, fix the problem at the
              source. Don't work around it at all the places it might show up.

              --
              Pete
              Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
              Standard C++ Library Extensions: a Tutorial and Reference
              (www.petebecker.com/tr1book)

              Comment

              Working...